getAlgoliaHits

Retrieves Algolia hits from multiple indices as arrays of records.

The getAlgoliaHits function lets you query several Algolia indices at once and returns the hits as an array of records.

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 { getAlgoliaHits } 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#

This example uses the function along with the algoliasearch API client.

import { getAlgoliaHits } from '@algolia/autocomplete-preset-algolia';
import algoliasearch from 'algoliasearch/lite';
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
getAlgoliaHits({
searchClient,
queries: [
{
indexName: 'instant_search',
query,
params: {
hitsPerPage: 3,
},
},
],
}).then((hits) => {
console.log(hits);
});

Parameters#

searchClient#

SearchClient | required

The initialized Algolia search client.

queries#

MultipleQueriesQuery[] | required

The queries to perform, with the following parameters:

indexName#

string | required

The index name to search into.

query#

string

The query to search for.

params#

SearchParameters

Algolia search parameters.

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

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

Returns#

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

[
{
"objectID": "433",
"firstname": "Jimmie",
"lastname": "Barninger",
"_highlightResult": {
"firstname": {
"value": "&lt;em&gt;Jimmie&lt;/em&gt;",
"matchLevel": "partial"
},
"lastname": {
"value": "Barninger",
"matchLevel": "none"
}
}
}
]