简体   繁体   中英

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

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)"

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

另请注意,在性能方面,您应该使用过滤器查询:

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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM