Implementing a search feature for my hugo static website

2021-09-19 - A golang webservice to complement hugo
Tags: golang hugo


When I migrated my personal website from dokuwiki to hugo two years ago, the main feature I lost was a way to search the website content. I used tags to mimic a fraction of this power, but with more and more articles over the years I felt the pressure to finally do something.

Hugo can easily generate a json index of the website, and according to my google-fu hugo users use javascript solutions to implement search on top of this. I was not satisfied by the idea of having javascript download the whole website index and running searches locally, but I found no alternative. Since I love having a javascript free website I wanted to keep it that way if possible, so I designed an alternative.

This blog post retraces my steps in programming a custom search feature in golang and hosting it alongside my blog on my personal infrastructure.

The design

Up until now the website was served from a simple nginx configuration. I plan to write a golang webservice, have nginx redirect the /search path to it and keep serving all the other paths statically. Having some experience from my work on my trains webapp I had a pretty good idea how to write the http handler, I just needed to figure out three things :

The JSON index

There are two things to configure in order to get a JSON index from hugo. The first one is to activate JSON as an output format in config.toml :

home = ["HTML", "RSS", "JSON"]

The second is to create a layouts/index.json template of what the index will contain. Here is mine, if you decide to tweak it you will need to rework the search algorithm accordingly :

{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
    {{- $.Scratch.Add "index" (dict "title" .Title "description" .Params.description "tags"
    .Params.tags "content" .Plain "permalink" .Permalink) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

How to maintain a coherent look

Since I am writing a golang webservice I can use its HTML templating library. It is handy because hugo uses the same library, but I cannot just reuse my hugo templates since they use hugo functions and api which would be a headache to mimic. One of the main pain points is to be able to reuse the css hugo generates, fingerprint and all.

In order to solve this I thought about my custom hugo shortcodes and figured a way to use it to my advantage. I added a content section with a single page in content/search/ :

title: "Search"
    weight: 1
layout: single

What are you looking for ?

To render this page I wrote the corresponding layout in layouts/search/single.html :

{{ define "main" }}

{{ .Content }}

{{ print "{{ template \"search\" . }}" }}

{{ end }}

And VOILĂ€! When building the hugo website, it generates a page that contains a simple and valid html template that I can render :

<!-- headers with css and everything exactly as on every page of the website,
     as well as the menu and its section highlighting) -->
<p>What are you looking for ?</p>
{{ template "search" . }}
<!-- the footer -->

The search template just need to be written accordingly, and the http templating library will do the rest.

The golang webservice

The webservice lives in a folder of my hugo repository and can be found here. The website’s makefile first builds the hugo website, then copies the HTML template and the json index in the search folder. It then builds the golang binary and embeds these.

When the webservice starts, it parses the JSON index and generates separate lists of unique words found in titles, descriptions, tags and page content. These lists each have a weight that factors in the results when the searched words are found in the list via a simple string.Contains match.

It is not very clever but because my website is small it produces pertinent results with this very simple algorithm. A friend pointed to me that Radix Trees are the efficient way to search if a word is part of a text and I plan to look into implementing that one day.


I find that this search feature works well and looks even better! Being light and fast it fits perfectly with my philosophy for this website, and I find that managing a server side service is preferable to pushing the burden to the visitor’s browser.

Future improvements I am considering (beside radix trees) are :