Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
fix(facetOrdering): hierarchical attributes sort by path
Browse files Browse the repository at this point in the history
this is because the path is the real value, and thus what people will use in the ordering
  • Loading branch information
Haroenv committed Jun 14, 2021
1 parent 22354e3 commit c1d9764
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ declare namespace algoliasearchHelper {
getFacetValues(
attribute: string,
opts: {
sortBy: string[],
sortBy: any;
facetOrdering: boolean;
}
): SearchResults.FacetValue[] | SearchResults.HierarchicalFacet | undefined;
Expand Down
8 changes: 5 additions & 3 deletions src/SearchResults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,10 @@ function sortViaFacetOrdering(facetValues, facetOrdering) {
}, {});

facetValues.forEach(function(item) {
if (reverseOrder[item.name] !== undefined) {
orderedFacets[reverseOrder[item.name]] = item;
// hierarchical facets get sorted using their raw name
var name = item.path || item.name;
if (reverseOrder[name] !== undefined) {
orderedFacets[reverseOrder[name]] = item;
} else {
remainingFacets.push(item);
}
Expand All @@ -731,7 +733,7 @@ function sortViaFacetOrdering(facetValues, facetOrdering) {
if (sortRemainingBy === 'hidden') {
return orderedFacets;
} else if (sortRemainingBy === 'alpha') {
ordering = [['name'], ['asc']];
ordering = [['path', 'name'], ['asc', 'asc']];
} else {
ordering = [['count'], ['desc']];
}
Expand Down
58 changes: 53 additions & 5 deletions test/spec/SearchResults/getFacetValues-facetOrdering.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ describe('hierarchical facet', function() {
isRefined: false,
exhaustive: true,
data: null
},
{
name: 'Useless Gift Cards',
path: 'Best Buy Gift Cards > Useless Gift Cards',
count: 12,
isRefined: false,
exhaustive: true,
data: null
}
]
},
Expand Down Expand Up @@ -374,6 +382,14 @@ describe('hierarchical facet', function() {
isRefined: false,
exhaustive: true,
data: null
},
{
name: 'Useless Gift Cards',
path: 'Best Buy Gift Cards > Useless Gift Cards',
count: 12,
isRefined: false,
exhaustive: true,
data: null
}
]
},
Expand Down Expand Up @@ -466,6 +482,14 @@ describe('hierarchical facet', function() {
isRefined: false,
exhaustive: true,
data: null
},
{
name: 'Useless Gift Cards',
path: 'Best Buy Gift Cards > Useless Gift Cards',
count: 12,
isRefined: false,
exhaustive: true,
data: null
}
]
},
Expand Down Expand Up @@ -558,6 +582,14 @@ describe('hierarchical facet', function() {
isRefined: false,
exhaustive: true,
data: null
},
{
name: 'Useless Gift Cards',
path: 'Best Buy Gift Cards > Useless Gift Cards',
count: 12,
isRefined: false,
exhaustive: true,
data: null
}
]
},
Expand Down Expand Up @@ -658,6 +690,14 @@ describe('hierarchical facet', function() {
isRefined: false,
exhaustive: true,
data: null
},
{
name: 'Useless Gift Cards',
path: 'Best Buy Gift Cards > Useless Gift Cards',
count: 12,
isRefined: false,
exhaustive: true,
data: null
}
]
}
Expand All @@ -678,7 +718,7 @@ describe('hierarchical facet', function() {
sortRemainingBy: 'hidden'
},
'hierarchicalCategories.lvl1': {
order: ['Swag Gift Cards'],
order: ['Best Buy Gift Cards > Entertainment Gift Cards'],
sortRemainingBy: 'count'
}
}
Expand Down Expand Up @@ -707,6 +747,14 @@ describe('hierarchical facet', function() {
isRefined: true,
exhaustive: true,
data: [
{
name: 'Entertainment Gift Cards',
path: 'Best Buy Gift Cards > Entertainment Gift Cards',
count: 17,
isRefined: true,
exhaustive: true,
data: null
},
{
name: 'Swag Gift Cards',
path: 'Best Buy Gift Cards > Swag Gift Cards',
Expand All @@ -716,10 +764,10 @@ describe('hierarchical facet', function() {
data: null
},
{
name: 'Entertainment Gift Cards',
path: 'Best Buy Gift Cards > Entertainment Gift Cards',
count: 17,
isRefined: true,
name: 'Useless Gift Cards',
path: 'Best Buy Gift Cards > Useless Gift Cards',
count: 12,
isRefined: false,
exhaustive: true,
data: null
}
Expand Down
3 changes: 2 additions & 1 deletion test/spec/SearchResults/getFacetValues/hierarchical.json
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,9 @@
"Best Buy Gift Cards": 80
},
"hierarchicalCategories.lvl1": {
"Best Buy Gift Cards > Swag Gift Cards": 20,
"Best Buy Gift Cards > Entertainment Gift Cards": 17,
"Best Buy Gift Cards > Swag Gift Cards": 20
"Best Buy Gift Cards > Useless Gift Cards": 12
}
},
"exhaustiveFacetsCount": true,
Expand Down

0 comments on commit c1d9764

Please sign in to comment.