Monday, 29 December 2014

Sitecore Content Search - MatchNoDocsQuery

Yesterday I spent way too long trying to solve a problem with Sitecore Content Search.  It was a simple enough query, but strangely it worked in some cases but not others.

I decided to look at the Search logs to see the exact query that was being produced. On successful occasions, it looked like this:

ExecuteQueryAgainstLucene (my_index): *:* - Filter : +(+(+region:eu +country:gb) +_language:en-gb)

On unsuccessful occasions, it looked like this:

ExecuteQueryAgainstLucene (my_index): *:* - Filter : +(+(+region:eu +MatchNoDocsQuery[]) +_language:fr-be)

I Googled "MatchNoDocsQuery" and found that it's used to ensure a query never returns any results. That's certainly not what I coded. The offending predicate was simply matching a country code (a string that I knew would never be null or empty). 

The answer turned out to be blindingly simple - stop words. 

Some country codes are also stop words, for example "BE" for Belgium and "IT" for Italy. When you perform a search, Sitecore obviously strips the stop words. In my case that resulted in the entire predicate being converted to +MatchNoDocsQuery[]

To rectify the problem I used Brent Svac's method of Turning off the stop words filter.

I found this all quite confusing. I never would have expected Sitecore to produce such a query from what I was supplying, but I accept it's an edge case. Hopefully if you ever see MatchNoDocsQuery appearing in your queries, you'll now immediately know what's going on.