---
title: "useInfiniteSearch"
slug: "useinfinitesearch"
abstract: "Learn about the useInfiniteSearch hook that enables searching with infinite scrolling."
---
# useInfiniteSearch

The `useInfiniteSearch` hook enables searching with infinite scrolling. It’s ideal for loading results progressively as the user scrolls or clicks a *Load More* button that can be handled via the `loadMore` function.

## Configuration

To use the hook, you can provide the `searchIndexId` and the search `query`.

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

const { results } = useInfiniteSearch({
  searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
  query: 'product'
});
```

## Supported parameters

The hook expects the ID of the search index and also supports several other parameters used to sort, paginate, perform a manual search etc.

<table style="width:100%;" class="informaltable frame-void rules-rows"><thead><tr><th class="th"><p>Parameter</p></th><th class="th"><p>Type</p></th><th class="th"><p>Description</p></th></tr></thead><tbody><tr><td class="td"><p><code class="code">searchIndexID</code></p></td><td class="td"><p>string</p></td><td class="td"><p>The ID of the SitecoreAI search index to retrieve results from.</p><p><span class="emphasis"><em>Required</em></span></p></td></tr><tr><td class="td"><p><code class="code">query</code></p></td><td class="td"><p>string</p></td><td class="td"><p>Specify a query to search the index for specific content. Mutually exclusive with <code class="code">seedItemId</code> and <code class="code">seedItemUrl</code>.</p></td></tr><tr><td class="td"><p><code class="code">seedItemId</code></p></td><td class="td"><p>string</p></td><td class="td"><p>ID of the item to use as the seed for a More Like This (MLT) query. Mutually exclusive with <code class="code">query</code> and <code class="code">seedItemUrl</code>. Only available in versions <code class="code">2.4.0</code> and above.</p></td></tr><tr><td class="td"><p><code class="code">seedItemUrl</code></p></td><td class="td"><p>string</p></td><td class="td"><p>URL of the item to use as the seed for a More Like This (MLT) query. Mutually exclusive with <code class="code">query</code> and <code class="code">seedItemId</code>. Only available in versions <code class="code">2.4.0</code> and above.</p></td></tr><tr><td class="td"><p><code class="code">pageSize</code></p></td><td class="td"><p>number</p></td><td class="td"><p>Specify the number of results per page.</p></td></tr><tr><td class="td"><p><code class="code">sort</code></p></td><td class="td"><pre class="programlisting json" data-language="json" id="UUID-914e1a2a-07c5-99cf-e6d4-13f238af291c_programlisting-id135315926967244"><code class="language-json" data-language="json">{
name: string;
order: "asc" | "desc"
}
OR
Array&lt;{
name: string;
order: "asc" | "desc"
}&gt;;</code></pre></td><td class="td"><p>Specify an object or array of objects containing the name and order to sort results by.</p></td></tr><tr><td class="td"><p><code class="code">enabled</code></p></td><td class="td"><p>boolean</p></td><td class="td"><p>Specify whether to run a manual search.</p></td></tr><tr><td class="td"><p><code class="code">locale</code></p></td><td class="td"><p>string</p></td><td class="td"><p>Specify the locale for the search. Required for multi-locale index configurations (for example, <code class="code">'en'</code>, <code class="code">'fr-FR'</code>, <code class="code">'el-GR'</code>). Omit for single-locale indexes. Only available in versions <code class="code">2.3.0</code> and above.</p></td></tr><tr><td class="td"><p><code class="code">facet</code></p></td><td class="td"><p><code class="code">FacetRequest</code></p></td><td class="td"><p>Specify facet configuration to retrieve facet counts and/or filter results by facet values. Only available in versions <code class="code">2.3.0</code> and above.</p></td></tr></tbody></table>

## Examples

The hook contains all traditional searching capabilities like sorting (single, multi-field, and nested fields), setting a custom page size, and manual search execution. The hook also supports TypeScript generics that lets you define the shape of your search results for type safety.

### Simple search with keyphrase

A basic search requires only the `searchIndexId`. You can specify additional parameters as needed.

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

function InfiniteSearchComponent() {
  const { results } = useInfiniteSearch({
    searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
    query: 'article',
    pageSize: 20,
  });

  return (
    <div>
      <p>Found {results.length} results</p>
      <ul>
        {results.map((result) => {
          return <li key={result.id}>{result.title}</li>;
        })}
      </ul>
    </div>
  );
}
```

### Search with a generic type

The `useInfiniteSearch` hook supports TypeScript generics, allowing you to define the shape of your search document for type safety and better IDE support.

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

type Article = {
  id: string;
  title: string;
};

function InfiniteSearchComponent() {
  const { results, isLoading, loadMore, hasNextPage, isLoadingMore } = useInfiniteSearch<Article>({
    searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
    query: 'article',
    pageSize: 20,
  });

  if (isLoading) return <div>Loading initial results...</div>;

  return (
    <div>
      <p>Found {results.length} results</p>
      <ul>
        {results.map((result) => {
          return <li key={result.id}>{result.title}</li>;
        })}
      </ul>

      {isLoadingMore && (
        <div style={{ textAlign: 'center', padding: '1rem' }}>
          Loading more results...
        </div>
      )}

      {hasNextPage && (
        <button onClick={loadMore} disabled={isLoadingMore}>
          {isLoadingMore ? 'Loading...' : 'Load More'}
        </button>
      )}
    </div>
  );
}
```

### Search with basic sorting

The hook supports sorting results using a single `sort` object specifying the `name` and `order`. The following example sorts the search results by the `price` in the ascending order (lowest first).

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

function SortedInfiniteSearch() {
  const { results, loadMore, hasNextPage, isLoadingMore } = useInfiniteSearch<Event>({
    searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
    query: 'conference',
    pageSize: 20,
    sort: {
      name: 'price',
      order: 'asc',
    },
  });
  ... rest of the code ...
}
```

### Search with multi-field sorting

You can also specify multiple fields to sort. Instead of a single object, you should specify this as an array of objects. The following example sorts the search results by `category` in the ascending order, followed by `price` in ascending order.

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

const { results, loadMore, hasNextPage } = useInfiniteSearch<Product>({
  searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
  query: 'product',
  pageSize: 30,
  sort: [
    {
      name: 'category',
      order: 'asc',
    },
    {
      name: 'price',
      order: 'asc',
    },
  ],
});
```

### Search with nested field sorting

Using dot notation (`.`), you can sort nested properties.

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

type Event = {
  id: string;
  name: string;
  date: string;
  organizer: {
    name: string;
    company: string;
  };
  attendees: number;
};

const { results, loadMore, hasNextPage } = useInfiniteSearch<Event>({
  searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
  query: 'conference',
  pageSize: 20,
  sort: {
    name: 'organizer.name', // Sort by nested property using dot notation
    order: 'asc',
  },
});
```

### Enable manual search

You can perform a manual search by setting the `enabled` parameter to `true`. This is useful when you want the user to type in a query and click a button to perform the search.

```csharp
import { useState } from 'react';
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

const [enabled, setEnabled] = useState(false);
const { results, isLoading, loadMore, hasNextPage } = useInfiniteSearch<Product>({
  searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
  query: 'product',
  pageSize: 30,
  enabled,
});
... rest of the code ...
```

### Search with custom page size

You can customize the number of results loaded per page using the `pageSize` option:

```csharp
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

const { results, loadMore, hasNextPage } = useInfiniteSearch<Product>({
  searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
  query: 'product',
  pageSize: 30,
});
... rest of the code ...
```

### Search with locale

Specify the locale using the `locale` parameter. For single-locale indexes, you can omit this parameter. This is only available in versions `2.3.0` and above.

```ts
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

function LocalisedInfiniteSearch() {
  const { results, loadMore, hasNextPage } = useInfiniteSearch({
    searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
    query: 'article',
    locale: 'fr-FR',
  });

  // ... rest of the code
}
```

### Search with facets

Set the facet value to `all: true` to return counts for all enabled facets. You can also set the facet value to specific fields to narrow the results to items matching given values or use a combination of both all:true and specific fields. This is only available in versions `2.3.0` and above.

```ts
import { useInfiniteSearch } from '@sitecore-content-sdk/nextjs/search';

type Product = {
  id: string;
  name: string;
  category: string;
};

function FacetedInfiniteSearch() {
  const { results, facets, loadMore, hasNextPage, isLoadingMore } = useInfiniteSearch<Product>({
    searchIndexId: '<YOUR_SEARCH_INDEX_ID>',
    query: 'shoes',
    facet: { all: true },
  });

  return (
    <div>
      {/* Facet sidebar */}
      <aside>
        {facets?.map((facet) => (
          <div key={facet.name}>
            <strong>{facet.name}</strong>
            <ul>
              {facet.value.map((v) => (
                <li key={String(v.text)}>{String(v.text)} ({v.count})</li>
              ))}
            </ul>
          </div>
        ))}
      </aside>

      {/* Results */}
      <main>
        {results.map((product) => (
          <div key={product.id}>{product.name}</div>
        ))}

        {hasNextPage && (
          <button onClick={loadMore} disabled={isLoadingMore}>
            {isLoadingMore ? 'Loading...' : 'Load More'}
          </button>
        )}
      </main>
    </div>
  );
}
```

### Search with More Like This (MLT)

Instead of `query`, you can pass `seedItemId` or `seedItemUrl` to retrieve items similar to a seed item, loaded incrementally with `loadMore`. These fields are mutually exclusive with `query` and with each other.

You can also seed the query using the item's URL. `pageSize`, `sort`, `locale`, and `facet` all work the same way with MLT queries as they do with a keyword search.

```ts
import { useInfiniteSearch } from '@sitecore-content-sdk/react';

const search = useInfiniteSearch({
  searchIndexId: '1234567890',
  seedItemUrl: 'https://example.com/articles/cloud',
});
```