在 Java 中使用 Lucene 搜索日期范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1644500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:24:00  来源:igfitidea点击:

Searching on date ranges with Lucene in Java?

javalucenedate

提问by user199013

Is it possible to search on date ranges using Lucene in Java? How do I build Lucene search queries based on date fields and dates ranges? For example:

是否可以在 Java 中使用 Lucene 搜索日期范围?如何根据日期字段和日期范围构建 Lucene 搜索查询?例如:

  • between specified dates
  • prior to a specified date
  • after a specified date
  • within the last 24 hours
  • within the past week
  • within the past month.
  • 指定日期之间
  • 在指定日期之前
  • 在指定日期之后
  • 过去 24 小时内
  • 在过去一周内
  • 在过去一个月内。

[Edit] i'm using Lucene 2.4.1 and my system is really legacy and really poorly tested so i would like if possible not to have to upgrade

[编辑] 我使用的是 Lucene 2.4.1,我的系统真的很旧而且测试很差,所以我希望如果可能的话不必升级

回答by skaffman

Lucene (before version 2.9 anyway) only stores String values, and it only supports lexicographical range queries on that data. So if you want to store date/time data and performa range queries on it, you need to explicitly format your data/time values in such a way as to make them lexicographically ordered.

Lucene(无论如何在 2.9 版之前)只存储字符串值,并且它只支持对该数据的字典范围查询。因此,如果您想存储日期/时间数据并对其执行范围查询,您需要显式格式化您的数据/时间值,以使它们按字典顺序排列。

For example, store your date/times as something like 2009-10-29T15:34:00, and then do range queries like [2009-10-29T15:00:00 TO 2009-10-29T16:00:00]

例如,将您的日期/时间存储为类似的内容2009-10-29T15:34:00,然后执行范围查询,例如[2009-10-29T15:00:00 TO 2009-10-29T16:00:00]

As has been pointed out elsewhere, Lucene 2.9 finally introduced support for range queries against non-string data, making this all rather easier.

正如其他地方所指出的,Lucene 2.9 最终引入了对非字符串数据的范围查询的支持,使这一切变得更加容易。