Upgraded from 7.2 to 8.1, Sort by Relevance in Lucene is not working

We recently upgraded from 7.2 to 8.1 Update 2. We experienced that Sort by Relevance in Lucene search is not working when we are using Contains, StartsWith, EndsWith, Equals, Like, and MatchWildcard operators while building the search querying _fullpath. 

Sample code:

string sitePath = “/sitecore/content/home”;
var results = SearchContext().GetQueryable<CustomResult>().Where(r => (r.Path.Contains(sitePath + “/”));
So if we have a look at the search logs after using Contains or any Wild Card search keyword, it adds SpanWildcardQuery keywords to the search query. Which basically ignore the Lucene Scroring, please have a look at the URL mentioned below:
Reason:
  • Sitecore.ContentSearch Linq layer
    • A number of Linq operators did not always return the expected results when passing a phrase as the parameter. This applied to the Contains, StartsWith, EndsWith, Equals, Like, and MatchWildcard operators. This has been fixed by making these operators use an analyzer and by making the operators create the correct multi-term query for phrase queries. As a result of this change, you might see a different set of results returned when using these operators. (389868, 396185, 396135)
    • The EndsWith() operator now requires the specified field to be stored in the index with termvector offsets. If the field is not stored with termvector offsets, a warning will be written to the log file, and the operator will return 0 results. (389868)
      Note: This fix was reworked in the 7.2 Initial Release. The EndsWith() operator now throws an exception if it is called for a field that is not stored in the index with termvector offsets (ref. no. 397998) . [Added March 11, 2014]

Please refer to the release notes at the URL mentioned below and search for (389868, 396185, 396135) :

https://sdn.sitecore.net/products/sitecore%20v5/sitecore%20cms%207/releasenotes/changelog.aspx

Solution:-

It’s a recommended way to query _path field instead of querying _fullpath. Hence the code above can be written as:-

var results = SearchContext().GetQueryable<CustomResult>().Where(r => (r.Paths.Contains(new Sitecore.Data.ID(“<<Item ID of the root Item>>”))

Hope this helps.

One thought on “Upgraded from 7.2 to 8.1, Sort by Relevance in Lucene is not working

  1. Pingback: Upgraded from 7.2 to 8.1, Sort by Relevance in Lucene is not working – Sitecore Competencies

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s