Kentico: Example: Exclude documents based on a field value
In this article you’ll find:
Goal
The purpose of this article is to show you how to exclude certain documents from being sent to Hawksearch based on some field value. For the purpose of this tutorial, it will be assumed that you have inherited the Hawksearch.Kentico.Xperience.CMS.Services.Indexing.Indexer class and want to extend its behavior instead of writing your own Indexer from scratch.
Prerequisites
Created custom indexer - Kentico: Creating your own Indexing API Indexer
Steps to excluding documents from being indexed
Override the GetDocuments method and implement your custom logic. In the following snippet we will exclude all documents which have an “EventEndDate” field name with a value that is in the past, meaning that the event has already passed.
public override IEnumerable<SubmitDocument> GetDocuments(IEnumerable<FieldMappingInfo> fieldMappings, IEnumerable<string> identifiers = null) { var documents = base.GetDocuments(fieldMappings, identifiers); documents = documents.Where(a => !a.Fields.Any(b => b.Name == "EventEndDate" && DateTime.Now > DateTime.Parse(b.Values.First()))); return documents; }
2. That’s it. Now the indexer will exclude documents which have an event end date in the past.