API
Version 7.x > API > Update smart index after edit, delete, insert document via API View modes: 
User avatar
Member
Member
Peter - 5/30/2013 12:01:36 AM
   
Update smart index after edit, delete, insert document via API
Hi,

Is there a way to update existing index in code after I've inserted, deleted or updated a document through the code?

I understand there is a rebuild on every index but was wondering if there is a method to call which would just update the index.

Thanks for any help
Peter

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 5/30/2013 8:48:43 AM
   
RE:Update smart index after edit, delete, insert document via API
This should get you what you need.
/// <summary>
/// Rebuilds an index based on the index name
/// </summary>
/// <param name="indexName"></param>
/// <returns></returns>
private bool RebuildIndex(string indexName)
{
// Get the search index
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo(indexName);
if (index != null)
{
// Create rebuild task
SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Rebuild, index.IndexType, null, index.IndexName);
return true;
}
return false;
}

User avatar
Member
Member
Peter - 5/30/2013 4:59:21 PM
   
RE:Update smart index after edit, delete, insert document via API
Thanks for that

I actually got the following and it works. Unfortunatelly it is not imidiate update of the index I would like.

SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Update, PredefinedObjectType.DOCUMENT, SearchHelper.ID_FIELD, node.GetSearchID());

Regards

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 6/2/2013 5:03:44 AM
   
RE:Update smart index after edit, delete, insert document via API
Hello Peter,

You can execute the same code, which is executed when you rebuild smart search index manually in Site Manager -> Administration -> Smart search -> edit -> General tab -> Rebuild link button. This code can be found in <project folder>\CMSModules\SmartSearch\Controls\UI\SearchIndex_General.ascx.cs.

At first, you need to create SmartSearchInfo object:
protected SearchIndexInfo sii = null; //(around line 26)

And assign proper index to it:
// Get search index info //(around line 65)
sii = SearchIndexInfoProvider.GetSearchIndexInfo(this.ItemID);
{
// see code inside
}

If you know ID of selected index, you can use it directly.

Then you can rebuild index using this code:
if (sii != null)
{
SearchTaskInfoProvider.CreateTask(SearchTaskTypeEnum.Rebuild, sii.IndexType, null, sii.IndexName);
}

lblInfo.Text = ResHelper.GetString("srch.index.rebuildstarted");
lblInfo.Visible = true;

// Reload info panel
System.Threading.Thread.Sleep(100);
ReloadInfoPanel();
}

Please take an inspiration from file above for more details.

Best regards,
Martin Danko

User avatar
Member
Member
Peter - 6/2/2013 6:14:36 PM
   
RE:Update smart index after edit, delete, insert document via API
Thanks Martin,

I was using the rebuild method, however I don't want to rebuild the whole index if I update 1 item. The following code is what I ended up with.
    private void UpdateSearchIndex(CMS.DocumentEngine.TreeNode node, SearchTaskTypeEnum type)
{
SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("EWGSiteExperts");
Lucene.Net.Documents.Document iDoc = node.GetSearchDocument(index as ISearchIndexInfo);

if (type == SearchTaskTypeEnum.Update)
{
SearchHelper.Update(iDoc, index);
}
else if (type == SearchTaskTypeEnum.Delete)
{
SearchTaskInfo sti = new SearchTaskInfo();
sti.SearchTaskType = type;
sti.SearchTaskField = SearchHelper.ID_FIELD;
SearchHelper.Delete(sti.SearchTaskField, node.GetSearchID(), index);
}
}

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 6/3/2013 5:35:41 AM
   
RE:Update smart index after edit, delete, insert document via API
Hi Peter,

Yes... now I see your point, thanks for sharing!

Best regards,
Martin Danko