Skip to content

Commit 2641a67

Browse files
authored
feat(facetOrdering): implement hidden facets in SFFV (#6648)
* feat(facetOrdering): implement hidden facets in SFFV We read the last results' facet ordering and apply hidden facets (not facet ordering) to the results array. nbHits does not need to be changed as search for facet values doesn't return that key. [CR-8645] * index match * correction * raise limit Less than a kb more, but it impacts all over
1 parent 8b9fae6 commit 2641a67

File tree

3 files changed

+185
-6
lines changed

3 files changed

+185
-6
lines changed

bundlesize.config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"files": [
33
{
44
"path": "packages/algoliasearch-helper/dist/algoliasearch.helper.js",
5-
"maxSize": "42 kB"
5+
"maxSize": "42.25 kB"
66
},
77
{
88
"path": "packages/algoliasearch-helper/dist/algoliasearch.helper.min.js",
@@ -18,19 +18,19 @@
1818
},
1919
{
2020
"path": "packages/react-instantsearch-core/dist/umd/ReactInstantSearchCore.min.js",
21-
"maxSize": "51.75 kB"
21+
"maxSize": "52 kB"
2222
},
2323
{
2424
"path": "packages/react-instantsearch/dist/umd/ReactInstantSearch.min.js",
25-
"maxSize": "65.75 kB"
25+
"maxSize": "66 kB"
2626
},
2727
{
2828
"path": "packages/vue-instantsearch/vue2/umd/index.js",
29-
"maxSize": "69.25 kB"
29+
"maxSize": "70 kB"
3030
},
3131
{
3232
"path": "packages/vue-instantsearch/vue3/umd/index.js",
33-
"maxSize": "69.75 kB"
33+
"maxSize": "70 kB"
3434
},
3535
{
3636
"path": "packages/vue-instantsearch/vue2/cjs/index.js",

packages/algoliasearch-helper/src/algoliasearch.helper.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,28 @@ AlgoliaSearchHelper.prototype.searchForFacetValues = function (
437437
query: query,
438438
});
439439

440+
var hide =
441+
(this.lastResults &&
442+
this.lastResults.index === state.index &&
443+
this.lastResults.renderingContent &&
444+
this.lastResults.renderingContent.facetOrdering &&
445+
this.lastResults.renderingContent.facetOrdering.values &&
446+
this.lastResults.renderingContent.facetOrdering.values[facet] &&
447+
this.lastResults.renderingContent.facetOrdering.values[facet].hide) ||
448+
[];
449+
440450
return searchForFacetValuesPromise.then(
441451
function addIsRefined(content) {
442452
self._currentNbQueries--;
443453
if (self._currentNbQueries === 0) self.emit('searchQueueEmpty');
444454

445455
content = Array.isArray(content) ? content[0] : content;
446456

447-
content.facetHits.forEach(function (f) {
457+
content.facetHits.forEach(function (f, i) {
458+
if (hide.indexOf(f.value) > -1) {
459+
content.facetHits.splice(i, 1);
460+
return;
461+
}
448462
f.escapedValue = escapeFacetValue(f.value);
449463
f.isRefined = isDisjunctive
450464
? state.isDisjunctiveFacetRefined(facet, f.escapedValue)

packages/algoliasearch-helper/test/spec/algoliasearch.helper/searchForFacetValues.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,168 @@ test('escaped value is marked as refined', function () {
493493
});
494494
});
495495
});
496+
497+
test('hides a facet value that is hidden according to `renderingContent`', function () {
498+
var fakeClient = {
499+
addAlgoliaAgent: function () {},
500+
search: function () {
501+
return Promise.resolve({
502+
results: [
503+
{
504+
index: 'index',
505+
renderingContent: {
506+
facetOrdering: {
507+
values: {
508+
facet: {
509+
hide: ['hidden'],
510+
},
511+
},
512+
},
513+
},
514+
},
515+
],
516+
});
517+
},
518+
searchForFacetValues: function () {
519+
return Promise.resolve([
520+
{
521+
exhaustiveFacetsCount: true,
522+
facetHits: [
523+
{
524+
count: 318,
525+
highlighted: 'something',
526+
value: 'something',
527+
},
528+
{
529+
count: 1,
530+
highlighted: 'hidden',
531+
value: 'hidden',
532+
},
533+
],
534+
processingTimeMS: 3,
535+
},
536+
]);
537+
},
538+
};
539+
540+
var helper = algoliasearchHelper(fakeClient, 'index', {
541+
disjunctiveFacets: ['facet'],
542+
renderingContent: {
543+
facetValues: [
544+
{
545+
name: 'something',
546+
isRefined: true,
547+
},
548+
],
549+
},
550+
});
551+
552+
return new Promise(function (res) {
553+
helper.search();
554+
helper.once('result', res);
555+
})
556+
.then(function () {
557+
return helper.searchForFacetValues('facet', 'k', 1);
558+
})
559+
.then(function (content) {
560+
expect(content).toEqual({
561+
exhaustiveFacetsCount: true,
562+
processingTimeMS: 3,
563+
facetHits: [
564+
{
565+
count: 318,
566+
highlighted: 'something',
567+
isRefined: false,
568+
escapedValue: 'something',
569+
value: 'something',
570+
},
571+
],
572+
});
573+
});
574+
});
575+
576+
test('does not hide if last results are for another index', function () {
577+
var fakeClient = {
578+
addAlgoliaAgent: function () {},
579+
search: function () {
580+
return Promise.resolve({
581+
results: [
582+
{
583+
index: 'index',
584+
renderingContent: {
585+
facetOrdering: {
586+
values: {
587+
facet: {
588+
hide: ['hidden'],
589+
},
590+
},
591+
},
592+
},
593+
},
594+
],
595+
});
596+
},
597+
searchForFacetValues: function () {
598+
return Promise.resolve([
599+
{
600+
exhaustiveFacetsCount: true,
601+
facetHits: [
602+
{
603+
count: 318,
604+
highlighted: 'something',
605+
value: 'something',
606+
},
607+
{
608+
count: 1,
609+
highlighted: 'hidden',
610+
value: 'hidden',
611+
},
612+
],
613+
processingTimeMS: 3,
614+
},
615+
]);
616+
},
617+
};
618+
619+
var helper = algoliasearchHelper(fakeClient, 'index', {
620+
disjunctiveFacets: ['facet'],
621+
renderingContent: {
622+
facetValues: [
623+
{
624+
name: 'something',
625+
isRefined: true,
626+
},
627+
],
628+
},
629+
});
630+
631+
return new Promise(function (res) {
632+
helper.search();
633+
helper.once('result', res);
634+
})
635+
.then(function () {
636+
return helper.searchForFacetValues('facet', 'k', 1, { index: 'index2' });
637+
})
638+
.then(function (content) {
639+
expect(content).toEqual({
640+
exhaustiveFacetsCount: true,
641+
processingTimeMS: 3,
642+
facetHits: [
643+
{
644+
count: 318,
645+
highlighted: 'something',
646+
isRefined: false,
647+
escapedValue: 'something',
648+
value: 'something',
649+
},
650+
{
651+
count: 1,
652+
highlighted: 'hidden',
653+
isRefined: false,
654+
escapedValue: 'hidden',
655+
value: 'hidden',
656+
},
657+
],
658+
});
659+
});
660+
});

0 commit comments

Comments
 (0)