indexing services - Philo/Umbraco.Elasticsearch GitHub Wiki

An indexing service is a pivotal piece of the Umbraco.Elasticsearch puzzle. As a key aim is to put you (the application developer) in total control of how your index to created, built and searched it is necessary that Umbraco.Elasticsearch calls out to you when it is iterating through nodes to index.

An index service describes the relationship between an Umbraco document type (Content Type) and an Elasticsearch document model. It provides us with the logic to determine whether a particular node should be indexed, and if so, how it is indexed.

There are 2 specific types of indexing services, one is defined for dealing with content nodes, the other specific to media nodes, while they both share very similar functions, they work on different underlying Umbraco types (IContent and IMedia).

It is the role of you to create indexing services for each of your content and media types you wish to index, each type (content or media) requires the following to be created:

  • An indexing service implementing either IContentIndexService or MediaIndexService. (I would recommend deriving from ContentIndexService and MediaIndexService instead of the interfaces)
  • A POCO class describing the Elasticsearch document implementing the IUmbracoDocument interface

Lets show an example:

ArticleContentIndexService

Create the POCO and decorate it with a NEST attribute to determine what document type it relates to, in this case dtArticle is the document type created within Umbraco.

[ElasticsearchType(Name = "dtArticle", IdProperty = "Id")]
public class ArticleDocument : IUmbracoDocument
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Summary { get; set; }
    public string Url { get; set; }
}

Then create your index service and implement the Create method to supply your custom logic that describes how to transform a dtArticle node into a ArticleDocument for indexing. The Id and Url properties are set automatically but can be customised via UrlFor and IdFor overrides.

public class ArticleContentIndexService : ContentIndexService<ArticleDocument>
{
    protected override void Create(ArticleDocument doc, IContent content)
    {
        // from here you can implement your own logic to convert the IContent node into a document model
        doc.Title = content.Name;
        doc.Summary = content.GetValue<string>("summary");
    }

    public ArticleContentIndexService(IElasticClient client, UmbracoContext umbracoContext) : base(client, umbracoContext)
    {
    }
}

This indexing service can now be registered within the SearchApplicationEventHandler.

Excluding nodes from the index

By default each indexing service will use the default node exclusion logic for each node as it passes through:

  1. Read the value of the ExcludeFromIndexPropertyAlias configuration setting, this will be a property alias.
  2. if the node does not have a property with that alias, the node is indexed
  • if the node does have a property with that alias, read the property value, if True then do not index

Custom node exclusion logic

It is possible to provide custom logic to the indexing service to determine whether a particular node should be indexed or not. This is done by overriding the IsExcludedFromIndex method.

public virtual bool IsExcludedFromIndex(IContent entity)
{
  return entity.PublishDate < DateTime.UtcNow.Subtract(TimeSpan.FromHours(24)); 
}

Advanced Indexing services

Advanced indexing service features are explained in a separate section

⚠️ **GitHub.com Fallback** ⚠️