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

2022-01-15 00:00:00 date lucene java

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

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:

  • 在指定日期之间
  • 在指定日期之前
  • 在指定日期之后
  • 过去 24 小时内
  • 过去一周内
  • 在过去一个月内.

我使用的是 Lucene 2.4.1,我的系统非常旧,而且测试非常糟糕,所以我希望尽可能不必升级

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.9 版本之前)只存储字符串值,并且它只支持对该数据的字典范围查询.因此,如果您想存储日期/时间数据并对其执行范围查询,则需要明确格式化您的数据/时间值,使其按字典顺序排列.

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.

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

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]

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

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

相关文章