-1

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. ?

user3085317
  • 29
  • 2
  • 9

1 Answers1

0

The easy solution: You can just sort by inv first, then the other values. This requires that inv only have true (1) or false (0) values. I guess that is not the case, so:

You can sort by a function query - and you can use the function if to return different values based on whether the value is set or not:

sort=if(inv, 1, 0) desc, name desc

If Solr fails to resolve inv by itself, you can use field(inv), but it shouldn't be necessary.

Another option is to use the function min to get either 1 or 0 for the sort field, depending on whether it's in inventory or not:

sort=min(1, inv)
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Thanks. But i dont want to sort by inv (inventory can have values from 0 to 1000). i just want to sort by name but have all records that have value 0 at the end .is the map function usable here. how can i use it – user3085317 Oct 13 '16 at 19:28
  • "Having all the records that have value 0 at the end" is by default sorting. By using the `if` function query as illustrated (or the min version), the only values will be either 0 or 1 for sorting. The next sort field will then give the direction within that sort again. Please give an example if this does not give the correct result as you expect. Using map should not be necessary (but would be possible), but you'd still have to sort by the value to get the documents to come last. – MatsLindh Oct 13 '16 at 20:32
  • Hi, I tried &sort=min(1, field(inv)) desc, name_ntk desc it did not work. – user3085317 Oct 14 '16 at 15:58
  • I tried below 1.sort=name desc, if(exists(query({!v='inv:0'})), 1, 0) desc also – user3085317 Oct 14 '16 at 16:01
  • for some reason , when i press enter, comment closes and i can't edit the comment here on stackoverflow. Ok ..sorry about that. i also tried sort=if(inv, 1, 0) desc, name desc also tried &sort=map(inv,0,0,inv,0),name desc ...nothing seems to work. i am new to solr terminology. could you give me the entire sort query string please ? – user3085317 Oct 14 '16 at 16:05