getAlgoliaFacetHits

Retrieves Algolia facet hits from multiple indices.

The getAlgoliaFacetHits function lets you query facet hits several Algolia indices at once.

note

If you're using autocomplete-js, all Algolia utilities are available directly in the package with the right user agents and the virtual DOM layer. You don't need to import the preset separately.

Installation#

First, you need to install the preset.

yarn add @algolia/autocomplete-preset-algolia@alpha
# or
npm install @algolia/autocomplete-preset-algolia@alpha

Then import it in your project:

import { getAlgoliaFacetHits } from '@algolia/autocomplete-preset-algolia';

If you don't use a package manager, you can use a standalone endpoint:

<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-preset-algolia@alpha"></script>

Example#

import { getAlgoliaFacetHits } from '@algolia/autocomplete-preset-algolia';
import algoliasearch from 'algoliasearch/lite';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
getAlgoliaFacetHits({
searchClient,
queries: [
{
indexName: 'instant_search',
params: {
facetName: 'categories',
facetQuery: query,
maxFacetHits: 10,
},
},
],
}).then((facetHits) => {
console.log(facetHits);
});

Parameters#

searchClient#

SearchClient | required

The initialized Algolia search client.

queries#

FacetQuery[] | required

The queries to search for, with the following parameters:

indexName#

string | required

The index name.

params#

SearchForFacetValuesQueryParams & SearchOptions

Algolia search for facet values parameters.

These are the default parameters. You can leave them as is and specify other parameters, or override them.

info

If you override highlightPreTag and highlightPostTag, you won't be able to use the built-in highlighting utilities such as highlightHit.

{
"highlightPreTag": "__aa-highlight__",
"highlightPostTag": "__/aa-highlight__"
}

Returns#

The function returns a promise that resolves to a response with the following schema:

[
{
"count": 507,
"label": "Mobile phones",
"_highlightResult": {
"label": {
"value": "Mobile <em>phone</em>s"
}
}
},
{
"count": 63,
"label": "Phone cases",
"_highlightResult": {
"label": {
"value": "<em>Phone</em> cases"
}
}
}
]