Algolia searchbar is not working with docusaurus v2

Hello everyone ,
I am using algolia searchbar with docusaurus v2.
I have added my data using docsearch-scraper in the index of algolia and also i have done the changes in docusaurus.config.js as per docs which you can see here:

algolia: {
        apiKey: "7bcdfd213bdb051b9614ce1083b698f2",
        indexName: "cybernotes",
        appId: "My app id",
        // contextualSearch: true,
        placeholder: "search",
      }

And this is my index

But whenever i search something in searchbar it shows nothing

Can anyone help me with this problem?
If you need more info then tell me :slight_smile:

1 Like

I ran into this same problem where I’m building my own index and after an evening of searching around I finally discovered what was going on. The short answer is set contextualSearch: false in your config above.

Longer answer:
contextualSearch is default true, which is why this is popping up. For reference in the Docusaurus docs for Search it says

Let’s consider you have 2 docs versions ( v1 and v2 ) and 2 languages ( en and fr ).
When browsing v2 docs, it would be odd to return search results for the v1 documentation. Sometimes v1 and v2 docs are quite similar, and you would end up with duplicate search results for the same query (one result per version).
Similarly, when browsing the French site, it would be odd to return search results for the English docs.
To solve this problem, the contextual search feature understands that you are browsing a specific docs version and language, and will create the search query filters dynamically.

While this does indeed sound useful, the default settings append a bunch of query parameters to the request. I had copied the query request out of the browser and put it into Postman to start understanding what was causing the issue. At the very end of the request was

facetFilters=%5B%22language%3Aen%22%2C%5B%22docusaurus_tag%3Adefault%22%2C%22docusaurus_tag%3Adocs-default-current%22%5D%5D

These facets come from Config templates | DocSearch by Algolia the template so I’m guessing Docusuarus made the distinction that by default they expect everyone to use their template. If you want to turn contextualSearch back on, it looks like you need to add the facets

      attributesForFaceting: [
        'type',
        'lang',
        'language',
        'version',
        'docusaurus_tag',
      ],

If you look in the config docs Config Files | DocSearch by Algolia it looks like you can add these facets like so

{
  "start_urls": [
    {
      "url": "http://www.example.com/docs/(?P<lang>.*?)/(?P<version>.*?)/",
      "variables": {
        "lang": ["en", "fr"],
        "version": ["latest", "3.3", "3.2"]
      }
    }
  ]
}

Though I tried doing that and adjusting to what was needed to no avail. I started searching through issues on Docusaurus and came across Search page always returns blank, since upgrading from alpha 66 to alpha 72 · Issue #4644 · facebook/docusaurus · GitHub which specifically had an answer Search page always returns blank, since upgrading from alpha 66 to alpha 72 · Issue #4644 · facebook/docusaurus · GitHub which indicated you can add custom_settings to your config.json like so

  "custom_settings": {
    "attributesForFaceting": [
      "language",
      "version",
      "type",
      "docusaurus_tag"
    ]
  }

Those attributes need to match up with what is in the <head> on the site you’re indexing. I think everything is there by default, mine had

<meta data-rh="true" name="docsearch:language" content="en">
<meta data-rh="true" name="docsearch:version" content="current">
<meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current">

After re-indexing with the custom_settings and setting contextualSearch: true (or removing it so it defaulted back to true) the search works.

Hopefully this question ranks high in Google and the next person who comes across this issue lands here :slight_smile:

1 Like