- Issue created by @andyg5000
- Status changed to Needs review
about 1 year ago 6:31pm 25 August 2023 - 🇺🇸United States andyg5000 North Carolina, USA
Disjunctive facets in Algolia allow you to retrieve facet value counts as if no other facets were selected, enabling users to see all available options even after making some facet selections.
I've discovered that disjunctiveFacets isn't supported by the API or the PHP library and that we have to handle these on our own if we want to use them. I also discovered that the Algolia dashboard search interface uses it's own disjunctiveFacets logic by making multiple requests to the API and merging them together (see screenshot for example). So we must make 1 query for each selected facet type to resolve the disjunctive facets.
In addition to that, the facets module invokes the `SearchApiAlgoliaBackend::search()` method and then the views (or other search_api display plugin) invokes the exact same method for the same results. This means if you have 2 facets selected, you're hitting the Algolia API 6 times. 1 for each disjunctiveFacet, and 1 for the actual search query, the double that.
I've written a patch that resolves the disjunctiveFacets issue and uses the `Drupal\search_api\Utility\QueryHelperInterface` cache utilities to prevent duplicate queries that we already have responses for.
disjunctiveFacets Fix:
* Loop over each disjunctiveFacet and make a separate Algolia API query with all the same search query parameters, but excluding the current disjunctiveFacet.
* Merge the disjunctiveFacet results with the facets returned from the results query.Multiple requests fix:
* After a search_api query is executed, the QueryHelperInterface is automatically populated as a cache.
* Simply returning the results from QueryHelperInterface::getResults() doesn't work, so I had to write `populateResultsFromCache` to hydrate the results object from the cacheSuggested improvements:
* It looks like we may be able to do all the disjunctiveFacet and search queries in 1 http request to Algolia. I haven't tested this, but the screenshot attached appears to be doing that.With this patch, my facets and results in search_api mirror what I see in the Algolia dashboard.
- 🇺🇸United States andyg5000 North Carolina, USA
I've since realized that my view was configured with a max-age of 0 even with search api tag caching enabled. Not sure how that happened.
Anyway, this has simplified the patch quite a bit.
The patch now just includes
disjunctiveFacets Fix:
* Loop over each disjunctiveFacet and make a separate Algolia API query with all the same search query parameters, but excluding the current disjunctiveFacet.
* Merge the disjunctiveFacet results with the facets returned from the results query. - 🇮🇳India yogen.prasad
comment no: #3 is working in my system.
Can move to RTBC