Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Customize search

Describes how to customize and extend the search in Optimizely Commerce Connect by adding index fields and creating new search indexers for specific purposes.

Classes in this topic are available in the following namespaces:

  • EPiServer.DataAnnotations
  • Mediachase.Search
  • Mediachase.Search.Extensions
  • Mediachase.Search.Extensions.Indexers

See also Create your own search provider.

Add index fields

The process of adding a field can differ depending on the provider and where the data comes from. The following steps add a field to the catalog index using the Lucene provider.

When using Lucene, decorate the property on the catalog content model with the [Searchable] attribute to make the field searchable.

To add a field programmatically if data comes from an external data source or needs to be computed, create a class that inherits the existing CatalogIndexer.

public class MyCatalogIndexer : Mediachase.Search.Extensions.Indexers.CatalogIndexBuilder
{
    /// <summary>
    /// Called when catalog entry is indexed. Override this method to add some extra fields.
    /// </summary>
    /// <param name="document">The document.</param>
    /// <param name="entry">The entry.</param>
    /// <param name="language"></param>
    protected override void OnCatalogEntryIndex(ref SearchDocument document,
        CatalogEntryDto.CatalogEntryRow entry, string language)
    {
        // Example, add all apple products as featured
        if (entry.Name.Contains("apple"))
            document.Add(new SearchField("featured", true));

        base.OnCatalogEntryIndex(ref document, entry, language);
    }
}

Create a search indexer

A search indexer is a component that assembles data into documents the search provider can index. It typically connects subsystems such as catalogs, gets a list of changes, and submits the changes to the search provider.

By default, Optimizely Commerce Connect indexes catalog data using the BaseCatalogIndexBuilder class or one of its inheriting classes. However, third-party implementations may implement ISearchIndexBuilder instead of extending BaseCatalogIndexBuilder.

/// <summary>
/// Search Index Builder Interface.
/// </summary>
public interface ISearchIndexBuilder
{
    /// <summary>
    /// An event handler for progress message events.
    /// </summary>
    event SearchIndexHandler SearchIndexMessage;

    /// <summary>
    /// Gets or sets the manager.
    /// </summary>
    /// <value>The manager.</value>
    SearchManager Manager { get; set; }

    /// <summary>
    /// Gets or sets the build indexer.
    /// </summary>
    /// <value>The build indexer.</value>
    IndexBuilder Indexer { get; set; }

    /// <summary>
    /// Builds the index.
    /// </summary>
    /// <param name="rebuild">if set to <c>true</c> the full rebuild will be done, if not. The last build date will be used.</param>
    void BuildIndex(bool rebuild);

    /// <summary>
    /// Updates the specified items in the index.
    /// </summary>
    /// <param name="itemIds">The ids of the items to update.</param>
    /// <returns>True if successful; otherwise, false.</returns>
    bool UpdateIndex(IEnumerable<int> itemIds);
}

The updated catalog event system uses the UpdateIndex method and calls it for any changed or deleted catalog entries. The parameter enumerates catalog entry IDs of changed or deleted entries. The implementation is expected to update the search indexes appropriately for these entries.

If a transient error occurs (such as a network error when accessing a remote service), UpdateIndex should return false. The system may then retry the same batch of updates later. If a non-transient error occurs, the implementation should throw an exception.

An ISearchIndexBuilder implementation should throw an exception from UpdateIndex if the updated event system is not supported and scheduled tasks execute BuildIndex.