Take the 2-minute tour ×

I use the following statement to index a date:

luceneDoc.add(new NumericField(key).setLongValue(date.getTime()));

I also use statements as follows to add text properties:

luceneDoc.add(new Field(key, value, Field.Store.YES, Field.Index.ANALYZED));

Then I perform a text property query:


This works perfect. But when I perform a range query, nothing gets returned:

my-date-property:[20100101 TO 20110101]

What am I missing here?

I had a look at the index with Luke, I see all my text property for a document but the date properties only appear in the overview page... maybe that is normal. I actually DO SEE the date properties if I add it like this:

NumericField field = new NumericField(key, Field.Store.YES, true);

But: the query still does not work! Maybe it only works from Java with the Query Builder? I have not tried out that. But it would be great if the text query would work too. ANY IDEA???

share|improve this question

3 Answers 3

up vote 3 down vote accepted

If you want a range query to work with dates in the form of YYYYMMDD, then index your date like this:

String dateString = DateTools.dateToString(date, Resolution.DAY);
luceneDoc.add(new Field(key, dateString, Store.YES, Index.NOT_ANALYZED));
share|improve this answer
Thanks, I'll try this out... So you are also saying that the standard query parser DOES support range queries, right? Because 'Moleski' says something different... –  basZero May 1 '11 at 8:46
Just a question: by adding a Field instead of NumericField I assume that it is stored as TEXT property in the index. Currently I see my date values as DATE in index (via Luke). If it is stored as TEXT, I guess you can't do range queries via the API, right? If so, it is also not what I wanted... –  basZero May 1 '11 at 10:31
Yes, you can do range queries over strings, see lucene.apache.org/java/2_4_0/…. –  Stefan Endrullis May 2 '11 at 8:01
Thanks! range queries also work with Text. I'm testing now. –  basZero May 2 '11 at 8:30
THANKS! This works like a charm! –  basZero May 2 '11 at 15:04

Try to declare my-date-property as DateField.

Since you are using a NumbericField I suppose that the range you specified is interpreted as a numeric range instead of a date range. In this case the numbers 20100101 and 20110101 are far too low to get any reasonable results.

share|improve this answer
I tried that but the class does not exist anymore, it is fully deprecated. Also in the "Lucene In Action" book they say to use NumericField. See the javadoc: DateField Javadoc –  basZero Apr 29 '11 at 18:36
I EDITED my question, please have a look. It is still not working! –  basZero Apr 29 '11 at 18:51

Numeric fields and numeric range queries are absolutely brilliant, but they really are tricky to use for the first time!

Currently, the standard query parser doesn't support numeric range queries. In order to make use of numeric fields, you'll need to derive your own query parser variants and construct numeric range queries where appropriate.

Clarifying my original answer a little (I've just seen it referred to in a comment...), I should note that range queries, where numbers are converted to (usually) zero prefixed text work fine (albeit relatively slowly) in the standard query parser. From the original information posted, the question is how to use numeric (trie encoded) fields in a query. For that you need to parse a query in such a way as to produce numeric range queries (which understand trie encodings). These work much faster than text encoded numeric fields.

Good luck

share|improve this answer

Your Answer


