2
votes

I am using new Sitecore search, and the issue I ran into is having results come for words that have nothing to do with my search term.

For example, searching for "lies" will find "applies". And this is not what I am looking for.

This is an example of search I am doing (simplified). It is a direct LINQ check for "Contains" on the "Content" property of the SearchResultItem, and most likely not what I supposed to do. It is just happen to be that samples I find online are practically doing so.

Example of my code (simplified). In here I break down the search sentence to separate keywords. By the way, I am looking for a way to show full sentence match first.

        using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
        {
            var results = context.GetQueryable<SearchResultItem>()
                .Filter(i => i.Path.StartsWith(Home.Paths.FullPath))
                .Filter(GetTermPredicate(Term));

           // use results here
        }



    protected Expression<Func<SearchResultItem, bool>> GetTermPredicate(string term)
    {
        var predicate = PredicateBuilder.True<SearchResultItem>();
        foreach (var tempTerm in term.Split(' '))
        {
            predicate = predicate.And(p => p.Content.Contains(tempTerm));
        }
        return predicate;
    }

Thank you in advance!!

4

4 Answers

1
votes

Replace Filter in your code by Where, it should be fine, here is an example :

    var currentIndex = ContentSearchManager.GetIndex("sitecore_web_index"); 
using (var context = currentIndex.CreateSearchContext()) 
{ 
  var predicate = PredicateBuilder.True(); 
  foreach (var currentWord in term.Split(‘ ‘)) 
  { 
      predicate = predicate.Or(x => x.Content.Contains(currentWord )); 
  } 
  var results = context.GetQueryable().Where(predicate).GetResults(); 
}
1
votes

All right. I got help from Sitecore Support.

In my version of Sitecore I can use the following to acheive search for a whole word instead of partial:

instead of:

predicate = predicate.And(p => p.Content.Contains(tempTerm));

use

predicate = predicate.And(p => p.Content.Equals(tempTerm));

Issue solved.

0
votes

As Ahmed notes, you should use Where instead of Filter, since Filter has no effect on search rank. The classic use case for filters is to apply a facet chosen by the user without distorting the ordering of results, as would happen if you used a Where clause.

Filtering is similar to using Where to restrict the result list. Both methods will affect the result in the same result list, but when you use a Filter the scoring/ranking of the search hits is not affected by the filters. Developer's Guide to Item Buckets and Search

There's a good dicussion of when to use Filter on the Sitecore 7 team blog: Making Good Part 4: Filters, Paging and I'm feeling lucky.

-1
votes

If you only want to search for whole words, you could prefix and postfix the searchterm with a space. Allthough this doesn't catch all situations.