Mike Robbins

Sitecore Developer Blog

Follow me on GitHub

Sitecore 7 Add Single Item To Index

Recently I came up with a scenario where users were authoring content on the front end of a Sitecore site which then updated a list on the screen. The list was taking advantage of Sitecore 7's new search API, so i needed to rebuild the index to display the newly inserted item.

Rebuilding the whole index for a single new item would be excessive, slow, CPU intensive and can cause issues for other users loading the page. Using JustDecompile to look through the ContentSearch Namespaces i found these useful functions to add, update or delete  a single item in the index. This performs really fast and solves all my issues about rebuilding the whole index.

Here are my methods wrapping the functionality.

public static void AddItemToIndex(Item item, string indexName)
{
  var tempItem = (SitecoreIndexableItem)item;
  ContentSearchManager.GetIndex(indexName).Refresh(tempItem);
}

public static void UpdateItemInIndex(Item item, string indexName)
{
  var tempItem = (SitecoreIndexableItem)item;
  ContentSearchManager.GetIndex(indexName).Update(tempItem.UniqueId);
}

public static void DeleteItemInIndex(Item item, string indexName)
{
  var tempItem = (SitecoreIndexableItem)item;
  ContentSearchManager.GetIndex(indexName).Delete(tempItem.UniqueId);
}