Chapter 6
Adding Search to Your Site

Once you’ve created a large amount of content on your site, you’ll want to make it easier for people to find things. When your site is live for all the world to see, you’ll want to make sure everyone can find your content.

You’ll also want a local search engine for your site, especially for situations where you have a significantly large corpus of content.

Database-driven sites like WordPress have built-in search capabilities. When a visitor seearches for content, their keywords are used to build a query that gets sent to the database containing the content. The database returns results that match, and the system displays the page.

For static sites like Hugo, you’re working without a database, so you’re going to have to think about solving the problem differently. You can use a third-party search service like Algolia,[29] which indexes your content and lets you search it. You can set up your own search indexer like ElasticSearch[30] and integrate it into your site. Or you can generate your own search index and use client-side JavaScript to perform the search.

In this chapter, we’ll take the last approach. You’ll build a client-side search engine using a JavaScript library called Lunr[31] to do the heavy lifting. Lunr needs a list of your site’s content in JSON format, so you’ll leverage Hugo’s ability to generate JSON to create that list.