Creating a Scheduled Task to Rebuild a Custom Index

Andre Pfanz asked on November 28, 2016 22:24

We created a custom index that queries data from Hubspot. We would like for it to automatically rebuild every so often and initially thought the "Execute search tasks" task was going to do this but found it is currently not doing that.

I gathered from searching the only way to do this is to create a custom task to rebuild the index. I found examples of how to create a custom task but not how to create a task to rebuild a custom index. Does anyone have any guidance on how to do that or if there is another way to do what we need?

Correct Answer

Andre Pfanz answered on November 29, 2016 23:13

Nevermind. Here's what I found that works

SearchIndexInfo mediaLibraryIndex = SearchIndexInfoProvider.GetSearchIndexInfo("MediaLibraryIndex");
SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Rebuild, mediaLibraryIndex.IndexType, null, mediaLibraryIndex.IndexName, mediaLibraryIndex.IndexID);
0 votesVote for this answer Unmark Correct answer

Recent Answers

Brenden Kehren answered on November 28, 2016 22:33

Have you checked out the API documentation?

1 votesVote for this answer Mark as a Correct answer

Michal Samuhel answered on November 29, 2016 08:47

Hi Andre,

You are correct and Execute search tasks is not exactly the same as index rebuild. As is mentioned in the documentation on custom indexes, you need to declare and create rebuild method so all you need to do in the code of custom scheduled task is to use SearchIndexInfoProvider.GetSearchIndexInfo() to get the index object and call rebuild method of custom index object.

1 votesVote for this answer Mark as a Correct answer

Andre Pfanz answered on November 29, 2016 22:38

Thanks Michal. I created this method in the custom task but cannot find the Rebuild method:

public string Execute(TaskInfo ti)
    string detail = "Executed from '/App_Code/HG/CustomIndexRebuildTask.cs'. Task data:" + ti.TaskData;

    // Logs the execution of the task in the event log.
    EventLogProvider.LogInformation("CustomIndexRebuildTask", "Execute", detail);

    SearchIndexInfo documentLibraryIndex = SearchIndexInfoProvider.GetSearchIndexInfo("DocumentLibraryIndex");
    documentLibraryIndex. // Cannot find Rebuild method
    return null;
0 votesVote for this answer Mark as a Correct answer

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