views:

1350

answers:

4

I want to filter my result set before I search. I know the correct way to do this is by using the filter query (fq) parameter. However, I want to filter based on the output of a function performed on a field.

I have a field 'rating' which is an integer in the range of 1 to ~75000. The upper limit may change. I want to filter to the top 500 items with the highest 'rating'. In SQL this would be something like:

... ORDER BY rating DESC LIMIT 500

I think I can get the documents in solr ranked by rating descending by using the function rord(rating), so basically I would like to do:

fq=rord(rating):[0 TO 500]

But that does not seem possible. Does anyone know what else I could do?

A: 

Unless I'm missing something, you could not sort by the rating field and then simply take the first 500. That would be identical to your SQL example.

Rob Young
I can't limit the responses to 500 because a. That would take away the point of paging; and b. I want to run a filter query so I can facet inside the 500 documents.
cubabit
A: 

Thanks to Yonik Seeley on the Solr mailing list:

Solr 1.4 can now do range queries on arbitrary functions: http://lucene.apache.org/solr/api/org/apache/solr/search/FunctionRangeQParserPlugin.html

Note that ord() and rord() won't work properly in Solr 1.4 trunk. Lucene has changed to searching per-segment in a MultiReader and hence you will currently get the ord() or rord() in that segment, not in the whole index.

cubabit
A: 

So what is the solution for this? to filter to 500 results?

JouMaN
I think the answer is to use the FunctionRangeQParserPlugin at http://lucene.apache.org/solr/api/org/apache/solr/search/FunctionRangeQParserPlugin.html but I had to do it via creating a new field holding the sort rank for my data as we use Solr 1.3.
cubabit