gpt4 book ai didi

java - 如何在lucene中索引日期字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:48 25 4
gpt4 key购买 nike

我是 lucene 的新手。我必须索引日期字段。我在 lucene 3.0.0 中使用以下 IndexWriter 构造函数。

IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir), new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED)

我的观点是:为什么在不分析日期字段时需要分析器,而索引时我使用了 Field.Index.NOT_ANALYZED

最佳答案

您可以用这种方式存储日期字段..

Document doc = new Document();
doc.add(new Field("modified",
DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE),
Field.Store.YES, Field.Index.NOT_ANALYZED));

其中 f 是一个文件对象...

现在将上面的文档用于索引编写器...

查看lucene附带的示例代码...和以下链接... http://lucene.apache.org/java/2_2_0/api/org/apache/lucene/document/DateTools.html

更新

Field.Index NOT_ANALYZED

Index the field's value without using an Analyzer, so it can be searched. As no analyzer is used the value will be stored as a single term. This is useful for unique Ids like product numbers.

根据 lucene javadoc,您不需要使用 Field.Index NOT_ANALYZED 的字段分析器,但我认为通过设计,IndexWriter 期望分析器作为索引精确副本的分析器数据在存储和搜索方面效率不高。

关于java - 如何在lucene中索引日期字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4333348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com