Search Index .json-file for Lunr.js

Kinda new here and I know this discussion is a few months old, but thought I’d mention I recently went through setting this up on my site. I wrote up walk-through here: https://www.mattwalters.net/posts/hugo-and-lunr/ (I have very little on my site but you’ll get search results if you search for things like “hugo” or “spacex”)

After reading over this discussion I might should make a few changes to my setup (particularly the parts talking about using alternate output formats)

My implementation is still evolving and has progressed some from that walk-through. For instance, I pre-build the actual Lunr index (not just the JSON of content) during the build process instead of having Lunr build it on the page load. This means I don’t have to call lunr.add() on the individual documents that are in the JSON, that step is done as part of the build process rather than in the browser which speeds getting the search results page ready to show results.

@Rick if you look at the search.js that actually running on my site (instead of the one in the walk-through) you can see my solution for letting the user enter a search phrase on one page but perform the search on another page. Essentially the search form uses the GET method to submit to the search results page and when the search results page loads, it checks to see if the query parameter for their search phrase is populated. If it is, then it performs a search using that phrase.

I’m hoping to put up a new post when my efforts settle some more to show how I pre-build the index, etc. But if someone is really interested and wants to look at the raw code without much explanation, then let me know and I can throw up a gist or something for the relevant parts. It does require a slightly more complex build process because you have to use node to execute some custom JS that does the index building but I was able to get it working with netlify’s CI/CD pretty easily.