Sitecore Solr Linq Error RANGE_GOOP while using String CompareTo
Sitecore 9.0.2 with Solr setup, our code has custom ResultSearchItem model and is using Sitecore ContentSearch Linq query and one of the query is using string.CompareTo() method, something like below:
var query1 = PredicateBuilder.True<ProductSearchResultItem>();
query1 = query1.And(x => x.somestringfield.CompareTo(SomeValuesWithSpaces) <= 0);
The query works sometimes and below errors starts to appear in the logs:
8156 12:44:41 INFO Solr Query - ?q=((xxxxxxx:(xxxx) AND _template:(xxxxxxx)) AND ((-xxxxx:(xxxxxx) *:*) AND somestringfield:[Some values with spaces TO *]))&rows=2&fq=_indexname:(xxxxxxx)&wt=xml&sort=xxxxxxx asc
8156 12:44:41 ERROR Solr Error : ["org.apache.solr.search.SyntaxError: Cannot parse ‘((xxxxxxxxx:(xxxx) AND _template:(xxxxxxxxxxxxxxxx)) AND ((-xxxxx:(xxxxxxx) *:*) AND somestringfield:[Some values with spaces TO *]))': Encountered " <RANGE_GOOP> "Tier "" at line 1, column 138.
Was expecting:
"TO" ...
"] - Query attempted: [((xxxxxxxx:(xxxx) AND _template:(xxxxx)) AND ((-xxxx:(xxxxx) *:*) AND somestringfield:[Some values with spaces TO *]))]
Basically when value of SomeValuesWithSpaces contains space, since Sitecore doesn’t wrap the value in double quotes while translating the LINQ to Solr queries, it breaks the Solr query syntax.
My solution is to manually wrap the SomeValuesWithSpaces value with double quotes, it does resolve the issue however I am uncertain if this is the correct and most elegant solution. Will update if I found one, please let me know if you have one.