---
title: Providing smart search on MVC sites
related:
  - https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website/adding-search-functionality-to-pages/setting-up-predictive-search.md
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development.md
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md
---

> 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).

Provide [smart search](https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website.md) on your MVC site to enable your visitors to search through your [pages](https://docs.kentico.com/k10/managing-website-content/working-with-pages.md), [products](https://docs.kentico.com/k10/e-commerce-features/managing-your-store/products.md) or other objects on your site. Smart search is an index-based search engine, which allows users to search through the content of websites and various types of data within the system. The smart search uses indexes to store information about the website content. See [Setting up search on your website](https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website.md) to learn more about smart search.

To provide smart search on your MVC site:

1. [Configure automatic Web farm mode](https://docs.kentico.com/display/K10/Configuring+web+farm+servers#Configuringwebfarmservers-Configuringwebfarmsautomatically) in Kentico.
2. [Enable smart search indexing](https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website/enabling-search-indexing.md).
3. [Create search indexes](https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website/creating-search-indexes.md). Assign the indexes to your site and define their exact content.

   > **Info:** With the **Web farm** service enabled and the related web farm servers showing the _Healthy_ status, search indexes on MVC applications are updated automatically.
   >
   > If you want to process the tasks in a regular interval (e.g. by a scheduler), you need to use the [Scheduler Windows service](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/processing-scheduled-tasks-when-developing-mvc-applications.md#example---regularly-processing-search-indexing-tasks-on-mvc-sites) since the standard way of scheduling tasks is not available in MVC applications.
4. Install the [Kentico.Searchintegration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) to your MVC application.
5. Initialize the **SearchService** from the integration package with the indexes you want to search through.

Use the **Search** method that executes the search and returns a set of results of the **SearchResult** class.

```csharp

            ISearchService searchService = new SearchService();

            SearchResult searchResult = searchService.Search(new SearchOptions(searchText, searchIndexes)
            {
                CultureName = "en-us",
                CombineWithDefaultCulture = true,
                PageSize = PAGE_SIZE
            });


```

In the search result's **Items** property, you can find the **Fields** and **Data** properties. The _Fields_ property contains fields available for all full-text search result items (such as the title or image). The _Data_ property is of an abstract _BaseInfo_ class. Its run-time type contains all type specific properties. For example:

- If the search indexes users, the run-time type of a search result item's _Data_ property is of the _UserInfo_ class.

- If the search indexes pages, the run-time type of a search result item's _Data_ property is of the _TreeNode_ class. If you [generated classes for your page types](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-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.

With the _Data_ property, you can access any object-specific property.

> **Info:** Currently, the _Kentico.Search_ integration package supports all types of [search indexes](https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website/creating-search-indexes.md) except the [custom indexes](https://docs.kentico.com/k10/custom-development/miscellaneous-custom-development-tasks/smart-search-api-and-customization/creating-custom-smart-search-indexes.md).

## Example – Providing search on MVC sites

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

> **Tip:** **Tip**: To view the full code of a functional example directly in Visual Studio, download the [Kentico MVC solution](https://github.com/Kentico/Mvc) from GitHub and inspect the **LearningKit** project. You can also run the Learning Kit website after connecting the project to a Kentico database.

1. Install the _Kentico.Search_ [integrationpackage](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) in your MVC application.
2. Create a new model for working with search queries and search result items.

   ```csharp

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

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


   ```
3. Create a new model for the search result item (_SearchResultItemModel_ in the example).
4. Create a controller with a controller action for displaying the results.

   ```csharp

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

   using CMS.DataEngine;

   using Kentico.Search;


   ```

   ```csharp

           // Adds the smart search indexes that will be used when performing a search and sets item count per page
           public static readonly string[] searchIndexes = new string[] { "MVCSite.Index" };
           private const int PAGE_SIZE = 10;

           /// <summary>
           /// Constructor.
           /// Initializes the search service that takes care of performing searches. You can use a dependency injection container to initialize the service.
           /// </summary>
           public SearchController()
           {
               searchService = new SearchService();
           }

           /// <summary>
           /// Performs a search and displays its result.
           /// </summary>
           /// <param name="searchText">Query for search.</param>
           [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<SearchResultItemModel>(),
                       Query = String.Empty
                   };

                   return View(emptyModel);
               }

               // Searches with the smart search through Kentico and gets the search result
               SearchResult searchResult = searchService.Search(new SearchOptions(searchText, searchIndexes)
               {
                   CultureName = "en-us",
                   CombineWithDefaultCulture = true,
                   PageSize = PAGE_SIZE
               });

               // Creates a list for search result item models
               List<SearchResultItemModel> itemModels = new List<SearchResultItemModel>();

               // Loops through the search result items
               foreach (SearchResultItem<BaseInfo> searchResultItem in searchResult.Items)
               {
                   // Adds data to a view model. You can adjust the logic here to get to the object specific fields
                   itemModels.Add(new SearchResultItemModel(searchResultItem.Fields));
               }

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

               return View(model);
           }


   ```
5. Provide a search field in one of your 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">
   }


   ```
6. Provide a view that displays search result items.

   ```csharp

   @model SearchResultModel

   @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 (SearchResultItemModel item in Model.Items)
       {
           <div>
               @item.Title
               @item.Date.ToLongDateString()
           </div>
       }
   }


   ```

Your visitors can now search on your website with the created search field. The system displays the results based on the indexes specified in the MVC application.
