3

I have a search functionality in my project and the code for exactly matched keywords and search all word, the exactly is working fine but not the search all words:

if (!searchRequest.SearchType.IsNullOrEmptyTrimmed() && searchRequest.SearchType.Equals("exactly"))
{
    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));
}
else
{
    var searchExpression = string.Format("{0}{1}{0}", "*", searchRequest.Keyword.Replace(" ", "*").Replace("-", "*"));
    filterSearchText = filterSearchText.Or(x => x.LastName.MatchWildcard(searchExpression));
    filterSearchText = filterSearchText.Or(x => x.Content.MatchWildcard(searchExpression));
    filterSearchText = filterSearchText.Or(x => x.RelatedContent.MatchWildcard(searchExpression));
    filterSearchText = filterSearchText.Or(x => x.SearchKeywords.MatchWildcard(searchExpression));
}
4
  • 1
    So when you search for multiple keywords, your query is MatchWildcard("*keyword1*keyword2*keyword3*")? – Marek Musielak Mar 27 '19 at 12:55
  • but my fields are text_general type, so tokenization of data happens , and each word is searched separately – Rohan Bhateja Apr 29 '19 at 10:03
  • Because of your field is tokenized, each word is stored separately. Example: text "word1 word2" is stored like "word1" and "word2", and of course it cann`t find wildcard phrase "word1*word2" in "word1" and "word2". – x3mxray Apr 29 '20 at 16:44
  • this is not even about search query, you need to apply proper tokenizers for this field in Solr first, may be to build n-grams, or pairs of words, or some custom splitting. And remove WhitespaceTokenizer that cuts you text to words. see solr.apache.org/guide/6_6/tokenizers.html for example – Daniil Mar 7 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.