Is it possible to trigger a Search Index update for a single file?

Kentico Dev asked on April 30, 2020 01:07

Our Kenitco 12 MVC project is using Azure Search.

We have widgets on some pages that we include in the search index. We do this via a Custom Search Module.

That works in that the widget content is include in the index, but we have to manually rebuild the index. If an editor modifies a widget on a page and saves/publishes, the index update task is not triggered. The task is only triggered of the editor updates something in the Content tab of the page.

What needs to be configured in order for Kentico to trigger the index update upon widget editing only?

I was considering hooking into a global event like DocumentEvents.GetContent.Execute but I am not sure how to programmatically trigger the index update task from here for the current page.

Correct Answer

Sean Wright answered on May 28, 2020 23:05

You can tell Kentico to update the search index value for a specific document via the SearchTaskAzureInfoProvider.SetSearchTaskAzureInfo method.

Here's an example call:

SearchTaskAzureInfoProvider.SetSearchTaskAzureInfo(new SearchTaskAzureInfo
{
    SearchTaskAzureType = SearchTaskTypeEnum.Update,
    SearchTaskAzureObjectType = TreeNode.OBJECT_TYPE,
    SearchTaskAzureInitiatorObjectID = document.DocumentID,

    // From Kentico source TreeNode.cs -> GetSearchID()
    SearchTaskAzureAdditionalData = $"{document.DocumentID};{document.NodeID}",
    SearchTaskAzureMetadata = SearchFieldsConstants.ID,
});

We use this to request index updates when related table columns are changed and the search index pulls data from those related tables for indexed documents.

This call will create an Azure Search task which will call your custom Azure Search module for the document specified.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on April 30, 2020 09:57

Hi,

When you customize content with DocumentEvents.GetContent.Execute event it should not prevent the system from sending update events for individual pages. It works for me on standard Dancing Goat website. So I see 2 potential issues here:

  • You haven't deployed dll with this customization to MVC website.
  • The problem is in the customization code. Could you post it here for more details?
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 30, 2020 18:44 (last edited on April 30, 2020 18:45)

I think it is more Azure search question. What you looking for is MERGE command (i.e. update an existing document with the specified fields). you basically need to send rest request wih JSON like

{
  "value": [
    {  
      "@search.action": "merge",  
      "Rating": 123,  
      "Description": "New Description",  
    }  
  ]  
} 

to you azure search (try it with postman first). You can dig in azure search api and try to google C# examples.

A couple ideas:

  • What I would first try to do is to "fake" the update of the content tab (read and save some value) and may be Kentico will do the job for you (hopefully it will update the whole document not a particular field).
  • The other way to approach this is to keep all the data in the content tab, so the editor doest touch the wigdet.
0 votesVote for this answer Mark as a Correct answer

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