2011-04-21 191 views
6

對於任何文檔,術語FreqVector始終爲空。 我確定這些文檔在集合和字段中存在。那麼問題在哪裏?Lucene:termFreqVector始終爲空?

對(INT I = 0;我< reader.numDocs();我++){
TermFreqVector TFV = reader.getTermFreqVector(I, 「標籤」);

感謝

回答

6

你確定你與你Field.TermVector.YES索引的字段?這裏有一個工作示例:

Directory directory = new RAMDirectory(); 
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); 
MaxFieldLength mlf = MaxFieldLength.UNLIMITED; 
IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf); 

Document doc = new Document(); 
doc.add(new Field("tags", "foo bar", Field.Store.NO, 
     Field.Index.ANALYZED, Field.TermVector.YES)); 

writer.addDocument(doc); 
writer.close(); 

IndexReader reader = IndexReader.open(directory); 
for (int i = 0; i < reader.numDocs(); i++) { 
    TermFreqVector tfv = reader.getTermFreqVector(i, "tags"); 
    System.out.println(tfv); 
} 
+0

你岩石,感謝 – user680406 2011-04-21 09:05:58

+0

@ user680406沒有問題:) – WhiteFang34 2011-04-21 09:08:08

+2

@ user680406: 你永遠不應該使用numDocs()來訪問文件的索引,而是maxDoc()代替。索引刪除後,您將無法獲得所有文檔。 – 2011-04-26 08:32:35