Perform search in an index
The following search example demonstrates how to perform search in an index.
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 Cognitive 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.