Mark's Sitecore Blog

Determining the Context Index when performing bucket queries

We have faced a problem when working with multilists and custom indexes. Some of our multilists would not contain any data even though the lucene queries would have matches when querying the index directly. We found that the problem had to do with the way Sitecore determines the index to perform the bucket query on.

Reason

When working with custom indexes in the Backend (i.E. bucket queries, multilist with search,…), Sitecore has a method to determine which index is most appropriate to use for the base item (i.E. the bucket container) you’re querying on.

In version 7.2, basically, Sitecore checks, which indexes are defined for the context database and then checks if the base item is included in the respective index by checking include/exclude templates. The first matching index is used. It is important to know that the root paths configured on the crawlers don’t have any affect on this. That means in some cases an index may be selected that doesn’t actually contain any information about the data i’m querying. I bug imo.

Workaround

To work around this you have the following options:

a) Index config files are read in alphabetical order. You might be able to move around your custom indexes to let Sitecore select the appropriate index.
b) Hook into the contentSearch.getContextIndex pipeline and implement your own logic.

Since a) is pretty straighforward but might not work for all scenarios, I’ll show an example of b):

I wrote a simple hook that allows me to read the name of a custom index from a field on the base item (i.E. the item bucket base item):This allows me to use a field named CustomSearchIndex to optionally set the index name used for querying.

AppliedIndexChecker admin page

To check which context index is resolved on specific items, i have implemented a simple admin page that allows me to resolve the index based on a GUID or an item path.

Summary

This workaround does the job, but it would of course be nicer to have Sitecore correctly resolving context indexes. Also it would be good to be able to set the index directly in the multilist / bucket configuration. I hope there will be an improvement here in future versions.

TwitterGoogle+Facebook 

Leave a Reply

Your email address will not be published. Required fields are marked *