Skip to content

Commit

Permalink
docs: edit search documentation (#28737)
Browse files Browse the repository at this point in the history
* Update adding-search.md

* Update adding-search.md

* Update adding-search.md

* fix: typo

* fix: replace URL with descriptive link text

* fix: lint

Co-authored-by: Megan Sullivan <megan@gatsbyjs.com>
  • Loading branch information
calcsam and meganesu committed Jan 15, 2021
1 parent 004acf0 commit f8bbc06
Showing 1 changed file with 21 additions and 38 deletions.
59 changes: 21 additions & 38 deletions docs/docs/how-to/adding-common-features/adding-search.md
Expand Up @@ -2,66 +2,49 @@
title: Adding Search
---

See below for a list of guides in this section, or keep reading for an overview on adding search functionality to your site.

<GuideList slug={props.slug} />

## Site search overview

Before going through the steps for adding search to your Gatsby website, examine the components needed for adding search to a website.

There are three required components for adding search to your Gatsby website:

1. index
2. engine
3. UI
There are three required components for adding search to your Gatsby website: the **search index**, the **search engine**, and **search UI**.

## Site search components

### Search index

The search index is a copy of your data stored in a search-friendly format. An index is for optimizing speed and performance when executing a search query. Without an index, every search would need to scan every page in your site—which quickly becomes inefficient.

### Search engine

The search engine takes a search query, runs it through the search index, and returns any matching documents.

### Search UI

The UI component provides an interface to the user, which allows them to write search queries and view the results of each query.
| Search Component | Description |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **Search index** | The search index is a copy of your data stored in a search-friendly format. An index is for optimizing speed and performance when executing a search query. Without an index, every search would need to scan every page in your site—which quickly becomes inefficient. |
| **Search engine** | The search engine indexes your content, takes a search query, runs it through theindex, and returns any matching documents. Search engines can be hosted services (like Algolia) or open-source that you can self-host (like Elastic) |
| **Search UI** | A UI component on your site that allows users to write search queries and view the results of each query. Some search providers provide out of the box React components that you can drop into Gatsby sites. | |

## Adding search to your site

Now that you know the three required components, there are a few ways to approach adding search to your Gatsby-powered site.

### Use an open source search engine
There are a few ways to approach adding search to your Gatsby-powered site.

Using an open source search engine is always free and allows you to enable offline search for your site. Note that you need to be careful with offline search because the entire search index has to be brought into the client, which can affect the bundle size significantly.
### Client-side search

Open source libraries like [`elasticlunr`](https://www.npmjs.com/package/elasticlunr), [`flexsearch`](https://github.com/nextapps-de/flexsearch) or [`js-search`](https://github.com/bvaughn/js-search) can be used to enable search for your site.
It is possible to do all the work in your Gatsby site without needing a third-party solution. This involves writing a bit of code, but using less services. With large amounts of content to index, it can also increase the bundle size significantly.

Doing so will require you to create a search index when your site is built. For [`elasticlunr`](https://www.npmjs.com/package/elasticlunr), there is a plugin called [`gatsby-plugin-elasticlunr-search`](https://github.com/gatsby-contrib/gatsby-plugin-elasticlunr-search) that creates a search index automatically. For [`flexsearch`](https://github.com/nextapps-de/flexsearch) there is a plugin called [`gatsby-plugin-flexsearch`](https://github.com/tmsss/gatsby-plugin-flexsearch).
One way of doing this is to use the `js-search` library:

For other libraries, you can use a combination of [`onCreateNode`](/docs/reference/config-files/gatsby-node/#onCreateNode), [`setFieldsOnGraphQLNodeType`](/docs/reference/config-files/gatsby-node/#setFieldsOnGraphQLNodeType) and [`sourceNodes`](/docs/reference/config-files/gatsby-node/#sourceNodes) from the Gatsby node API to create the search index and make it available in GraphQL. For more info on how to do this check out [`gatsby-plugin-elasticlunr-search`'s source code](https://github.com/gatsby-contrib/gatsby-plugin-elasticlunr-search/blob/master/src/gatsby-node.js#L96-L131).
- [Adding Search with JS Search](/docs/adding-search-with-js-search)

Another option is to generate the search index at the end of the build using the [`onPostBuild`](/docs/reference/config-files/gatsby-node/#onPostBuild) node API. This approach is used by [`gatsby-plugin-lunr`](https://github.com/humanseelabs/gatsby-plugin-lunr) to build a multilanguage index.
There are two Gatsby plugins that support this as well:

After building the search index and including it in Gatsby's data layer, you will need to allow the user to search your website. This is typically done by using a text input to capture the search query, then using one of the libraries mentioned above to retrieve the desired document(s).
- [gatsby-plugin-elasticlunr-search](/plugins/@gatsby-contrib/gatsby-plugin-elasticlunr-search)
- [gatsby-plugin-local-search](/plugins/gatsby-plugin-local-search)

### Use an API-based search engine

Another option is to use an external search engine. This solution is much more scalable as visitors to your site don't have to download your entire search index (which becomes very large as your site grows) in order to search your site. The trade-off is you'll need to pay for hosting the search engine or pay for a commercial search service.

There are many available both open source that you can host yourself and commercial hosted options.
There are many options available, including both self-hosted and commercially hosted open source:

- [ElasticSearch](https://www.elastic.co/products/elasticsearch) — OSS and has commercial hosting available
- [Solr](http://lucene.apache.org/solr/) — OSS and has commercial hosting available
- [Algolia](https://www.algolia.com/) — Commercial

If you're building a documentation website you can use [Algolia's DocSearch feature](https://community.algolia.com/docsearch/). It will automatically create a search index from the content of your pages.
Of these, the most common solution is Algolia. The Gatsby docs include a guide to adding Algolia to your site:

- [Adding Search with Algolia](/docs/adding-search-with-algolia)

If your website does not qualify as documentation, you need to collect the search index at build time and upload it using [`gatsby-plugin-algolia`](https://github.com/algolia/gatsby-plugin-algolia).
When using Algolia, they host the search index and search engine for you. Your search queries will be sent to their servers which will respond with any results. For UI components, Algolia provides a [React library](https://github.com/algolia/react-instantsearch) has helpful components.

When using Algolia, they host the search index and search engine for you. Your search queries will be sent to their servers which will respond with any results. You'll need to implement your own UI; Algolia provides a [React library](https://github.com/algolia/react-instantsearch) which may have components you'd like to use.
If you're building a documentation website you can use [Algolia's DocSearch feature](https://community.algolia.com/docsearch/). It will automatically create a search index from the content of your pages.

Elasticsearch has several React component libraries for search e.g. https://github.com/appbaseio/reactivesearch
Elasticsearch also has several React component libraries for search, such as [ReactiveSearch](https://github.com/appbaseio/reactivesearch)

0 comments on commit f8bbc06

Please sign in to comment.