10.4
  1. Configure indexing

Using the IndexCustodian class for index operations

We highly recommended that you use the IndexCustodian class for index operations instead of the ISearchIndex interface. The ISearchIndex interface contains the following API calls for index operations:

  • Rebuild
  • Update
  • Delete
  • Refresh

When you invoke these methods directly on the ISearchIndex interface, it can cause various issues ranging from missing log entries to application crash due to a StackOverflowException exception. Therefore, use these API calls from the IndexCustodian class. When you use the IndexCustodian class, the following things are different from the ISearchIndex interface:

  • The  IndexCustodian class runs the index operation in the context of a job. This ensures that the operation is atomic: no other rebuild or update operations can run for the same search index simultaneously.
  • The  IndexCustodian class triggers the indexing:start and indexing:end events. Sitecore relies on the events to keep instances in sync, and it also uses events to process some local changes.

To delete a specific document from a search index, for example, if the document is obsolete or when implementing some customization that deletes items in the context of an event disabler, execute Content Search API:

IndexCustodian.DeleteItem(ContentSearchManager.GetIndex("sitecore_web_index"), new SitecoreItemId(id);

or

IndexCustodian.DeleteItem(ContentSearchManager.GetIndex("sitecore_web_index"), new SitecoreItemUniqueId(itemUri));

Obsolete documents can also be removed from a search index using the index rebuild operation.

The 'Re-Index Tree' operation, also known as the Refresh operation, updates the search index based on the items that are still present in a Sitecore database. This operation does not delete any obsolete documents from a search index.

If you have suggestions for improving this article, let us know!