1

Sitecore 9.3

I have a keyword search implemented and working fine until I use spaces in the keyword. As soon as I include space in a keyword it doesn't show me any results. Here is my query.

var keywordQuery = PredicateBuilder.True<SearchResultItem>();
keywordQuery = keywordQuery.Or(item => item.ShortTitle.Contains(keyword));
keywordQuery = keywordQuery.Or(item => item.LongTitle.Contains(keyword));

I have also gone through this article https://chrisperks.co/2018/02/12/sitecore-9-contentsearch-solr-query-quirks-with-spaces-and-wildcards/, discussed the same problem, and suggests to use "StartsWith" instead of "Contains" but it didn't help.

5
  • Can you try to replace the spaces by "@" and use == instead of contains?
    – adarsh
    Jun 12 '20 at 11:16
  • @adarsh will == search with a keyword? I don't want exactly word comparison but a field that contains the keyword even from the whole line. Jun 12 '20 at 11:23
  • When using the "@", Solr will interpret the keywords as a list of tokens to perform the query.
    – adarsh
    Jun 12 '20 at 11:35
  • But in that way, solr will also search the record with after space keyword and not the record having both keyword in single record. Jun 12 '20 at 11:54
  • 1
    I think that your fields ShortTitle/LongTitle are tokenized (splitted by whitespace as minimum) and because of this you cannot find keyword with whitespace.
    – x3mxray
    Jun 12 '20 at 11:55
1

This depends on a couple of factors like:

  1. Your ShortTitle and LongTitle fields configuration, what analyzer you used to index these fields.
  2. What is your target, do you want to match the whole keyword (with spaces), do you want to match single words? do you want to match all single words or any?

This is very useful link to understand the analyzers available: https://community.sitecore.net/technical_blogs/b/getting_to_know_sitecore/posts/understanding-analyzers-and-sitecore-7

Also, here is a trick I use for searching keyword that contain spaces:

var keywordSplitted = KeyWord.Split(' ');
var predicate = PredicateBuilder.True<CustomSearchResultItem>();

foreach (var word in keywordSplitted)
{
    predicate = predicate.And(item => item.LongTitle.Contains(word));
}
1
  • Yeah, this could be a quick fix. Jun 12 '20 at 14:09

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.