[英]Lucene: how to index file names
我是新手Lucene用戶,現在嘗試獲取一些基礎知識。
我有三個文件:
apache_empty.txt
(空文件), apache.txt
(包含許多'apache'
令牌), other.txt
(僅包含一個令牌other.txt
'apache'
) 當我嘗試搜索'apache'
, 我只得到 apache.txt
和other.txt
的結果,但是我什至想要得到apache_empty.txt
文件,該文件的名稱中包含搜索到的單詞...
這就是我將文檔添加到索引的方式:
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*
,該apache*
也要與文件名apache_empty
匹配以獲取完整的語法,另請參閱Apache Lucene查詢解析器 。
另一種選擇是在所使用的分析器中包括下划線作為單詞分隔符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.