Skip to content

Commit

Permalink
fix(cache): support nested hierarchical refinements (#6283)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-giraudet authored Jul 12, 2024
1 parent 6022197 commit 8a0ae01
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
7 changes: 3 additions & 4 deletions packages/algoliasearch-helper/src/requestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ var requestBuilder = {
params.facetFilters = filteredFacetFilters.concat(
parent.attribute + ':' + parent.value
);
} else if (filteredFacetFilters.length > 0) {
params.facetFilters = filteredFacetFilters;
} else {
params.facetFilters =
filteredFacetFilters.length > 0
? filteredFacetFilters
: undefined;
delete params.facetFilters;
}

queries.push({ indexName: index, params: params });
Expand Down
76 changes: 75 additions & 1 deletion packages/instantsearch.js/src/lib/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
createSearchClient,
} from '@instantsearch/mocks';

import { connectConfigure, connectSearchBox } from '../../connectors';
import {
connectConfigure,
connectHierarchicalMenu,
connectSearchBox,
} from '../../connectors';
import instantsearch from '../../index.es';
import { index } from '../../widgets';
import { getInitialResults, waitForResults } from '../server';
Expand Down Expand Up @@ -347,4 +351,74 @@ describe('getInitialResults', () => {
expectedInitialResults
);
});

test('returns correct requestParams with nested hierarchical facets', async () => {
const search = instantsearch({
indexName: 'indexName',
searchClient: createSearchClient(),
initialUiState: {
indexName: {
hierarchicalMenu: {
'hierarchicalCategories.lvl0': ['Appliances', 'Fans'],
},
},
},
});

search.addWidgets([
connectHierarchicalMenu(() => {})({
attributes: [
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
],
}),
]);

search.start();

const requestParams = await waitForResults(search);

expect(requestParams).toMatchInlineSnapshot(`
[
{
"facetFilters": [
[
"hierarchicalCategories.lvl1:Appliances > Fans",
],
],
"facets": [
"hierarchicalCategories.lvl0",
"hierarchicalCategories.lvl1",
],
"maxValuesPerFacet": 10,
},
{
"analytics": false,
"clickAnalytics": false,
"facetFilters": [
[
"hierarchicalCategories.lvl0:Appliances",
],
],
"facets": [
"hierarchicalCategories.lvl0",
"hierarchicalCategories.lvl1",
],
"hitsPerPage": 0,
"maxValuesPerFacet": 10,
"page": 0,
},
{
"analytics": false,
"clickAnalytics": false,
"facets": [
"hierarchicalCategories.lvl0",
],
"hitsPerPage": 0,
"maxValuesPerFacet": 10,
"page": 0,
},
]
`);
});
});

0 comments on commit 8a0ae01

Please sign in to comment.