如何编写 solr 查询来检索数字字段值小于指定值的所有记录?

发布于 2024-12-23 08:53:06 字数 140 浏览 1 评论 0原文

假设我们有一组带有名称和价格的 MP3 播放器。

如何编写正确的 solr 查询来查找具有特定名称且价格低于 100 美元的所有商品?

q = "(名称:(ipod) AND 价格 ???? 100.0)"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮湿 2024-12-30 08:53:06

我认为查询解析器不支持 < 运算符。您必须定义 RangeQuery 明确地。然后,您可以使用 布尔查询

更新:显然,我错了。事实上,Solr 的查询解析器比 Lucene 的更智能。这是您的答案:

field:[* TO 100] 查找所有小于或等于 100 的字段值

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

往昔成烟 2024-12-30 08:53:06

另请注意,从性能角度来看,您应该为此使用过滤器查询:

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

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

&q=name:ipod&fq=price:[* TO 100]
黑凤梨 2024-12-30 08:53:06

据我所知,我不认为 Solr/Lucene 支持大于/小于。它可以以编程方式完成,例如整数和日期(在您的情况下,货币值,因为只需要担心两位小数)。

例如,Lucene 和 Solr 查询解析器本身支持小于或等于 (<=):

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

这将为您提供您正在寻找的不到 100 美元的数据,前提是数据不涉及一美分的零头。

对于日期和整数之类的东西,或者其他具有显着有限差异的东西,您可以减少(或者在大于的情况下,增加)您想要的值。

编辑:查看 Solr 6.5 版本的文档。它确实包含独家范围支持。

本质上,使用花括号来表示小于。

?q=name:ipod AND 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.

Essentially, use curly braces to denote less than.

?q=name:ipod AND price:{* TO 100}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文