Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add searchForFacetValues for composition #6489

Merged
merged 5 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
},
{
"path": "./packages/instantsearch.js/dist/instantsearch.production.min.js",
"maxSize": "83.75 kB"
"maxSize": "84 kB"
},
{
"path": "./packages/instantsearch.js/dist/instantsearch.development.js",
"maxSize": "182.25 kB"
},
{
"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",
"maxSize": "65.50 kB"
},
{
"path": "packages/vue-instantsearch/vue2/umd/index.js",
"maxSize": "69.00 kB"
"maxSize": "69.25 kB"
},
{
"path": "packages/vue-instantsearch/vue3/umd/index.js",
Expand Down
20 changes: 20 additions & 0 deletions packages/algoliasearch-helper/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ declare namespace algoliasearchHelper {
userState?: PlainSearchParameters
): Promise<SearchForFacetValues.Result>;

/**
* 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<SearchForFacetValues.Result>;

/**
* Sets the text query used for the search.
*
Expand Down
75 changes: 75 additions & 0 deletions packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,81 @@ 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.<FacetSearchResult>} 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: maxFacetHits,
searchQuery: { query: query },
},
},
});

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.
*
Expand Down
5 changes: 5 additions & 0 deletions packages/instantsearch.js/src/lib/InstantSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ See documentation: ${createDocumentationLink({
persistHierarchicalRootCount: this.future.persistHierarchicalRootCount,
});

if (this.compositionID) {
mainHelper.searchForFacetValues =
mainHelper.searchForCompositionFacetValues.bind(mainHelper);
}
Comment on lines +592 to +595
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with this approach because it allows things to be completely transparent for connectRefinementList, but I'm open to suggestions


mainHelper.search = () => {
this.status = 'loading';
this.scheduleRender(false);
Expand Down