From 26a79295a088bc441f5313876031f0df054c4eac Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Fri, 20 Dec 2024 16:42:25 +0100 Subject: [PATCH 1/5] feat: add searchForCompositionFacetValues in the helper --- packages/algoliasearch-helper/index.d.ts | 20 +++++ .../src/algoliasearch.helper.js | 79 +++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/packages/algoliasearch-helper/index.d.ts b/packages/algoliasearch-helper/index.d.ts index b3b2410512..4430f2fdfe 100644 --- a/packages/algoliasearch-helper/index.d.ts +++ b/packages/algoliasearch-helper/index.d.ts @@ -197,6 +197,26 @@ declare namespace algoliasearchHelper { userState?: PlainSearchParameters ): Promise; + /** + * Search for facet values using the Composition API & based on a query and the name of a faceted attribute. + * This triggers a search and will return a promise. On top of using the query, it also sends + * the parameters from the state so that the search is narrowed down to only the possible values. + * + * See the description of [FacetSearchResult](reference.html#FacetSearchResult) + * @param facet the name of the faceted attribute + * @param query the string query for the search + * @param [maxFacetHits] the maximum number values returned. Should be > 0 and <= 100 + * @param [userState] the set of custom parameters to use on top of the current state. Setting a property to `undefined` removes + * it in the generated query. + * @return the results of the search + */ + searchForCompositionFacetValues( + facet: string, + query: string, + maxFacetHits: number, + userState?: PlainSearchParameters + ): Promise; + /** * Sets the text query used for the search. * diff --git a/packages/algoliasearch-helper/src/algoliasearch.helper.js b/packages/algoliasearch-helper/src/algoliasearch.helper.js index b1aa13f677..2115f2c1aa 100644 --- a/packages/algoliasearch-helper/src/algoliasearch.helper.js +++ b/packages/algoliasearch-helper/src/algoliasearch.helper.js @@ -461,6 +461,85 @@ AlgoliaSearchHelper.prototype.searchForFacetValues = function ( ); }; +/** + * Search for facet values using the Composition API & based on a query and the name of a faceted attribute. + * This triggers a search and will return a promise. On top of using the query, it also sends + * the parameters from the state so that the search is narrowed down to only the possible values. + * + * See the description of [FacetSearchResult](reference.html#FacetSearchResult) + * @param {string} facet the name of the faceted attribute + * @param {string} query the string query for the search + * @param {number} [maxFacetHits] the maximum number values returned. Should be > 0 and <= 100 + * @param {object} [userState] the set of custom parameters to use on top of the current state. Setting a property to `undefined` removes + * it in the generated query. + * @return {promise.} the results of the search + */ +AlgoliaSearchHelper.prototype.searchForCompositionFacetValues = function ( + facet, + query, + maxFacetHits, + userState +) { + if (typeof this.client.searchForFacetValues !== 'function') { + throw new Error( + 'search for facet values (searchable) was called, but this client does not have a function client.searchForFacetValues' + ); + } + + var state = this.state.setQueryParameters(userState || {}); + var isDisjunctive = state.isDisjunctiveFacet(facet); + + this._currentNbQueries++; + // eslint-disable-next-line consistent-this + var self = this; + var searchForFacetValuesPromise; + + searchForFacetValuesPromise = this.client.searchForFacetValues({ + compositionID: state.index, + facetName: facet, + searchForFacetValuesRequest: { + params: { + query: state.query, + maxFacetHits, + searchQuery: { query }, + }, + }, + }); + // searchForFacetValuesPromise = this.client.searchForFacetValues([ + // { indexName: state.index, params: algoliaQuery }, + // ]); + // algoliasearch < 3.27.1 + + this.emit('searchForFacetValues', { + state: state, + facet: facet, + query: query, + }); + + return searchForFacetValuesPromise.then( + function addIsRefined(content) { + self._currentNbQueries--; + if (self._currentNbQueries === 0) self.emit('searchQueueEmpty'); + + content = content.results[0]; + + content.facetHits.forEach(function (f) { + f.escapedValue = escapeFacetValue(f.value); + f.isRefined = isDisjunctive + ? state.isDisjunctiveFacetRefined(facet, f.escapedValue) + : state.isFacetRefined(facet, f.escapedValue); + }); + + return content; + }, + function (e) { + self._currentNbQueries--; + if (self._currentNbQueries === 0) self.emit('searchQueueEmpty'); + throw e; + } + ); +}; + /** * Sets the text query used for the search. * From 00e11cbe437c042c940d5fc086de506c06fe88b2 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Fri, 20 Dec 2024 16:42:42 +0100 Subject: [PATCH 2/5] feat: plug searchForFacetValues for Composition --- packages/instantsearch.js/src/lib/InstantSearch.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/instantsearch.js/src/lib/InstantSearch.ts b/packages/instantsearch.js/src/lib/InstantSearch.ts index 89b673f458..1eef6c5508 100644 --- a/packages/instantsearch.js/src/lib/InstantSearch.ts +++ b/packages/instantsearch.js/src/lib/InstantSearch.ts @@ -589,6 +589,11 @@ See documentation: ${createDocumentationLink({ persistHierarchicalRootCount: this.future.persistHierarchicalRootCount, }); + if (this.compositionID) { + mainHelper.searchForFacetValues = + mainHelper.searchForCompositionFacetValues.bind(mainHelper); + } + mainHelper.search = () => { this.status = 'loading'; this.scheduleRender(false); From 7c729179307290d162ce6a412dc89ae3cc6e226a Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Fri, 20 Dec 2024 16:49:10 +0100 Subject: [PATCH 3/5] fix: js syntax --- packages/algoliasearch-helper/src/algoliasearch.helper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/algoliasearch-helper/src/algoliasearch.helper.js b/packages/algoliasearch-helper/src/algoliasearch.helper.js index 2115f2c1aa..ceb4989ecd 100644 --- a/packages/algoliasearch-helper/src/algoliasearch.helper.js +++ b/packages/algoliasearch-helper/src/algoliasearch.helper.js @@ -500,8 +500,8 @@ AlgoliaSearchHelper.prototype.searchForCompositionFacetValues = function ( searchForFacetValuesRequest: { params: { query: state.query, - maxFacetHits, - searchQuery: { query }, + maxFacetHits: maxFacetHits, + searchQuery: { query: query }, }, }, }); From aafd00648befd9c7e3b2a9582b079756fa1c3c9c Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Fri, 20 Dec 2024 17:02:57 +0100 Subject: [PATCH 4/5] fix: bundlesizes --- bundlesize.config.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bundlesize.config.json b/bundlesize.config.json index 52a70b56a6..e90615b149 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -10,7 +10,7 @@ }, { "path": "./packages/instantsearch.js/dist/instantsearch.production.min.js", - "maxSize": "83.75 kB" + "maxSize": "84 kB" }, { "path": "./packages/instantsearch.js/dist/instantsearch.development.js", @@ -18,7 +18,7 @@ }, { "path": "packages/react-instantsearch-core/dist/umd/ReactInstantSearchCore.min.js", - "maxSize": "51.50 kB" + "maxSize": "51.75 kB" }, { "path": "packages/react-instantsearch/dist/umd/ReactInstantSearch.min.js", @@ -26,7 +26,7 @@ }, { "path": "packages/vue-instantsearch/vue2/umd/index.js", - "maxSize": "69.00 kB" + "maxSize": "69.25 kB" }, { "path": "packages/vue-instantsearch/vue3/umd/index.js", From 7d594f374e615fef536bd8e93dceec5346cc65a6 Mon Sep 17 00:00:00 2001 From: Emmanuel Krebs Date: Fri, 20 Dec 2024 17:34:42 +0100 Subject: [PATCH 5/5] Update packages/algoliasearch-helper/src/algoliasearch.helper.js Co-authored-by: Haroen Viaene --- packages/algoliasearch-helper/src/algoliasearch.helper.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/algoliasearch-helper/src/algoliasearch.helper.js b/packages/algoliasearch-helper/src/algoliasearch.helper.js index ceb4989ecd..f0496dcd67 100644 --- a/packages/algoliasearch-helper/src/algoliasearch.helper.js +++ b/packages/algoliasearch-helper/src/algoliasearch.helper.js @@ -505,10 +505,6 @@ AlgoliaSearchHelper.prototype.searchForCompositionFacetValues = function ( }, }, }); - // searchForFacetValuesPromise = this.client.searchForFacetValues([ - // { indexName: state.index, params: algoliaQuery }, - // ]); - // algoliasearch < 3.27.1 this.emit('searchForFacetValues', { state: state,