HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Create your own search provider

Describes how to extend your Optimizely Commerce (PaaS) solution by creating your own search provider. The examples in this topic are based on Lucene.

Classes in this topic are available in the following namespaces:

  • Mediachase.Search
  • Mediachase.Search.Extensions
  • Mediachase.Search.Extensions.Indexers

See also Customize search.

To implement a new search provider or extend or replace an existing one, create a new class that implements the SearchProvider abstract class. That class contains one method that performs the search, while the others deal with indexing the data. It is typically much easier to take the existing implementation (Lucene) as a starting point.

public abstract class SearchProvider : ProviderBase
        {
            /// <summary>
            /// Gets the class type of the query builder. This class will be used to dynamically convert SearchCriteria to the query
            /// that Search Provider can understand.
            /// </summary>
            /// <example>
            /// // the following type will build query for the SOLR server
            /// "Mediachase.Search.Providers.Solr.SolrSearchQueryBuilder, Mediachase.Search.SolrSearchProvider"
            /// </example>
            /// <value>The type of the query builder.</value>
            public abstract string QueryBuilderType { get; }
    
            /// <summary>
            /// Searches the datasource using the specified criteria. Criteria is parsed by the query builder specified by <typeparamref name="QueryBuilderType"/>.
            /// </summary>
            /// <param name="applicationName">Name of the application.</param>
            /// <param name="criteria">The criteria.</param>
            /// <returns></returns>
            public abstract ISearchResults Search(string applicationName, ISearchCriteria criteria);
    
            /// <summary>
            /// Adds the document to the index. Depending on the provider, the document will be commited only after commit is called.
            /// </summary>
            /// <param name="applicationName">Name of the application.</param>
            /// <param name="scope">The scope.</param>
            /// <param name="document">The document.</param>
            public abstract void Index(string applicationName, string scope, ISearchDocument document);
    
             /// <summary>
             /// Removes the document by specifying scope (core in SOLR), key (a field that can be used to lookup for a document) and
             /// value of the key.
             /// </summary>
             /// <param name="applicationName">Name of the application.</param>
             /// <param name="scope">The scope.</param>
             /// <param name="key">The key.</param>
             /// <param name="value">The value.</param>
             /// <returns></returns>
             public abstract int Remove(string applicationName, string scope, string key, string value);
    
             /// <summary>
             /// Removes all documents in the specified scope.
             /// </summary>
             /// <param name="applicationName">Name of the application.</param>
             /// <param name="scope">The scope.</param>
             public abstract void RemoveAll(string applicationName, string scope);
    
             /// <summary>
             /// Closes the specified provider.
             /// </summary>
             /// <param name="applicationName">Name of the application.</param>
             /// <param name="scope">The scope.</param>
             public abstract void Close(string applicationName, string scope);
    
             /// <summary>
             /// Commits changes made to this instance.
             /// </summary>
             /// <param name="applicationName">Name of the application.</param>
             public abstract void Commit(string applicationName);
        }

The main classes in this Lucene implementation example are:

  • LuceneSearchProvider. The main class.
  • LuceneSearchQueryBuilder. Converts ISearchCriteria to the provider's native language and is called within the Search method of the provider.