Search is a handy feature you might want on your Hugo static site.
Hugo does not have any built-in way to provide a search function, but they do offer some suggestions on how to do it yourself.

The difficulty with implementing a search function is that it usually requires some kind of server side logic, which is obviously missing when using a static site like Hugo.
So any search implementation must be done entirely on the client side.
One of the options Hugo suggests offers an implementation using jQuery and Fuse.js.

This seemed a bit overkill to me.
I did not want to have to add two (rather large) JavaScript libraries to my site simply to provide a simple feature like search.
Further, even after trying Fuse.js I was utterly confounded by the search results it returned and found the documentation incomplete and confusing.
For example, while testing Fuse I searched my site for “fpga”.
As of this writing, there are two posts on this site containing the word “fpga”, so I would expect the search to return those two posts and those two posts only.
However, Fuse completely failed to find one of the posts and also returned several false positives!
This happens because Fuse uses a fuzzy search algorithm, so the search term “fpga” matches a lot of things besides the term itself, which is not what I wanted.

Fortunately, there is a better way.

Hugo can create a JSON index of your entire site, with keys of your choosing.
For example, you can create an index.json file with keys such as title, tags, permalink, date, and contents.
You can then use JavaScript to download this index.json file, parse it into a JavaScript object and then search through it (this is also how the Fuse implementation works).

To tell Hugo to create a index.json of your site, add the following to your config.toml file:

// XMLHttpRequest boilerplate from above
// ...
constindex=JSON.parse(httpRequest.responseText);constkeys=["title","permalink","tags","contents"];constpattern=newRegExp(query);// See below for how we get the query
constresults=index.filter(item=>{for(vari=0;i<keys.length;i++){constkey=keys[i];if(!item[key]){continue;}if(key==="tags"){if(item[key].some(tag=>pattern.test(tag.toLowerCase()))){returntrue;}}elseif(pattern.test(item[key].toLowerCase())){returntrue;}}returnfalse;});// ...

The items in results are your search results.
Now it’s just a matter of adding those results to the DOM to present them to your user.

I added a form to the navigation banner on this site with a single input field that redirects users to gpanders.com/search/ with a single query param q.
So a search URL looks like gpanders.com/search/?q=query_phrase.
To get the search query from the request URL, I simply use:

We capture the query parameter from the URI and decode it.
We then force query to be lower case to make searches case-insensitive and also escape characters that are semantically meaningful in regular expressions.
This ensures that the search query entered by the user is interpreted literally and not as a regular expression.
If you want search queries to use regular expression syntax, then simply omit the .replace() call above.
pattern is then used in the XMLHttpRequest callback we wrote above.

Save this JavaScript file to assets/js/search.js.
It needs to be under the assets directory so that we can access it using Hugo’s resources API.

To create the actual search page in Hugo, create the file content/search/index.md with the simple contents:

---
layout: "search"
---

Then create the layout file for this page at layouts/_default/search.html.
For example,

The post above is not meant to be a finely detailed, step-by-step guide, but rather a high-level overview of how I did my implementation.
There are a lot of gaps you’ll have to fill in for yourself, but of course, that’s the fun part!
Feel free to contact me with any questions or feedback.
And of course you can always find my full implementation here on this site.