Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching on date ranges with Lucene in Java?

Tags:

java

date

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:

  • 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

like image 342
user199013 Avatar asked Oct 29 '09 15:10

user199013


People also ask

How do you search Lucene?

Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries). To perform a single character wildcard search use the "?" symbol. To perform a multiple character wildcard search use the "*" symbol. You can also use the wildcard searches in the middle of a term.

How does Lucene index search work?

Simply put, Lucene uses an “inverted indexing” of data – instead of mapping pages to keywords, it maps keywords to pages just like a glossary at the end of any book. This allows for faster search responses, as it searches through an index, instead of searching through text directly.

How do you find special characters in Lucene?

You can't search for special characters in Lucene Search. These are + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / @. You can search for special characters, with the exception of the @ character, in a field-level search as long as you escape them using \ before the special character.


1 Answers

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.

like image 111
skaffman Avatar answered Sep 30 '22 08:09

skaffman