When implementing the content search for one of our projects we ran into the problem, that if we use the wildcard search in a specific field and only search for a phrase of the value no result was returned. We tried to find out what happened.
We have a custom string field “article_number” in our index for storing the article number for the products.
Let’s say the value of the article number field for the specific product we try to find is “IX 15 B 13-450”.
Our filter for the article number in the ContentSearch query looks like this:
x.ArticleNumber.Equals(keyword).Boost(90) || x.ArticleNumber.Contains(keyword).Boost(60)
If we now put as keyword the correct value of the field (“IX 15 B 13-450”) into our search, we find the result. But if we just put a phrase into it like “IX 15 B 13” we do not find it. Why?
Contains is described as Wildcardsearch and should return the correct result, that’s what we thought. But: ” Solr’s standard query parser supports single and multiple character wildcard searches within single terms. Wildcard characters can be applied to single terms, but not to search phrases.“
When we use the .Contains(keyword) part and search for a phrase, the following solr query is built:
(article_number_s:("IX 15 B 13"))^90 OR (article_number_s:("\*IX 15 B 13\*"))^60
(article_number_s:(\*IX 15 B 13\*) would be the query which works in solr.
So what to do? We tried to research a lot, asked in the Sitecore Slack Channel and opened a Support Ticket.
While waiting for the answer from support we got the hint to have a try at .MatchWildcard() in the Sitecore Slack Channel – so we did.
Instead of
x.ArticleNumber.Contains(keyword).Boost(60)
we now use
string wildcardKeyword = $"*{keyword}*".Replace(" ", @"\ ");
x.ArticleNumber.MatchWildcard(wildcardKeyword)
And with this part in C# we get the correct query in solr:
article_number_s:(*IX\ 15\ B\ 13*)
So, you can use MatchWildcard instead of Contains if you have problems with search phrases in string fields.
Sitecore Support registered a bug for the Contains (Reference number #206650 #177874). They also confirmed that using .MatchWildcard() is a workaround we can use.