Adding search to a Hugo blog
First thing to do after a static blog is set up isn’t just filling it with posts but taking care of how it gets discovered. Hugo doesn’t provide search out of the box but lists several options1. One of them is hugo-elasticsearch2 looks promising but it requires a running Elasticsearch instance which is a bit of an overkill.
hugo-lunr3 is a nice client side search solution, the downside is the routine of rebuilding index manually on every post or edit yet I was concerned about how it performs over time when the index grows to contain megabytes of data. Moreover, mentioned repositories seem to be abandoned: they haven’t seen a commit in 3 to 6 years.
Feels like the best solution possible is to rely on good old server side search engines. It actually kills two birds with one stone providing organic search discoverability and the site specific full-text search and requires no maintenance. That’s what I thought…
Being a devout user of DuckDuckGo for years my immediate solution was to generate an HTML form taking user input and a DDG specific
sites parameter pointing to my domain
gdmka.me to set the search context. Due to minimalist nature of the theme a search button felt redundant.
<form class="search" method="GET" id="search" action="https://duckduckgo.com/"> <input type="hidden" name="sites" value="gdmka.me"/> <input class="searchTerm" type="text" name="q" placeholder="Search"/> <input type="submit" value="Search" style="visibility: hidden;"/> </form>
Clearly DuckDuckGo cannot return any results for a domain registered three days ago (at the time of writing). Because it doesn’t know about it yet and primarily relies on Microsoft Bing index4.
To speed up the process I registered with Bing Webmaster Tools5 and submitted a couple of URLs for indexing. Much to my surprise it took about three hours for the index to start appearing on Bing. I was expecting that DuckDuckGo will be catching up in a couple of days.
Then the next morning I navigated myself back to Bing and found out that my blog is in fact removed.
This is what Bing Webmaster Tools was showing me. Clueless I send an email to support and the response is blazing fast but doesn’t provide any specifics aside from the blog being too fresh. According to tech support it will take a couple of weeks to populate the index properly, rendering the DuckDuckGo search box ineffective for an even longer period.
Google expressed similar confusing behavior with manually submitted URLs at Google Search Console6. Indexing went quickly and was gone in a 24 hour timespan without any elaborate hints or reasons. But hey, on the upside, site specific search seems to be working. Eventually, I ended up with this Google search form.
<form class="search" method="GET" id="search" action="https://google.com/search"> <input type="hidden" name="q" value="site:gdmka.me"/> <input class="searchTerm" type="text" name="q" placeholder="Search"/> <input type="submit" value="Search" style="visibility: hidden;"/> </form>
It may take some time and effort by submitting fresh URLs manually and triggering sitemap re-scans to push your blog for an up to date search quality.