Sitecore provides a Linq powered
IQueryable mechanism with which you can build powerful search queries. Your query will be translated into a native query for your underlying search engine (eg. Solr). There are some odd quirks (bugs?) with this translation in Sitecore 9.0 and 9.0.1 when your search term includes a space. Let’s take a look.
In the below examples, context is an instance of
IProviderSearchContext, which you’d typically wire up with dependency injection. In each case, we’re looking to query something from the index based the item’s path in the Sitecore tree.
Ok! This makes sense.
Notice that if your query term has a space, we need to wrap the term in quotes.
Backslash? No problem.
Forwardslash? We need to escape that with a
Backslash with space? No problem, just add the quotes.
As above, we’re all good, the forwardslash is just escaped.
Querying on partial matches – where things get interesting:
All good. Here, we wrap our search term in a wildcard,
Uh oh! Something weird has happened. The quotes and wildcard seem to have got mixed up, and we’ve ended up with something which won’t return the results we want. Having read more about wildcard / space combinations here , we probably want to end up with something simpler, like
No problem with this partial match, as we don’t have a space to deal with.
The space completely breaks everything here
I raised this with Sitecore and it has been raised as a bug. In the meantime – if you can get away with using StartsWith rather than Contains, you’ll find this works OK:
Which is just about perfect.