How to write a solr query for retrieving all records with numeric field value less then specified?

26,186

Solution 1

I don't think the query parser supports < operator. You have to define a RangeQuery explicitly. You can then attach a name query to that using a BooleanQuery.

Update: Apparently, I was wrong. In fact, Solr's query parser is smarter than Lucene's. Here is your answer: https://lucene.apache.org/solr/guide/8_0/the-standard-query-parser.html#differences-between-lucene-s-classic-query-parser-and-solr-s-standard-query-parser

field:[* TO 100] finds all field values less than or equal to 100

Solution 2

also note that performancewise you should use a filter query for this:

&q=name:ipod&fq=price:[* TO 100]

Solution 3

From my knowledge, I do not believe that Solr/Lucene supports greater/less than. It can be done programmatically for things like integers and dates (and in your case, money values, since there are only two decimal places to worry about).

For example, natively, Lucene and Solr query parsers support less than or equal to (<=):

?q=name:ipod AND price:[* to 99.99]

This would give you the less than 100 dollars that you're looking for, provided the data doesn't involve fractions of a cent.

For things like dates and integers, or other things that have notably finite differences, you can decrement (or in the case of greater than, increment) the value you're going for.

EDIT: Check the documentation for version 6.5 of Solr. It DOES contain exclusive range support. Page 272 of the reference guide explains.

http://mirror.cc.columbia.edu/pub/software/apache/lucene/solr/ref-guide/apache-solr-ref-guide-6.5.pdf

Essentially, use curly braces to denote less than.

?q=name:ipod AND price:{* TO 100}
Share:
26,186
Roman
Author by

Roman

Updated on July 09, 2022

Comments

  • Roman
    Roman almost 2 years

    Let's assume we have a set of mp3-players with names and prices.

    How to write a correct solr query for finding all goods with a certain name and with price less then 100$?

    q = "(name:(ipod) AND price ???? 100.0)"