2

I am trying to move my code from Lucene to Solr, but am facing an issue. I have 4 fields indexed, all the field are of text_general type (from the managed schema). The code from Lucene is:

filterSearchText = filterSearchText.Or(x => x.LastName.Contains(searchRequest.Keyword));
filterSearchText = filterSearchText.Or(x => x.Content.Contains(searchRequest.Keyword));
filterSearchText = filterSearchText.Or(x => x.RelatedContent.Contains(searchRequest.Keyword));
filterSearchText = filterSearchText.Or(x => x.SearchKeywords.Contains(searchRequest.Keyword));

So whenever I search a single word (e.g. "health") Solr gives the same results as Lucene, but when I search multiple words (e.g. "health ser") it does not give any result. The query is in the logs as follows for single and multiple words:

last_name_t:(*health*) OR _content:(*health*) OR relatedcontent_s:(*health*) OR search_keywords_t:(*health*)

and for multiple words

last_name_t:("\*health ins\*") OR _content:("\*health ins\*") OR relatedcontent_s:("\*health ins\*") OR search_keywords_t:("\*health ins\*")

but when I search for the full keywords such as (e.g. "health services") I get correct results with the query:

last_name_t:("\*Health Services\*") OR _content:("\*Health Services\*") OR relatedcontent_s:("\*Health Services\*") OR search_keywords_t:("\*Health Services\*")

So what is the replacement for finding the substring in Solr?

4
  • What is the return type for your fields? Return type should be "Text". Commented Apr 29, 2019 at 10:21
  • text is a System.string type (form the default solr conf ) file , and my field are also having richtext and other types , so converting type to Text will not impact on the content inside the fields ? .. btw i`ll try and will get back to this Commented Apr 29, 2019 at 10:35
  • no converting the field to text from text_general didn`t worked Commented Apr 29, 2019 at 12:21
  • converting type to "text" will not impact the content inside the fields. Commented Apr 30, 2019 at 10:06

2 Answers 2

2

I faced the same issue, then I replaced

contains

with

MatchWildcard

and it worked fined for me. Update your code syntax as below:

filterSearchText = filterSearchText.Or(x => x.LastName.MatchWildcard(searchRequest.Keyword));

1
  • 1
    This worked for me like a charm. Commented May 11, 2021 at 17:38
1

I have faced with the same issue some time ago. As it is mentioned in documentation, Solr does not support the wildcard (say partial) query for search phrases (this is a type of query to which .Contains("simple site") will be converted):

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. https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html

When you try to search health, your query is parsed as <str name="parsed query">content:health </str> and Solr searches for health as a part of word or an entire word and it works fine. But when you try to search health ins, your query is parsed as <str name="parsedquery">PhraseQuery(_content:"health ins") </str>. In this case, Solr searches of containing the health and ins just as entire words in the document and you don't get any results, because you don't have a document with ins word in the content.

The easiest way to supporting multi word search with search by the part of word is to apply the code like below:

using (var searchContext = ContentSearchManager.GetIndex(new SitecoreIndexableItem(startItem)).CreateSearchContext()) 
{

var querySplitted = queryItem.Split(' '); //Split the queryItem by the white space (or any other symbols if you need) 

var predicate = PredicateBuilder.True <TIndexModel>(); 

foreach (var query in querySplitted) 
{ 
    predicate  = predicate.And(item => item.Content.Contains(query)); 
} 

var query = searchContext.GetQueryable<SearchResultItem>().Filter(predicate);
}

The code above works as expect.

Another way is to change the tokenizer. But in case you decide to change the analyzer, be careful as the size of index will increase and potentially can cause a performance issue. Make sure you know what you're doing before changing of analyzer.

I have explained how the analyzer changing might help you with the issue in the following article: https://www.brimit.com/blog/sitecore-multiple-partial-word-search-solr

1
  • @Rohan Is the answer helped? Or did you maybe solved it in some other way? Commented Sep 4, 2019 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.