在Java中使用Lucene搜索日期范围? [英] Searching on date ranges with Lucene in Java?

查看:319
本文介绍了在Java中使用Lucene搜索日期范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  • 指定日期之前

  • 指定日期之前

  • 过去一周内



  • >在过去一个月内。



我使用的是Lucene 2.4.1,我的系统真的是遗产,所以我想,如果可能,不必升级

解决方案

Lucene(版本2.9之前)只存储String值,而仅支持该数据的词典范围查询。因此,如果要存储日期/时间数据并对其执行范围查询,则需要以数字/时间值格式显式格式化,以使其按字典顺序排列。



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



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


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:

  • 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.

[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 (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.

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]

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

这篇关于在Java中使用Lucene搜索日期范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆