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

Commit c1d9764

Browse files
committed
fix(facetOrdering): hierarchical attributes sort by path
this is because the path is the real value, and thus what people will use in the ordering
1 parent 22354e3 commit c1d9764

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ declare namespace algoliasearchHelper {
13531353
getFacetValues(
13541354
attribute: string,
13551355
opts: {
1356-
sortBy: string[],
1356+
sortBy: any;
13571357
facetOrdering: boolean;
13581358
}
13591359
): SearchResults.FacetValue[] | SearchResults.HierarchicalFacet | undefined;

src/SearchResults/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,10 @@ function sortViaFacetOrdering(facetValues, facetOrdering) {
719719
}, {});
720720

721721
facetValues.forEach(function(item) {
722-
if (reverseOrder[item.name] !== undefined) {
723-
orderedFacets[reverseOrder[item.name]] = item;
722+
// hierarchical facets get sorted using their raw name
723+
var name = item.path || item.name;
724+
if (reverseOrder[name] !== undefined) {
725+
orderedFacets[reverseOrder[name]] = item;
724726
} else {
725727
remainingFacets.push(item);
726728
}
@@ -731,7 +733,7 @@ function sortViaFacetOrdering(facetValues, facetOrdering) {
731733
if (sortRemainingBy === 'hidden') {
732734
return orderedFacets;
733735
} else if (sortRemainingBy === 'alpha') {
734-
ordering = [['name'], ['asc']];
736+
ordering = [['path', 'name'], ['asc', 'asc']];
735737
} else {
736738
ordering = [['count'], ['desc']];
737739
}

test/spec/SearchResults/getFacetValues-facetOrdering.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ describe('hierarchical facet', function() {
275275
isRefined: false,
276276
exhaustive: true,
277277
data: null
278+
},
279+
{
280+
name: 'Useless Gift Cards',
281+
path: 'Best Buy Gift Cards > Useless Gift Cards',
282+
count: 12,
283+
isRefined: false,
284+
exhaustive: true,
285+
data: null
278286
}
279287
]
280288
},
@@ -374,6 +382,14 @@ describe('hierarchical facet', function() {
374382
isRefined: false,
375383
exhaustive: true,
376384
data: null
385+
},
386+
{
387+
name: 'Useless Gift Cards',
388+
path: 'Best Buy Gift Cards > Useless Gift Cards',
389+
count: 12,
390+
isRefined: false,
391+
exhaustive: true,
392+
data: null
377393
}
378394
]
379395
},
@@ -466,6 +482,14 @@ describe('hierarchical facet', function() {
466482
isRefined: false,
467483
exhaustive: true,
468484
data: null
485+
},
486+
{
487+
name: 'Useless Gift Cards',
488+
path: 'Best Buy Gift Cards > Useless Gift Cards',
489+
count: 12,
490+
isRefined: false,
491+
exhaustive: true,
492+
data: null
469493
}
470494
]
471495
},
@@ -558,6 +582,14 @@ describe('hierarchical facet', function() {
558582
isRefined: false,
559583
exhaustive: true,
560584
data: null
585+
},
586+
{
587+
name: 'Useless Gift Cards',
588+
path: 'Best Buy Gift Cards > Useless Gift Cards',
589+
count: 12,
590+
isRefined: false,
591+
exhaustive: true,
592+
data: null
561593
}
562594
]
563595
},
@@ -658,6 +690,14 @@ describe('hierarchical facet', function() {
658690
isRefined: false,
659691
exhaustive: true,
660692
data: null
693+
},
694+
{
695+
name: 'Useless Gift Cards',
696+
path: 'Best Buy Gift Cards > Useless Gift Cards',
697+
count: 12,
698+
isRefined: false,
699+
exhaustive: true,
700+
data: null
661701
}
662702
]
663703
}
@@ -678,7 +718,7 @@ describe('hierarchical facet', function() {
678718
sortRemainingBy: 'hidden'
679719
},
680720
'hierarchicalCategories.lvl1': {
681-
order: ['Swag Gift Cards'],
721+
order: ['Best Buy Gift Cards > Entertainment Gift Cards'],
682722
sortRemainingBy: 'count'
683723
}
684724
}
@@ -707,6 +747,14 @@ describe('hierarchical facet', function() {
707747
isRefined: true,
708748
exhaustive: true,
709749
data: [
750+
{
751+
name: 'Entertainment Gift Cards',
752+
path: 'Best Buy Gift Cards > Entertainment Gift Cards',
753+
count: 17,
754+
isRefined: true,
755+
exhaustive: true,
756+
data: null
757+
},
710758
{
711759
name: 'Swag Gift Cards',
712760
path: 'Best Buy Gift Cards > Swag Gift Cards',
@@ -716,10 +764,10 @@ describe('hierarchical facet', function() {
716764
data: null
717765
},
718766
{
719-
name: 'Entertainment Gift Cards',
720-
path: 'Best Buy Gift Cards > Entertainment Gift Cards',
721-
count: 17,
722-
isRefined: true,
767+
name: 'Useless Gift Cards',
768+
path: 'Best Buy Gift Cards > Useless Gift Cards',
769+
count: 12,
770+
isRefined: false,
723771
exhaustive: true,
724772
data: null
725773
}

test/spec/SearchResults/getFacetValues/hierarchical.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,9 @@
937937
"Best Buy Gift Cards": 80
938938
},
939939
"hierarchicalCategories.lvl1": {
940+
"Best Buy Gift Cards > Swag Gift Cards": 20,
940941
"Best Buy Gift Cards > Entertainment Gift Cards": 17,
941-
"Best Buy Gift Cards > Swag Gift Cards": 20
942+
"Best Buy Gift Cards > Useless Gift Cards": 12
942943
}
943944
},
944945
"exhaustiveFacetsCount": true,

0 commit comments

Comments
 (0)