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).
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | sort-by-relevance-asc.png | 1.24 MB | dermario |
| #16 | sort-by-relevance-desc.png | 1.24 MB | dermario |
| #15 | 2800261.patch | 5.04 KB | mkalkbrenner |
| #13 | interdiff.txt | 2.06 KB | dermario |
| #13 | views_relevance_sort-2800261-13.patch | 2.03 KB | dermario |
Comments
Comment #1
Anonymous (not verified) commentedcgallucci created an issue. See original summary.
Comment #2
Anonymous (not verified) commented[duplicate comment removed]
Comment #3
Anonymous (not verified) commentedComment #4
criscomI 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.
I found this patch: https://www.drupal.org/files/issues/added-correct-grouping.patch but this is for D7.
Comment #5
criscomComment #6
dermarioI 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.
Patch to follow very soon.
Comment #7
dermarioThis patch fixed it for me. I created a new condition, that catches the "sort-by-score" case before the fulltext sort condition is met.
Comment #9
dermarioApparently this is a search_api_solr issue.
Comment #10
dermarioComment #11
Sumit kumar commentedHi @dermario Thanks for contribution some coding thing to be change like variable name
$f. Give some specific name.Comment #12
Sumit kumar commentedComment #13
dermarioThank 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.
Comment #14
Sumit kumar commented@dermario Thanks for quick response now its look great for me.
Comment #15
mkalkbrennerYou 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.
Comment #16
dermarioLooks 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:*

Comment #18
mkalkbrennerJust committed the patch. Thanks everyone.
Comment #20
b_sharpe commentedWas 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...
Comment #21
mkalkbrennerThe 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.