Create a custom search service

Overview

Sitefinity CMS supports the following search services out of the box: Lucene.NET (default service), Azure AI Search service, and Elasticsearchservice. If support for another search service is needed (such as Solr, SharePoint Search, and more), you can accomplish this with custom code. The ISearchService interface must be implemented and then to replace the default service, the interface should be registered by invoking ObjectFactory.RegisterType.

Sitefinity CMS calls the UpdateIndex method of the your implementation when you create and modify each item. It provides a collection of IDocument instances to be added or updated in the index.

The Search method of the registered interface (either the built-in or a custom implementation) is invoked whenever the user uses the Search widget. If the Search widget is not used on your Sitefinity CMS site, the other interface methods can be used in order to run a search query directly through the API.

The Search method takes ISearchQuery and SearchOptions as arguments. SearchOptions are specifically used for defining the search type, scoring settings when search scoring is required (only available for Azure AI Search service) or if search facets are required (available for Azure AI Search and Elasticsearch services). In the default implementation, the query is instantiated and its ISearchGroup groups are built with the BuildQuery method of the IQueryBuilder interface. You can add or remove groups to the query once it is built.
ISearchGroup consists of ISearchTerm and ISearchGroup enumerations. When building the search expression, the group operator is used between the terms and the groups.
ISearchTerm describes a search term with its Field and Value. In most cases, terms contains the search field names and the text that the user is searching for.
ISearchFilter defines a search filter for the search query. The filter normally contains static clauses like language filtering, type filtering, date filtering, or any other custom filtering.

ISearchService methods

Here is a list of ISearchService methods:

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

namespace Telerik.Sitefinity.Services.Search
{
   /// <summary>
   /// The contract for search operations that provide the ability to create search catalogues, index and returns the documents matching search query.
   /// </summary>
   public interface ISearchService
   {
       /// <summary>
       /// Searches using given query and returns the documents matching the query.
       /// </summary>
       /// <param name="query">The search query.</param>
       /// <returns>The result set</returns>
       IResultSet Search(ISearchQuery query);

       /// <summary>
       /// Searches using given query and returns the documents matching the query.
       /// </summary>
       /// <param name="query">The search query.</param>
       /// <param name="searchOptions">The search options.</param>
       /// <returns>The result set</returns>
       IResultSet Search(ISearchQuery query, SearchOptions searchOptions);

       /// <summary>
       /// Creates new search index
       /// </summary>
       /// <param name="name">The name of the index</param>
       /// <param name="fieldDefinitions">The definitions for the fields included in the search index</param>
       void CreateIndex(string name, IEnumerable<IFieldDefinition> fieldDefinitions);

       /// <summary>
       /// Adds or updates the provided documents in the specified index.
       /// </summary>
       /// <param name="name">Name of the index.</param>
       /// <param name="documents">A collection of documents to add or update.</param>
       void UpdateIndex(string name, IEnumerable<IDocument> documents);

       /// <summary>
       /// Return true if the index exists.
       /// </summary>
       /// <param name="indexName">Name of the index.</param>
       /// <returns>A value, indicating if the index exists</returns>
       bool IndexExists(string indexName);

       /// <summary>
       /// Removes the provided documents from the specified catalogue.
       /// </summary>
       /// <param name="indexName">The name of the index.</param>
       /// <param name="documents">The collection of documents to remove.</param>
       void RemoveDocuments(string indexName, IEnumerable<IDocument> documents);

       /// <summary>
       /// Removes the document with the specified identity from the specified catalogue.
       /// </summary>
       /// <param name="indexName">The name of the index.</param>
       /// <param name="identityField">The identity field.</param>
       void RemoveDocument(string indexName, IField identityField);

       /// <summary>
       /// Renames the specified index.
       /// </summary>
       /// <param name="indexName">The name of the index.</param>
       /// <param name="newIndexName">The new name of the index.</param>
       void RenameIndex(string indexName, string newIndexName);

       /// <summary>
       /// Deletes the specified index.
       /// </summary>
       /// <param name="indexName">The name of the index.</param>
       void DeleteIndex(string indexName);
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
This Article Contains
New to Sitefinity?