开发者

Range Query with Lucene 2.9.x - Dates in Index are not working

开发者 https://www.devze.com 2023-03-02 04:44 出处:网络
I use the following statement to index a date: luceneDoc.add(new NumericField(key).setLongValue(date.getTime()));

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:

author:hans

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);
field.setLongValue(date.getTime());
luceneDoc.add(field);

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???


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));


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消