Richard Banks
Richard Banks

Reputation: 2983

Specifying which index to use in sitecore lucene

I have a website that was produced in sitecore that i have inherited. The search does not seem to be working correctly. Basically documents do not seem to be returned correctly. I have noted that there is the default sitecore_web_index index and also a custom index that seems to index the same content more or less. Currently the search queries the custom index however I would like to change the query to the default index to see if the document is then returned. I was told you can specify which index to use but the person never told me how to do it. Does anyone know how i can change this?

Upvotes: 1

Views: 293

Answers (3)

luckymow8
luckymow8

Reputation: 72

Had to do it this way ...

var indexable = (SitecoreIndexableItem)Sitecore.Context.Item;

Upvotes: 0

Jose Neto
Jose Neto

Reputation: 121

You also need to remember that on Content Manager environment, the "sitecore_master_index" will be used, and on CD Environment the "sitecore_web_index" will be used so this can lead to test errors

You can try to get the index dynamically, in that case, the code will select the correct index use, According to their environment

var indexable = Sitecore.Context.Item as SitecoreIndexableItem;

ISearchIndex index = ContentSearchManager.GetIndex(indexable);

using (IProviderSearchContext context = index.CreateSearchContext())
{
 //search code...
}

Upvotes: 0

Marek Musielak
Marek Musielak

Reputation: 27152

Sitecore 8 content search uses Sitecore.ContentSearch.ContentSearchManager.GetIndex(...) method to retrieve chosen index.

You can pass either:

  1. Index name as a string:
Sitecore.ContentSearch.ContentSearchManager.GetIndex("sitecore_web_index")
  1. IIndexable item - in this case Sitecore will try to find first registered index for you:
Sitecore.ContentSearch.ContentSearchManager.GetIndex(iIndexable)

Just find where GetIndex is used in your code and replace it with a default index name.

One thing you should be aware of - there is a chance that your custom index has some customization added (like computed fields, list of fields indexed etc). Be careful with any changes. Maybe there are other reasons why your search doesn't work. Try using IndexingManager app to rebuild the indexes and see if it helps.

Upvotes: 1

Related Questions