I have Search API installed, using Solr. I created a view with an exposed Fulltext Search filter [see attached]. This works fine: search results are returned according to relevance to the search term.

However, when I add a Relevance sort [see attached], the results are no longer ordered according to relevance but rather just filtered by the search term, not ordered according to relevance.

Isn't this opposite of what should actually be happening? Adding the Fulltext Search filter should merely filter out content that does not contain the search term, whereas adding the Relevance sort is what should further sort the results according to relevancy.

The reason this is such a problem is because I cannot set a default fallback sort order [see attached] for when there is no search term (no search term would return all results, each with relevance of 1, therefore falling back to a second criterion, see "Authored On" in attached).

Comments

Anonymous’s picture

cgallucci created an issue. See original summary.

Anonymous’s picture

[duplicate comment removed]

Anonymous’s picture

Issue summary: View changes
criscom’s picture

Issue summary: View changes
StatusFileSize
new40.54 KB

I have the same problem. If in my view for the search results I configure the sort criteria as search: relevance (descending), the actual results are sorted randomly.

random sorting of search results

I found this patch: https://www.drupal.org/files/issues/added-correct-grouping.patch but this is for D7.

criscom’s picture

Issue summary: View changes
dermario’s picture

Assigned: Unassigned » dermario
Issue tags: +Dublin2016

I think i found the problem.

This condition in SolrHelper::addSorts() causes the problem, as the fieldname is search_api_relevance. That matches strpos($field_names[$field], 's') and $f becomes sort_search_api_relevance instead of sort.

      elseif ($new_schema_version && (strpos($field_names[$field], 't') === 0 || strpos($field_names[$field], 's') === 0)) {
        // For fulltext fields use the dedicated sort field for faster alpha
        // sorts. Use the same field for strings to sort on a normalized value.
        $f = 'sort_' . $field;
      }

Patch to follow very soon.

dermario’s picture

Assigned: dermario » Unassigned
Status: Active » Needs review
StatusFileSize
new687 bytes

This patch fixed it for me. I created a new condition, that catches the "sort-by-score" case before the fulltext sort condition is met.

Status: Needs review » Needs work

The last submitted patch, 7: views_relevance_sort-2800261-7.patch, failed testing.

dermario’s picture

Project: Search API » Search API Solr
Component: General code » Code
Status: Needs work » Needs review

Apparently this is a search_api_solr issue.

dermario’s picture

StatusFileSize
new687 bytes
Sumit kumar’s picture

Hi @dermario Thanks for contribution some coding thing to be change like variable name $f. Give some specific name.

Sumit kumar’s picture

Status: Needs review » Needs work
dermario’s picture

Status: Needs work » Needs review
StatusFileSize
new2.03 KB
new2.06 KB

Thank you @sumit-kumar, for the quick review. Although i am not sure, whether the variable naming of $f is the scope of this issue, i renamed it to $sort_field. Hope thats better now.

Sumit kumar’s picture

@dermario Thanks for quick response now its look great for me.

mkalkbrenner’s picture

StatusFileSize
new5.04 KB

You already discovered the right part of the code. Nevertheless I want to solve the issue slightly different and prepare some further cleanup of the code in the future.
Basically the the association between "search_api_relevance" and the corresponding pseudo Solr field "score" is already in place. But the lately added match for dynamic fields starting with an "s" caused an overwrite of that association.

dermario’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.24 MB
new1.24 MB

Looks good and works for me. The restructuring makes totally sense to me. As i introduced a kind of redundant code with my extra-condition. I applied the patch in #15, read and understood the changes and verified its correct functionality. I will put the issue to RTBC.

*Sort by asc:*

*Sort by dec:*

  • mkalkbrenner committed 884b6d8 on 8.x-1.x
    Issue #2800261 by dermario, mkalkbrenner, Sumit kumar: Views "Relevance...
mkalkbrenner’s picture

Status: Reviewed & tested by the community » Fixed

Just committed the patch. Thanks everyone.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

b_sharpe’s picture

Was this only for 8.x-2.x?

The issue + commit says 1.x, but I can't find this code in dev nor does the patch apply...

mkalkbrenner’s picture

The code exists in 1.x and 2.x! Search for the protected function setSorts.
If you have an issue with sorts, please open a new issue.