Mun
Mun

Reputation: 14308

Conditional sorting in Solr 3.6

We're running Solr 3.6 and are trying to apply a conditional sort on the result set. To clarify, the data is a set of bids, and we want to add the option to sort by the current user's bid, so it can't function as a regular sort (as the bid will be different for each user that runs the query).

The documents in the result set include a "CurrentUserId" and "CurrentBid" field, so I think we need something like the following to sort:

sort=((CurrentUserId = 12345) ? CurrentBid : 0) desc

This is just pseudocode, but the idea is that if the currentUserId in Solr matches the user Id (12345 in this example), then sort by CurrentBid, otherwise, just use 0.

It seems like doing a sort by query might be the way to go with achieving this (or at least form part of the solution), using something like the following query:

http://localhost:8080/solr/select/?q=:&sort=query(CurrentUserId:10330 AND CurrentBid:[1 TO *])+desc

This doesn't seem to be working for me though, and results in the following error:

sort param could not be parsed as a query, and is not a field that exists in the index: ...

The Solr documentation indicates that the query function can be used as a sort parameter from Solr 1.4 onwards, so this seems like it should work.

Any advice on how to go about achieving this would be greatly appreciated.

Upvotes: 2

Views: 2253

Answers (1)

Paige Cook
Paige Cook

Reputation: 22555

According to the Solr Documentation link you provided,

Any type of subquery is supported through either parameter dereferencing $otherparam or direct specification of the query string in the LocalParams via "v".

So based on the examples and your query, I think one or both of the following should work:

http://localhost:8080/solr/select/?q=:&sort=query($qq)+desc&qq=(CurrentUserId:10330 AND CurrentBid:[1 TO *])

http://localhost:8080/solr/select/?q=:&sort=query({v='CurrentUserId:10330 AND CurrentBid:[1 TO *]'})+desc

Upvotes: 3

Related Questions