Modified Azure Search index give error on page update

Alexandre Poirier asked on May 4, 2020 21:30

I am currently using Azure Search to index pages and everything is working wonders. However, I would like to be able to sort by documentpublishfrom which is a kentico field and is not sortable in the created index. I have not found a way to make it sortable through the admin UI so I decided to try modifying it where I setup my Scoring Profile.

Here's my code:

 SearchServiceManager.CreatingOrUpdatingIndex.Execute += this.SetupIndex;

 private void SetupIndex(object sender, CreateOrUpdateIndexEventArgs e)
    {
        if (e.Index?.Name == ConfigurationManager.AppSettings["ArticlesIndexName"])
        {
            this.SetupSortField(e.Index);
            this.SetupScoringProfile(e.Index);
        }
    }

 private void SetupSortField(Microsoft.Azure.Search.Models.Index index)
    {
        var indexField = index.Fields.FirstOrDefault(f => f.Name == "documentpublishfrom");
        if (indexField != null)
        {
            indexField.IsSortable = true;
        }
    }

This works fine when rebuilding but when there is any other task in queue (like update) it fails stating : Index can not contain two search fields with the same name 'documentpublishfrom' but different properties. I tried to find an event for when the Update task is processed but there appears to be none.

So my question is why does Kentico appear to be modifying the index when running an update task but it doesn't run through the CreatingOrUpdatingIndex event and is there a way I could make it use my modified index?

Correct Answer

Dmitry Bastron answered on May 4, 2020 23:23

Hi Alexandre,

You were nearly there. Please refer to this blog article by Sean G. Wright: it walks through the example of making another Kentico system field (documentcategories) facetable (IsFacetable). So literally what you need to do is change the field name in the code example to documentpublishfrom and apply IsSortable instead.

1 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.