Skip to content

Commit

Permalink
fix(options): remove persistHierarchicalRootCount option
Browse files Browse the repository at this point in the history
It behaves as persistHierarchicalRootCount: true
  • Loading branch information
Haroenv committed Dec 18, 2024
1 parent 34cca9a commit 5721ac8
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 312 deletions.
1 change: 0 additions & 1 deletion packages/algoliasearch-helper/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,6 @@ declare namespace algoliasearchHelper {
* This is for internal use, e.g., avoiding caching in infinite hits, or delaying the display of these results.
*/
__isArtificial?: boolean | undefined;
persistHierarchicalRootCount?: boolean;
}

type ISearchResponse<T> = Omit<SearchResponse<T>, 'facets' | 'params'> &
Expand Down
42 changes: 6 additions & 36 deletions packages/algoliasearch-helper/src/SearchResults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,8 @@ function SearchResults(state, results, options) {
});

// Make every key of the result options reachable from the instance
var opts = defaultsPure(options, {
persistHierarchicalRootCount: true,
});
Object.keys(opts).forEach(function (key) {
self[key] = opts[key];
Object.keys(options || {}).forEach(function (key) {
self[key] = options[key];
});

/**
Expand Down Expand Up @@ -508,16 +505,10 @@ function SearchResults(state, results, options) {
return;
}

self.hierarchicalFacets[position][attributeIndex].data =
self.persistHierarchicalRootCount
? defaultsPure(
self.hierarchicalFacets[position][attributeIndex].data,
facetResults
)
: defaultsPure(
facetResults,
self.hierarchicalFacets[position][attributeIndex].data
);
self.hierarchicalFacets[position][attributeIndex].data = defaultsPure(
self.hierarchicalFacets[position][attributeIndex].data,
facetResults
);
} else {
position = disjunctiveFacetsIndices[dfacet];

Expand Down Expand Up @@ -589,28 +580,7 @@ function SearchResults(state, results, options) {
return;
}

// when we always get root levels, if the hits refinement is `beers > IPA` (count: 5),
// then the disjunctive values will be `beers` (count: 100),
// but we do not want to display
// | beers (100)
// > IPA (5)
// We want
// | beers (5)
// > IPA (5)
// @MAJOR: remove this legacy behaviour in next major version
var defaultData = {};

if (
currentRefinement.length > 0 &&
!self.persistHierarchicalRootCount
) {
var root = currentRefinement[0].split(separator)[0];
defaultData[root] =
self.hierarchicalFacets[position][attributeIndex].data[root];
}

self.hierarchicalFacets[position][attributeIndex].data = defaultsPure(
defaultData,
facetResults,
self.hierarchicalFacets[position][attributeIndex].data
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ function getData() {
index: 'test_hotels-node',
hitsPerPage: 20,
nbHits: 4,
persistHierarchicalRootCount: true,
nbPages: 1,
page: 0,
params:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,12 +751,10 @@ test('getFacetValues(facetName) prefers the "main" facet result (disjunctive)',
expect(facetValues).toEqual(expected);
});

test('getFacetValues(facetName) prefers the "main" facet result (hierarchical) (persistHierarchicalRootCount: true)', function () {
test('getFacetValues(facetName) prefers the "main" facet result (hierarchical)', function () {
var data = require('./getFacetValues/hierarchical-non-exhaustive.json');
var searchParams = new SearchParameters(data.state);
var result = new SearchResults(searchParams, data.content.results, {
persistHierarchicalRootCount: true,
});
var result = new SearchResults(searchParams, data.content.results);

var facetValues = result.getFacetValues('brand').data;

Expand Down Expand Up @@ -792,45 +790,3 @@ test('getFacetValues(facetName) prefers the "main" facet result (hierarchical) (

expect(facetValues).toEqual(expected);
});

test('getFacetValues(facetName) prefers the "main" facet result (hierarchical) (persistHierarchicalRootCount: false)', function () {
var data = require('./getFacetValues/hierarchical-non-exhaustive.json');
var searchParams = new SearchParameters(data.state);
var result = new SearchResults(searchParams, data.content.results, {
persistHierarchicalRootCount: false,
});

var facetValues = result.getFacetValues('brand').data;

var expected = [
{
count: 50,
data: null,
isRefined: true,
name: 'Apple',
escapedValue: 'Apple',
path: 'Apple',
exhaustive: true,
},
{
count: 551,
data: null,
isRefined: false,
name: 'Insignia™',
escapedValue: 'Insignia™',
path: 'Insignia™',
exhaustive: true,
},
{
count: 551,
data: null,
isRefined: false,
name: 'Samsung',
escapedValue: 'Samsung',
path: 'Samsung',
exhaustive: true,
},
];

expect(facetValues).toEqual(expected);
});
Original file line number Diff line number Diff line change
Expand Up @@ -86,196 +86,20 @@ describe('hierarchical facets: simple usage', function () {
client.search.mockClear();
});

test('persistHierarchicalRootCount: false', function (done) {
var helper = algoliasearchHelper(
client,
indexName,
{
hierarchicalFacets: [
{
name: 'categories',
attributes: [
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
'categories.lvl3',
],
},
],
},
{
persistHierarchicalRootCount: false,
}
);

helper.toggleFacetRefinement('categories', 'beers > IPA > Flying dog');

var expectedHelperResponse = [
{
name: 'categories',
count: null,
isRefined: true,
path: null,
escapedValue: null,
exhaustive: true,
data: [
{
name: 'beers',
path: 'beers',
escapedValue: 'beers',
count: 9,
isRefined: true,
exhaustive: true,
data: [
{
name: 'IPA',
path: 'beers > IPA',
escapedValue: 'beers > IPA',
count: 9,
isRefined: true,
exhaustive: true,
data: [
{
name: 'Flying dog',
path: 'beers > IPA > Flying dog',
escapedValue: 'beers > IPA > Flying dog',
count: 3,
isRefined: true,
exhaustive: true,
data: null,
},
{
name: 'Brewdog punk IPA',
path: 'beers > IPA > Brewdog punk IPA',
escapedValue: 'beers > IPA > Brewdog punk IPA',
count: 6,
isRefined: false,
exhaustive: true,
data: null,
},
],
},
{
name: 'Pale Ale',
path: 'beers > Pale Ale',
escapedValue: 'beers > Pale Ale',
count: 10,
isRefined: false,
exhaustive: true,
data: null,
},
{
name: 'Stout',
path: 'beers > Stout',
escapedValue: 'beers > Stout',
count: 1,
isRefined: false,
exhaustive: true,
data: null,
},
],
},
{
name: 'fruits',
path: 'fruits',
escapedValue: 'fruits',
count: 5,
isRefined: false,
exhaustive: true,
data: null,
},
{
name: 'sales',
path: 'sales',
escapedValue: 'sales',
count: 20,
isRefined: false,
exhaustive: true,
data: null,
},
],
},
];

helper.setQuery('a').search();

helper.once('result', function (event) {
var queries = client.search.mock.calls[0][0];
var hitsQuery = queries[0];
var parentValuesQuery = queries[1];
var fullParentsValuesQueries = queries.slice(2);

expect(queries.length).toBe(4);

expect(hitsQuery.params.facets).toEqual([
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
'categories.lvl3',
]);
expect(hitsQuery.params.facetFilters).toEqual([
['categories.lvl2:beers > IPA > Flying dog'],
]);

expect(parentValuesQuery.params.facets).toEqual([
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
]);
expect(parentValuesQuery.params.facetFilters).toEqual([
['categories.lvl1:beers > IPA'],
]);

// Root
expect(fullParentsValuesQueries[0].params.facets).toEqual(
'categories.lvl0'
);
expect(fullParentsValuesQueries[0].params.facetFilters).toBe(undefined);

// Level 1
expect(fullParentsValuesQueries[1].params.facets).toEqual(
'categories.lvl1'
);
expect(fullParentsValuesQueries[1].params.facetFilters).toEqual([
'categories.lvl0:beers',
]);

expect(event.results.hierarchicalFacets).toEqual(expectedHelperResponse);
expect(
event.results.hierarchicalFacets.find((f) => f.name === 'categories')
).toEqual(expectedHelperResponse[0]);

// we do not yet support multiple values for hierarchicalFacetsRefinements
// but at some point we may want to open multiple leafs of a hierarchical menu
// So we set this as an array so that we do not have to bump major to handle it
expect(
Array.isArray(helper.state.hierarchicalFacetsRefinements.categories)
).toBeTruthy();
done();
test('simple usage', function (done) {
var helper = algoliasearchHelper(client, indexName, {
hierarchicalFacets: [
{
name: 'categories',
attributes: [
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
'categories.lvl3',
],
},
],
});
});

test('persistHierarchicalRootCount: true', function (done) {
var helper = algoliasearchHelper(
client,
indexName,
{
hierarchicalFacets: [
{
name: 'categories',
attributes: [
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
'categories.lvl3',
],
},
],
},
{
persistHierarchicalRootCount: true,
}
);

helper.toggleFacetRefinement('categories', 'beers > IPA > Flying dog');

Expand Down
25 changes: 1 addition & 24 deletions packages/instantsearch-core/src/__tests__/instantsearch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -864,30 +864,7 @@ describe('start', () => {
expect(algoliasearchHelper).toHaveBeenCalledWith(
searchClient,
indexName,
undefined,
{ persistHierarchicalRootCount: true }
);
});

it('creates a Helper with `persistHierarchicalRootCount` set to true when specified with a future flag', () => {
const searchClient = createSearchClient();
const indexName = 'indexName';
const future = {
persistHierarchicalRootCount: true,
};
const search = new InstantSearch({
indexName,
searchClient,
future,
});

search.start();

expect(algoliasearchHelper).toHaveBeenCalledWith(
searchClient,
indexName,
undefined,
{ persistHierarchicalRootCount: true }
undefined
);
});

Expand Down
Loading

0 comments on commit 5721ac8

Please sign in to comment.