11

A new index was added to a Sitecore instance I am working on. This ended up causing a different module in the implementation to have a problem. The part having an issue uses Sitecore.ContentSearch.ContentSearchManager.GetIndex(IIndexable indexable) to get an index to use passing in the current item cast appropriately. To fix the issue, I switched from passing in the current item to passing in the item that represents the homepage of the current site. This did the trick, since the homepage item was outside of the other index.

I don't understand exactly how the index to use is determined.

How does Sitecore decide what index to use when passing in an item, if that item is in multiple indexes?

  • I'm not sure how Sitecore determines the index it returns. However, if you know the name of the index you can use the Sitecore.ContentSearch.ContentSearchManager.GetIndex(string name) method to get the proper index. – Sebastian Lay Jan 31 '18 at 16:30
  • 1
    @SebastianLay yeah that's definitely a valid option but not really what I'm asking about. – Teeknow Jan 31 '18 at 19:26
16

When you call Sitecore.ContentSearch.ContentSearchManager.GetIndex(IIndexable indexable), it runs contentSearch.getContextIndex pipeline.

In the version I'm checking now (it's 8.2 update 3), this pipeline has 1 processor only: FetchIndex.

What it does, it takes all the indexex in the order in which they are defined in the config files (see /sitecore/admin/showconfig.aspx for the order) an for each of them it checks things like:

  • Does any of the ancestors of the item is the root of the index provider
  • Does the database of the item is same as defined for the index
  • Is item excluded from the index

When it finally has all indexes which are valid for the current item, it runs int SitecoreItemCrawler.GetContextIndexRanking(IIndexable indexable) method. Default implementation returns difference between the index root item level and the indexable item level, so the closer to the index root the better. If there is only 1 index or if the best score from SitecoreItemCrawler.GetContextIndexRanking method is better then the score of the second index, we have the winner.

If there are 2 or more indexes with same lowest score, Sitecore checks ContentSearch.DefaultIndexType setting (for Lucene it's Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider) and returns first index of that type in alphabetical order.

If there is no index of type matching ContentSearch.DefaultIndexType, first index of those with lowest score is selected.

There may be some other checks in the other Sitecore versions, but the logic will be similar.


So back to your scenario - when you were passing current item to the GetIndex method, the other index was winning cause

  • either that item was "closer" to the other index root than to your chosen index root
  • or it was on the same level in the other index and in your index, but your index type is not same as in the ContentSearch.DefaultIndexType setting
  • or both your and the other index type matches the ContentSearch.DefaultIndexType setting type but the other index name is alphabetically "lower" than your index name
  • or both index types don't match ContentSearch.DefaultIndexType setting type but the other index is just defined earlier in the config
  • or ... well I guess there is some scenario I couldn't think of now :)
  • Nailed it with your first assumption about what was happening in my scenario. Great answer. Thanks! – Teeknow Feb 1 '18 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.