solr boost query with separate sort

天大地大妈咪最大 提交于 2019-12-04 06:38:15

问题


I want to demote all documents that have inv=0(possible values from 0 to 1000) to the end of the result set. i have got other sorting options like name desc also as part of the query.

For example below are my solr documents

Doc1 : name=apple , Inv=2
Doc2 : name=ball , Inv=1
Doc3 : name=cat , Inv=0
Doc4 : name=dog , Inv=0
Doc5 : name=fish , Inv=4
Doc6 : name=Goat , Inv=5

I want achieve below sorting ...here, i want to push all documents with inv=0 down to bottom and then apply "name asc" sorting.

Doc1
Doc2
Doc5
Doc6
Doc3
Doc4

my solr request is like

bq: "(: AND -inv:"0")^999.0" & defType: "edismax"

here 999 is the rank that i gave to demote results.

this boosting query works fine. it moves all documents with inv=0 down to the bottom.

But when i add &sort=name asc to the solr query, it prioritizes "sort" over bq..i am seeing below results with "name asc".

Doc1 : name=apple , Inv=2
Doc2 : name=ball , Inv=1
Doc3 : name=cat , Inv=0
Doc4 : name=dog , Inv=0
Doc5 : name=fish , Inv=4
Doc6 : name=Goat , Inv=5

can anyone please help me out. ?


回答1:


Sort will override the boost.

So, you either move your sort into boost by making that condition map into boost values.

Or you move your boost condition into sort, using query() syntax. This was one of the gems from the Lucene/Solr Revolution 2016 presentation by hoss (click start presentation):

        qq = Harry
         q = +{!edismax v=$qq}
        qf = title actor writer director  keywords
      sort = query($title_sort,0) desc, title asc
title_sort = {!field f=title v=$qq}



回答2:


Default sorting criteria in Solr is score desc, where score is a virtual field and it actually represents the document's score.
Once one is passing &sort=name asc it will override default sorting.

Possible solution here might be something like this: &sort=score desc, name asc. Which literally means: please sort by score first and for documents with equal score please make a tie-break by name ascending.

It should work as long as you will have equal scores for doc1, doc2, doc5, doc6.

If it is not the case - then check out this Solr Wiki link for more details how to penalize docs with inv:0.



来源:https://stackoverflow.com/questions/40495998/solr-boost-query-with-separate-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!