---
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.</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-cf135828-68fa-280c-966f-e3cf3e72bfeb_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></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 ...
```