Skip to content

Commit f8bbc06

Browse files
calcsammeganesu
andauthored
docs: edit search documentation (#28737)
* 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 <[email protected]>
1 parent 004acf0 commit f8bbc06

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

docs/docs/how-to/adding-common-features/adding-search.md

+21-38
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,49 @@
22
title: Adding Search
33
---
44

5-
See below for a list of guides in this section, or keep reading for an overview on adding search functionality to your site.
6-
7-
<GuideList slug={props.slug} />
8-
9-
## Site search overview
10-
11-
Before going through the steps for adding search to your Gatsby website, examine the components needed for adding search to a website.
12-
13-
There are three required components for adding search to your Gatsby website:
14-
15-
1. index
16-
2. engine
17-
3. UI
5+
There are three required components for adding search to your Gatsby website: the **search index**, the **search engine**, and **search UI**.
186

197
## Site search components
208

21-
### Search index
22-
23-
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.
24-
25-
### Search engine
26-
27-
The search engine takes a search query, runs it through the search index, and returns any matching documents.
28-
29-
### Search UI
30-
31-
The UI component provides an interface to the user, which allows them to write search queries and view the results of each query.
9+
| Search Component | Description |
10+
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
11+
| **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. |
12+
| **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) |
13+
| **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. | |
3214

3315
## Adding search to your site
3416

35-
Now that you know the three required components, there are a few ways to approach adding search to your Gatsby-powered site.
36-
37-
### Use an open source search engine
17+
There are a few ways to approach adding search to your Gatsby-powered site.
3818

39-
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.
19+
### Client-side search
4020

41-
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.
21+
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.
4222

43-
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).
23+
One way of doing this is to use the `js-search` library:
4424

45-
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).
25+
- [Adding Search with JS Search](/docs/adding-search-with-js-search)
4626

47-
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.
27+
There are two Gatsby plugins that support this as well:
4828

49-
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).
29+
- [gatsby-plugin-elasticlunr-search](/plugins/@gatsby-contrib/gatsby-plugin-elasticlunr-search)
30+
- [gatsby-plugin-local-search](/plugins/gatsby-plugin-local-search)
5031

5132
### Use an API-based search engine
5233

5334
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.
5435

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

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

61-
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.
42+
Of these, the most common solution is Algolia. The Gatsby docs include a guide to adding Algolia to your site:
43+
44+
- [Adding Search with Algolia](/docs/adding-search-with-algolia)
6245

63-
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).
46+
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.
6447

65-
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.
48+
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.
6649

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

0 commit comments

Comments
 (0)