Dmytro Chepurnyi
06/30/2022, 1:18 PM'typesense-instantsearch-adapter'
and algolia components. Everything looks neat and fast to implement. But I’m facing issues when it comes to implementing mixed AND+OR filtering for the same attribute. We have attribute in schema variant_options.id
which corresponds to different type of product variant types (can be color, storage size etc.) - and displayed as checkboxes grouped by different type.
1. Initial approach was to use const { items, refine } = useRefinementList(...)
from 'react-instantsearch-hooks-web'
. Works just fine - I select one option - and multi_search endpoint was called with 4 searches in payload and returns proper amount of facets with correct amount of facets (20 items in my case for that filters `filter_by: "en.categories.lvl1:=[Phones > iPhone
] && variant_options.id:=[41]"`). This works good but allows to filter only by using OR across all variant options.
2. Later I tried to implement AND+OR filtering using useConfiguration
hook from 'react-instantsearch-hooks-web'
and update facetFilters
in configuration when user select some option. To display list of available filters I still use const { items } = useRefinementList(...)
. Difference here is how I “refine” option, without using refine
function from refinement list. So with that tweaks I was able to construct correct search request with something like that
filter_by: "en.categories.lvl1:=[`Phones > iPhone`] && variant_options.id:=[41,42] && variant_options.id:=[55,56]"
Logic works fine, I get correct amount of hits in results and correct counts for returned facets. But the problem is that every time I select some option, items.length
from ``useRefinementList`` is changed. For the same search ``filter_by: "en.categories.lvl1:=[Phones > iPhone
] && variant_options.id:=[41]"`` I get 10 facets, compare to 20 when using approach 1. Also multi_search endpoint was called with 3 searches in payload.
So at this stage it becomes not possible to see facet counts for other options that are not returned in results. For example I select “Black” color and wanted to add “Gold” as next step. But after first search refinemets list updated and I’m not getting “Gold” color in response anymore, only some other colors. But it is still should be possible to add “Gold” to search. Because it’s a valid filter, and definetely has results.
As a workaround I can cache refinement options from previous response, but then facet counts numbers become incorrect.
My question - Is it possible to get consistent amount of facets in results for case when AND + OR filtering is for same attribute? If it’s possible, what is a better approach to implement this? Maybe without using algolia components at all…We can change schema, but there is endless amount of variants. For now it looks like changing schema and reindexing every time when new variant type is added is more expensive solution. I believe there should be some way to get correct results.
Thanks in advance for answers. I can provide more information if needed, just decided to start from brief description of issue.Jason Bosco
06/30/2022, 3:32 PMreact-instantsearch-hooks-web
, but could you try using the equivalent of the limit
parameter in refinementList:
https://www.algolia.com/doc/api-reference/widgets/refinement-list/react-hooks/#widget-param-limitJason Bosco
06/30/2022, 3:32 PMDmytro Chepurnyi
06/30/2022, 4:01 PMlimit
parameter with useRefinementList
. In my case it 200, so Im sure its more than number of facets count. But it doesn’t work for approach2. Looks like it just ignoredDmytro Chepurnyi
06/30/2022, 4:05 PMfacet_by: "variant_options.id,en.categories.lvl0,en.categories.lvl1"
filter_by: "variant_options.id:=[41] && en.categories.lvl1:=[`Phones > iPhone`]
max_facet_values: 200
Jason Bosco
06/30/2022, 10:43 PMJason Bosco
06/30/2022, 10:43 PMDmytro Chepurnyi
07/01/2022, 7:01 AM