/
Kentico: Example: Exclude documents based on a field value

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

Steps to excluding documents from being indexed

  1. 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.

Related content

Kentico: Example: Exclude documents based on a column value
Kentico: Example: Exclude documents based on a column value
More like this
Kentico: Example: Adding additional information to a document
Kentico: Example: Adding additional information to a document
More like this
Indexing operations
Indexing operations
More like this
Filtering results based on a condition
Filtering results based on a condition
More like this
Kentico: Creating your own Indexing API Indexer
Kentico: Creating your own Indexing API Indexer
More like this
How to index DateTime fields
How to index DateTime fields
More like this