getAlgoliaFacets
The getAlgoliaFacets
function lets you query facet hits from one or several Algolia indices.
Using getAlgoliaFacets
lets Autocomplete batch all queries using the same search client into a single network call, and thus minimize search unit consumption. It also works out of the box with the components
exposed in templates.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import algoliasearch from 'algoliasearch/lite';
import { autocomplete, getAlgoliaFacets } from '@algolia/autocomplete-js';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
autocomplete({
// ...
getSources({ query }) {
return [
{
sourceId: 'products',
getItems({ query }) {
return getAlgoliaFacets({
searchClient,
queries: [
{
indexName: 'instant_search',
facet: 'categories',
params: {
facetQuery: query,
maxFacetHits: 10,
},
},
],
});
},
// ...
},
];
},
});
When using getAlgoliaFacets
and getAlgoliaResults
with the same search client in different sources or plugins, Autocomplete batches all queries into a single network call to Algolia. If you’re using the same search client for different sources or plugins, make sure to use the same instance to leverage the internal cache and batching mechanism.
Parameters
Parameter | Description |
---|---|
searchClient
|
type: SearchClient
Required
The initialized Algolia search client. |
queries
|
type: FacetQuery[]
Required
The queries to search for. |
queries âž” queries
Parameter | Description | ||
---|---|---|---|
indexName
|
type: string
Required
The index name. |
||
facet
|
type: string
Required
The attribute name to search facet values into. Note that for this to work, it must be declared in the |
||
params
|
type: SearchForFacetValuesQueryParams
Algolia search for facet values parameters. These are the default parameters. You can leave them as is and specify other parameters, or override them.
Copy
If you override |
||
transformResponse
|
type: (response: { results: Array<SearchResponse<THit> | SearchForFacetValuesResponse>, hits: MaybeArray<Hit<THit>[]>, facetHits: MaybeArray<FacetHit[]> }) => MaybeArray<Hit<THit>[] | FacetHit[]>
The function to transform the Algolia response before passing it to the Autocomplete state. You have access to the full Algolia results, as well as the pre-computed hits and facet hits. This is useful to manipulate the hits, or store data from the results in the context. This is the default implementation:
Copy
|
Returns
The function returns a description with the following interface:
1
2
3
4
5
6
{
searchClient: SearchClient;
queries: MultipleQueriesQuery[];
transformResponse: TransformResponse<THit>;
execute: Execute<THit>;
}