cubabit
cubabit

Reputation: 2700

Using a function in a filter query in Solr

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?

Upvotes: 1

Views: 6204

Answers (3)

cubabit
cubabit

Reputation: 2700

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.

Upvotes: 3

Rob Young
Rob Young

Reputation: 1245

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.

Upvotes: 1

Related Questions