Lucene 2.9.x 的范围查询 - 索引中的日期不起作用
我使用以下语句来索引日期:
luceneDoc.add(new NumericField(key).setLongValue(date.getTime()));
我还使用如下语句来添加文本属性:
luceneDoc.add(new Field(key, value, Field.Store.YES, Field.Index.ANALYZED));
然后我执行文本属性查询:
author:hans
这非常有效。但是当我执行范围查询时,没有返回任何内容:
my-date-property:[20100101 TO 20110101]
我在这里缺少什么?
我和 Luke 一起查看了索引,我看到文档 的所有文本属性,但日期属性仅出现在概述页面中......也许这是正常的。 如果我像这样添加它,我实际上会看到日期属性:
NumericField field = new NumericField(key, Field.Store.YES, true);
field.setLongValue(date.getTime());
luceneDoc.add(field);
但是:查询仍然不起作用!也许它只能在 Java 中使用查询生成器工作?我还没有尝试过。但如果文本查询也能工作那就太好了。有什么想法吗???
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???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望范围查询能够处理
YYYYMMDD
形式的日期,请像这样索引您的日期:If you want a range query to work with dates in the form of
YYYYMMDD
, then index your date like this:尝试将
my-date-property
声明为DateField
。由于您使用的是
NumbericField
我认为您指定的范围被解释为数字范围而不是日期范围。在本例中,数字 20100101 和 20110101 太低,无法获得任何合理的结果。Try to declare
my-date-property
asDateField
.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.数字字段和数字范围查询绝对很棒,但第一次使用它们确实很棘手!
目前,标准查询解析器不支持数字范围查询。为了使用数字字段,您需要派生自己的查询解析器变体并在适当的情况下构造数字范围查询。
稍微澄清一下我原来的答案(我刚刚看到它在评论中提到......),我应该注意到范围查询,其中数字转换为(通常)零前缀文本在标准查询解析器。从发布的原始信息来看,问题是如何在查询中使用数字(trie 编码)字段。为此,您需要以生成数字范围查询(理解 trie 编码)的方式解析查询。它们的工作速度比文本编码的数字字段快得多。
祝你好运
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