> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# getAlgoliaFacets

> Retrieve Algolia facet hits from one or several indices with autocomplete-js.

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

<Tip>
  Autocomplete is also available as an experimental widget in InstantSearch,
  making it easier to integrate into your search experience.
  For more information,
  see the API reference for [InstantSearch.js](/doc/api-reference/widgets/autocomplete/js) or
  [React InstantSearch](/doc/api-reference/widgets/autocomplete/react).
</Tip>

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`](/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete#param-components)
exposed in [templates](/doc/ui-libraries/autocomplete/core-concepts/templates).

## Example

```js JavaScript icon=code theme={"system"}
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,
                },
              },
            ],
          });
        },
        // ...
      },
    ];
  },
});
```

<Note>
  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.
</Note>

## Parameters

<ParamField body="searchClient" type="SearchClient">
  The initialized Algolia search client.
</ParamField>

<ParamField body="queries" type="FacetQuery[]">
  The queries to search for.

  <Expandable title="FacetQuery properties">
    <ParamField body="FacetQuery.indexName" type="string" required>
      The <Index /> name.
    </ParamField>

    <ParamField body="FacetQuery.facet" type="string" required>
      The attribute name to search facet values into.

      The attribute must be declared in the [`attributesForFaceting`](/doc/api-reference/api-parameters/attributesForFaceting)
      index setting with the `searchable()` modifier.
    </ParamField>

    <ParamField body="FacetQuery.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.

      ```json JSON icon=braces theme={"system"}
      {
        "highlightPreTag": "__aa-highlight__",
        "highlightPostTag": "__/aa-highlight__"
      }
      ```

      <Note>
        If you override `highlightPreTag` and `highlightPostTag`, you won't be able to
        use the built-in highlighting components such as `Highlight`.
      </Note>
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="transformResponse" type="function">
  ```ts Type definition theme={"system"}
  (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](/doc/ui-libraries/autocomplete/core-concepts/context).

  This is the default implementation:

  ```js JavaScript icon=code theme={"system"}
  getAlgoliaFacets({
    // ...
    transformResponse({ facetHits }) {
      return facetHits;
    },
  });
  ```
</ParamField>

## Returns

The function returns:

```ts TypeScript icon=code theme={"system"}
{
  searchClient: SearchClient;
  queries: MultipleQueriesQuery[];
  transformResponse: TransformResponse<THit>;
  execute: Execute<THit>;
}
```
