# Perform search in an index

The following search example demonstrates how to perform search in an index.

```C#
using System.Collections.Generic;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Publishing;
using Telerik.Sitefinity.Search;
using Telerik.Sitefinity.Services.Search;
using Telerik.Sitefinity.Services.Search.Data;

namespace SitefinityWebApp
{
   public class SearchExample
   {
       /// <summary>
       /// Perform a search using the specified parameters
       /// </summary>
       /// <param name="query">The search term</param>
       /// <param name="indexName">The name of the search index. Replace spaces with dashes</param>
       /// <param name="searchFields">Array of fields to look for the search term in</param>
       /// <param name="hitCount">Returns the number of hits of the search</param>
       /// <param name="highlightedFields">Array of fields to highlight the values of in the search results</param>
       /// <param name="skip">Skip a number of results</param>
       /// <param name="take">Take a number of results</param>
       /// <param name="resultsForAllSites">The param is used to filter search results per site when the index is for all sites</param>
       /// <returns>Collection of search documents</returns>
       public IEnumerable<IDocument> Search(
           string query,
           string indexName,
           string[] searchFields,
           out int hitCount,
           string[] highlightedFields = null,
           int skip = 0,
           int take = 10,
           bool? resultsForAllSites = null
           )
       {
           if (searchFields.Length == 0)
           {
               // The default fields to search in are Title and Content
               searchFields = new string[]
               {
                   "Title",
                   "Content"
               };
           }

           hitCount = int.MinValue;

           // The culture property is not required and can be null
           // Depending on the configured languages it can include the culture too, for example "de-DE"
           var culture = "en";

           // The orderBy property is not required and can be null
           // Each expression can be a field name only or it can be a field name followed by " asc" to indicate ascending, and " desc" to indicate descending
           var orderBy = new List<string>() { "PublicationDate desc" };

           // The scoringSettings property is not required and can be null
           // Search Scoring feature is only available for Azure Cognitive Search service and must be configured both in Sitefinity CMS and in the index after it has been created
           // For more information, see the documentation https://www.progress.com/documentation/sitefinity-cms/azure-search-services
           var scoringSettings = new SearchScoring();
           scoringSettings.ScoringSetting = "Profile1";
           scoringSettings.ScoringParameters = "customClassificationParam:new;isNewCustomer:true";

           // If we need to make additional filtering, we can create a custom filter and set it to SearchFilter property
           var filter = this.CreateCustomFilter();

           SearchBuilderParams searchParams = new SearchBuilderParams()
           {
               IndexName = indexName,
               SearchText = query,
               SearchFields = searchFields,
               Culture = culture, // The culture from current context will be used when null
               HighlightedFields = highlightedFields, // highlightedFields is not required and can be null, each highlighted field that is set here must be part of searchFields array
               OrderBy = orderBy, // orderBy is not required and can be null
               Skip = skip,
               Take = take,
               ScoringSettings = scoringSettings, // scoringSettings is not required and can be null
               GetResultsFromAllSites = resultsForAllSites, // resultsForAllSites is not required and can be null
               SearchFilter = filter // filter is not required and can be null
           };

           var searcher = ObjectFactory.Resolve<ISearchResultsBuilder>();

           return searcher.Search(searchParams, out hitCount);
       }

       private ISearchFilter CreateCustomFilter()
       {
           // Additional filters can be added to the searchParams
           var filter = ObjectFactory.Resolve<ISearchFilter>();

           // Create a search filter for Categories with clauses
           var categoriesFilterWithClauses = ObjectFactory.Resolve<ISearchFilter>();
           categoriesFilterWithClauses.Clauses = new List<ISearchFilterClause>()
           {
               new SearchFilterClause(PublishingConstants.FieldCategories, "Test", FilterOperator.Equals),
               new SearchFilterClause(PublishingConstants.FieldCategories, "Other", FilterOperator.Equals)
           };

           // Create a new list which will contain the filter groups and add the categories filter with clauses
           var groups = new List<ISearchFilter>() { categoriesFilterWithClauses };

           // Create a search filter for Tags with clauses
           var tagsFilterWithClauses = ObjectFactory.Resolve<ISearchFilter>();
           tagsFilterWithClauses.Clauses = new List<ISearchFilterClause>()
           {
               new SearchFilterClause(PublishingConstants.FieldTags, "TestTag", FilterOperator.Equals),
               new SearchFilterClause(PublishingConstants.FieldTags, "OtherTag", FilterOperator.Equals)
           };

           // We can add the tags filter as well as a group
           groups.Add(tagsFilterWithClauses);

           // Any filter can contain other nested filters with clauses represented as filter Groups. In this case, a QueryOperator must be set.
           filter.Groups = groups;
           filter.Operator = QueryOperator.Or;

           return filter;
       }
   }
}
```

The recommended way to perform search when using the API is to resolve an instance of the `ISearchResultsBuilder` interface by invoking `ObjectFactory.Resolve<ISearchResultsBuilder>()`.\
The resolved object will be used to call the method `IEnumerable<IDocument> Search(SearchBuilderParams searchParams, out int hitCount)`, which will take care to create the search query and invoke the configured search service. The `Search` method returns all the search results that are found as `IEnumerable<IDocument>`, takes a set of parameters organized in `SearchBuilderParams` object, and outputs `hitCount` (the number of the returned search results).

The `SearchBuilderParams` object must include the `IndexName` and `SearchText` properties.\
You can also use this object to set optional properties: `SearchFields`, `Culture`, `HighlightedFields`, `OrderBy`, `Skip`, `Take`, `ScoringSettings`, `GetResultsFromAllSites`, `SearchFilter`.

* If `Culture` property is not provided, it will be taken from the current context.
* The `ScoringSettings` property can only be used with Azure AI Search service, when scoring profiles are configured for the search index.
* When a search index is made common for several sites or for all sites in your Sitefinity CMS, `GetResultsFromAllSites` can be set to retrieve results only for the current site.
* The `SearchFilter` property represents additional filters that can be added and applied to the search query. This can be done by resolving an instance of `ISearchFilter` interface and setting one or more `SearchFilterClause` to the `object.Clauses` field. Each filter created this way can be added to the group filters collection of another `ISearchFilter` instance, giving the possibility to use nested filters with different operators applied.
