facebook / docusaurus

Easy to maintain open source documentation websites.
https://docusaurus.io
MIT License
54.16k stars 8.11k forks source link

Algolia Contextual Search Generates Incorrect FaceFilters #10050

Closed karl-cardenas-coding closed 2 months ago

karl-cardenas-coding commented 2 months ago

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

When experimenting with the Algolia Crawler, I noticed that when contextual search is enabled no results are returned. The API requests are successful (status code 200), but no hits are returned.

I stumbled upon this Aloglia support thread trying to understand what might be causing the issue.

Upon closer investigation I noticed how the facetFilter parameter generated by Docusarus is incorrect compared to what Algolia expects.

The Crawler’s search UI includes the following values in the searchFacets query when I enable some custom facets such as as docusaurus_tag , version, and language.

[["docusaurus_tag:docs-default-4.3.x"],["version:4.3.x"],["lang:en"]]"}]}

This returns results as expected.

Now, in Docusarus, when contextual search in enabled, here is what is provided in the API request.

 ["language:en",["docusaurus_tag:default","docusaurus_tag:docs-default-current","docusaurus_tag:docs-api-current"]]"}]}

The list value is not generated correctly.

If I re-create the API post request with this vale for the faceFilter, I get the expected results.

facetFilters=[["docusaurus_tag:docs-default-4.3.x"],["version:4.3.x"],["lang:en"]]"}]}

Reproducible demo

No response

Steps to reproduce

You can use this codebox URL.

But or spin up a quick Docusarus project and use my Algolia config or the one included in the Algolia issue.

Expected behavior

The expected behavior is to return the expected results using the facefilter Docusarus includes in the post request.

Actual behavior

No results are returned.

Your environment

Self-service

karl-cardenas-coding commented 2 months ago

We can close this ticket. I figured out the issue. Check your index and make sure language is an exposed facet.

CleanShot 2024-04-16 at 17 25 54

I'm not sure if the default Algolia crawler config is what's causing the issue but lang is not gonna help you. You need to make sure the face language is an attribute available on you index.

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

I hope this saves other some time 😅

slorber commented 2 months ago

Yes, you need to make sure your index match our official recommendations, otherwise it's your responsibility to make things work

The recommended config contains that "language" facetting field

https://docsearch.algolia.com/docs/templates/#docusaurus-v3-template

slorber commented 2 months ago

If that helps anyone stumbling on this issue, I posted this answer in a few Algolia issues, hope it helps.

https://github.com/facebook/docusaurus/issues/6693#issuecomment-2039757018

No search result?

For anyone passing by, if you don't get any Algolia search results:

  • make sure that your Algolia index has the fields in the screenshot below
  • If you don't see these fields, then you have an index config problem
  • You should check your crawler config, make sure it matches the recommended one, and then delete/recreate your index based on the newly updated/fixed crawler config (Algolia team recommendation)

image

See also: #10007 (comment)

karl-cardenas-coding commented 2 months ago

@slorber would you be open to a PR that adds this tiny bit of information to the https://docusaurus.io/docs/search#contextual-search page? It could be a tiny lil info box and that also links to the issue #6693

Happy to author it.

slorber commented 2 months ago

@karl-cardenas-coding thanks for your proposal, but it was already on my todo list to add this index troubleshooting section so I just did it (https://github.com/facebook/docusaurus/pull/10056).

We get many support requests about this over time so it's important to explain this thing properly so I preferred to do it myself.

karl-cardenas-coding commented 2 months ago

No worries 😊 @slorber and thank you for adding the updates. That's gonna help a lot for future users.