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

# getAlgoliaResults

> Retrieve Algolia results from one or several indices with autocomplete-preset-algolia.

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 `getAlgoliaResults` function lets you **query one or several Algolia indices.**

Using `getAlgoliaResults` 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).

<Info>
  If you're using [`autocomplete-js`](/doc/ui-libraries/autocomplete/api-reference/autocomplete-js),
  **you don't need to import the preset separately.**
  The package includes all Algolia utilities, with the appropriate user agents and virtual DOM support.
</Info>

## Installation

First, you need to install the preset.

<CodeGroup>
  ```sh npm theme={"system"}
  npm install @algolia/autocomplete-preset-algolia
  ```

  ```sh yarn theme={"system"}
  yarn add @algolia/autocomplete-preset-algolia
  ```
</CodeGroup>

Then import it in your project:

```js JavaScript icon=code theme={"system"}
import { getAlgoliaResults } from "@algolia/autocomplete-preset-algolia";
```

If you don't use a package manager, you can use the HTML `script` element:

```html HTML icon=code-xml theme={"system"}
<script
  src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-preset-algolia@1.19.9/dist/umd/index.production.js"
  integrity="sha256-mbQPS+x0+jNL5Xw3QUxxFofMTi1IncKd/7o4dqP9PDY="
  crossorigin="anonymous"
></script>
<script>
  const { getAlgoliaResults } = window["@algolia/autocomplete-preset-algolia"];
</script>
```

## Example

This example uses the function along with the [`algoliasearch`](https://www.npmjs.com/package/algoliasearch) API client.

```js JavaScript icon=code theme={"system"}
import { liteClient as algoliasearch } from "algoliasearch/lite";
import { createAutocomplete } from "@algolia/autocomplete-core";
import { getAlgoliaResults } from "@algolia/autocomplete-preset-algolia";

const searchClient = algoliasearch(
  "latency",
  "6be0576ff61c053d5f9a3225e2a90f76",
);

const autocomplete = createAutocomplete({
  // ...
  getSources() {
    return [
      {
        sourceId: "products",
        getItems({ query }) {
          return getAlgoliaResults({
            searchClient,
            queries: [
              {
                indexName: "instant_search",
                params: {
                  query,
                  hitsPerPage: 5,
                },
              },
            ],
          });
        },
        // ...
      },
    ];
  },
});
```

## Parameters

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

<ParamField body="queries" type="MultipleQueriesQuery[]" required>
  The queries to perform.

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

    {" "}

    <ParamField body="MultipleQueriesQuery.query" type="string">
      The query to search for.
    </ParamField>

    <ParamField body="MultipleQueriesQuery.params" type="SearchParameters">
      Algolia [search parameters](/doc/api-reference/search-api-parameters).

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

      ```json JSON icon=braces theme={"system"}
      {
        "hitsPerPage": 5,
        "highlightPreTag": "__aa-highlight__",
        "highlightPostTag": "__/aa-highlight__"
      }
      ```
    </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"}
  getAlgoliaResults({
    // ...
    transformResponse({ hits }) {
      return hits;
    },
  });
  ```
</ParamField>

## Returns

The `getAlgoliaResults` function returns:

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