Skip to main content
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

JavaScript
import { liteClient as 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

searchClient
SearchClient
The initialized Algolia search client.
queries
FacetQuery[]
The queries to search for.

Returns

The function returns:
TypeScript
{
  searchClient: SearchClient;
  queries: MultipleQueriesQuery[];
  transformResponse: TransformResponse<THit>;
  execute: Execute<THit>;
}
I