---
title: Building a search interface for local indexes
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

To allow users to search your website using [locally stored indexes](https://docs.kentico.com/13/configuring-xperience/setting-up-search-on-your-website/using-locally-stored-search-indexes.md), you need to build pages with a search interface. Use the API from the **CMS.Search** namespace (provided as part of the _Kentico.Xperience.Libraries_ NuGet package).

1. Prepare a [SearchParameters](#preparing-the-searchparameters-object) object with the required search inputs and parameters.
2. Call the **SearchHelper.Search** method.

   ```csharp title="Example"

               // Prepares variables required to perform the search operation
               IEnumerable<string> searchIndexes = new List<string> { "ProductIndex", "ArticleIndex" };
               int pageNumber = 1;
               int pageSize = 10;
               UserInfo searchUser = MembershipContext.AuthenticatedUser;
               string cultureCode = "en-us";
               /* Indicates whether the search service uses site default language version of pages as a replacement
               for pages that are not translated into the language specified by 'cultureCode' */
               bool combineWithDefaultCulture = true;

               // Prepares a 'SearchParameters' object to search through indexes of the 'Pages' type
               SearchParameters searchParameters = SearchParameters.PrepareForPages("search query", searchIndexes, pageNumber, pageSize, searchUser, cultureCode, combineWithDefaultCulture);

               // Searches the specified indexes
               SearchResult searchResult = SearchHelper.Search(searchParameters);


   ```

The _Search_ method returns a **SearchResult** object representing the set of results. The result's **Items** property contains a collection of **SearchResultItem** objects, representing individual search results.

Every _SearchResultItem_ provides the following data:

- Properties corresponding to general search result fields (such as the title, image or content extract), which contain data based on the [field mapping](https://docs.kentico.com/13/configuring-xperience/setting-up-search-on-your-website/using-locally-stored-search-indexes/mapping-fields-for-search-results.md) of the indexed object type.
- Properties related to the relevance score and positioning of the result item for the given search.
- The **Data** property, which you can use to access any object-specific values of the searched items.

The _Dat&#x61;_&#x70;roperty is of an abstract _BaseInfo_ type, and its run-time type depends on the searched Xperience object. For example:

- If the search indexes pages, the run-time type of a search result item's _Dat&#x61;_&#x70;roperty is of the _TreeNode_ class. If you [generated classes for your page types](https://docs.kentico.com/13/developing-websites/generating-classes-for-xperience-objects.md), the run-time type is of the generated class.
- If the search indexes pages representing products, the run-time type of a search result item's _Data_ property is of the _SKUTreeNode_ class.
- If the search indexes users, the run-time type of a search result item's _Dat&#x61;_&#x70;roperty is of the _UserInfo_ class.

## Preparing the SearchParameters object

Search is performed by passing a **SearchParameters** object to the _SearchHelper.Search_ method. The object contains all information required by the system to complete the search operation. Since the _SearchParameters_ object is complex and requires a large amount of contextual information, the system provides the **SearchParameters.PrepareForPages** method to facilitate its creation. The method takes the following parameters:

| Parameter                           | Type                            | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ----------------------------------- | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| searchText<br>—OR—<br>searchPattern | string<br>—OR—<br>SearchPattern | The query text. See [Smart search syntax](https://docs.kentico.com/13/configuring-xperience/setting-up-search-on-your-website/using-locally-stored-search-indexes/smart-search-syntax.md) for a reference of supported syntax.<br>You can specify the search query using query text or a _SearchPattern_ object. The object consists of the following:<br>_SearchText_ – the search query to search for in the given indexes.<br>_SupportedSearchSyntax_ – an object of the **SearchOptionsEnum** type that sets the level of supported Lucene syntax for the search query.<br>_SearchOptionsEnum_ allows the following options:<br>_NoneSearch_ – the search does not recognize any Lucene query syntax. The system processes all text entered by users as a part of the search expression.<br>_BasicSearch_ – the search recognizes all query syntax, _except for field search_.<br>_FullSearch_ – the search processes all search query syntax, including field search. |
| searchIndexNames                    | IEnumerable                     | Code names of the indexes to search.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| pageNumber                          | int                             | The page number of paginated results to retrieve starting from 1.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| pageSize                            | int                             | The number of results on each page.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| user                                | IUserInfo                       | The user for whom to filter the search results. For example, filters out pages the user is not authorized to view.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| cultureName                         | string                          | Culture code of the culture to search in. If _null_, searches in all cultures.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| combineWithDefaultCulture           | bool                            | Indicates whether the search service uses site default language version of pages as a replacement for pages that are not translated into the language specified by _cultureName_.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |

> **Info:** **Number of processed search results**
>
> Every search request has a maximum number of processed and returned search results. The purpose of the limit is to avoid processing large numbers of results with low relevance. The limit is determined by the **NumberOfProcessedResults** property of the **SearchParameters** object that you use to perform the search.
>
> By default, the _SearchParameters.PrepareForPages_ method sets the _NumberOfProcessedResults_ property to **100**. If you use large search indexes that regularly produce more results, you can either set a higher value for the property before calling _SearchHelper.Search_, or implement search filters to help narrow down the search.

> **Tip:** **Searching according to page permissions**
>
> To filter out search results for pages that users are not allowed to access, set the **CheckPermissions** property to _true_ for the used **SearchParameters** object. The search results then contain pages according to the [read permissions](https://docs.kentico.com/13/managing-users/configuring-permissions/configuring-page-permissions/page-level-permissions-acls.md) of individual users.

## Example – Building a search interface

The following search example uses an index that covers pages (_TreeNode_ objects) and products (_SKUTreeNode_ objects). The example does not use pagination when displaying search result items.

1. Open your live site (MVC) project in Visual Studio.
2. Create a new model class for working with search queries and search result items.

   ```csharp

   using System.Collections.Generic;
   using CMS.Search;

   ```

   ```csharp

       public class SearchResultModel
       {
           public string Query { get; set; }

           public IEnumerable<SearchResultItem> Items { get; set; }
       }


   ```
3. Create a controller with an action that searches and provides search results.

   ```csharp

   using System;
   using System.Collections.Generic;
   using System.Web.Mvc;

   using CMS.Membership;
   using CMS.Search;


   ```

   ```csharp

           // Adds the smart search indexes that will be used to perform searches
           public static readonly string[] searchIndexes = new string[] { "MVCSite.Index" };
           // Sets the limit of items per page of search results
           private const int PAGE_SIZE = 10;

           /// <summary>
           /// Performs a search and displays its result.
           /// </summary>
           [ValidateInput(false)]
           public ActionResult SearchIndex(string searchText)
           {
               // Displays the search page without any search results if the query is empty
               if (String.IsNullOrWhiteSpace(searchText))
               {
                   // Creates a model representing empty search results
                   SearchResultModel emptyModel = new SearchResultModel
                   {
                       Items = new List<SearchResultItem>(),
                       Query = String.Empty
                   };

                   return View(emptyModel);
               }

               // Searches the specified index and gets the matching results
               SearchParameters searchParameters = SearchParameters.PrepareForPages(searchText, searchIndexes, 1, PAGE_SIZE, MembershipContext.AuthenticatedUser, "en-us", true);
               SearchResult searchResult = SearchHelper.Search(searchParameters);

               // Creates a model with the search result items
               SearchResultModel model = new SearchResultModel
               {
                   Items = searchResult.Items,
                   Query = searchText
               };            

               return View(model);
           }


   ```
4. Add a search input field to one of your site's views. For example:

   ```csharp

   @using (Html.BeginForm("SearchIndex", "Search", FormMethod.Get))
   {
       <input type="text" name="searchtext" placeholder="Search..." maxlength="1000">
       <input type="submit" value="Search">
   }


   ```
5. Create a view that displays the search result items provided by your search controller. For example:

   ```csharp

   @model SearchResultModel

   @using CMS.Search;

   @if (!Model.Items.Any())
   {
       if (!String.IsNullOrWhiteSpace(Model.Query))
       {
           <h3>No results found for "@Model.Query"</h3>
       }
   }
   else
   {
       <h3>Results for "@Model.Query"</h3>
       foreach (SearchResultItem item in Model.Items)
       {
       <div>
           <strong>@item.Title</strong>
       </div>
       <div>
           @Html.Kentico().ResolveUrls(item.Content)
       </div>
       }
   }


   ```

Your visitors can now search on your website through the created search field. The system searches through the specified indexes and displays the results.
