关于Java:Lucene:如何索引文件名

Lucene: how to index file names

我是lucene的新手用户,现在正在尝试获取一些基础知识。

我有三个文件:

  • apache_empty.txt(空文件),
  • apache.txt(包含许多'apache'令牌),
  • other.txt(仅包含一个令牌-'apache')

当我尝试搜索'apache'时,只得到apache.txtother.txt的结果,但是我什至想要得到apache_empty.txt文件,该文件的名称中包含被搜索的单词...

这就是我将文档添加到索引的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected Document getDocument(File f) throws Exception
{
  Document doc   = new Document();
  Field contents = new Field("contents", new FileReader(f));
  Field parent   = new Field("parent",   f.getParent(), Field.Store.YES, Field.Index.NOT_ANALYZED);
  Field filename = new Field("filename", f.getName(), Field.Store.YES, Field.Index.ANALYZED);
  Field fullpath = new Field("fullpath", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED);
  filename.setBoost(2.0F);
  doc.add(contents);
  doc.add(parent);
  doc.add(filename);
  doc.add(fullpath);
  return doc;
}

如何让lucene索引也使用文件名?


要启用通配符,您应该搜索与您的文件名apache_empty匹配的apache*以获取完整的语法,另请参阅Apache Lucene查询解析器。

一种替代方法是在所使用的分析器中包括下划线作为单词分隔符。