【问题标题】:Apache lucene indexingApache lucene 索引
【发布时间】:2013-03-08 12:29:59
【问题描述】:

我正在使用 apache lucene 为日志文件创建一个文本搜索应用程序。我正在使用下面的代码来索引文件

doc.add(new LongField("modified", file.lastModified(), Field.Store.NO));
doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8"))));
doc.add(new StoredField("filename", file.getCanonicalPath()));

我在这里为每个文件创建 3 个索引但是在搜索时我只能检索一个索引的值,其他两个索引为空。这是搜索端代码

Document d = searcher.doc(docId);
System.out.println(i+":File name is"+d.get("filename"));
System.out.println(i+":File name is"+d.get("modified"));
System.out.println(i+":File name is"+d.get("contents"));

我得到的输出是

2 total matching documents
0:File name is/home/maclean/NetBeansProjects/LogSearchEngine/src/SimpleSearcher.java
0:File name isnull
0:File name isnull
1:File name is/home/maclean/NetBeansProjects/LogSearchEngine/src/SimpleFileIndexer.java
1:File name isnull
1:File name isnull   

我做错了什么

【问题讨论】:

  • 使用 Field.Store.YES 代替 Field.Store.NO

标签: java jakarta-ee lucene


【解决方案1】:

在 Lucene 中,如果要检索某个字段的值,则需要存储该字段。如果未存储字段,则在搜索时其值为null

对于modified,您已通过传递参数Field.Store.NO 将其明确指定为未存储字段;结果,它的值 not 存储在索引中,因此在搜索时返回 null。要存储和检索其值,您需要将构造函数调用更改为:

doc.add(new LongField("modified", file.lastModified(), Field.Store.YES));

对于contents,您使用的构造函数会创建未存储的字段。您需要将其构造函数更改为:

doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, "UTF-8")), Field.Store.YES));

进行这些更改后,您应该能够检索这两个字段。

您可以检索filename 的值,因为您使用的是默认创建存储字段的构造函数。

【讨论】:

  • 没问题。很高兴它有帮助。
猜你喜欢
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
相关资源
最近更新 更多