2013-03-08 102 views
1

我正在使用apache lucene爲日誌文件創建文本搜索應用程序。我使用的波紋管代碼索引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 

我在做什麼錯

+2

使用Field.Store.YES而不是Field.Store.NO – vikasing 2013-03-08 12:41:24

回答

2

在Lucene的,如果你要檢索的值的字段,你需要存儲該字段。如果未存儲字段,則在搜索其值時將爲null

對於modified,您已通過傳遞參數Field.Store.NO明確指定它爲未存儲的字段;因此它的值是而不是被存儲在索引中,因此在搜索時返回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的值,因爲您使用的構造函數默認會創建存儲的字段。

+0

非常感謝。 – 2013-03-10 10:23:54

+0

沒問題。很高興幫助。 – Kshitij 2013-03-10 13:00:15