2016-03-01 93 views
0

我用TestRegexpQuery在Lucene的這個單元測試工作,一切都運行得很好,但是當我增加了一些額外的打印語句,我不明白爲什麼它不返回文檔本身。Lucene的空車返回正則表達式搜索結果

private int regexQueryNrHits(String regex) throws IOException { 
    // RegexpQuery query = new RegexpQuery(newTerm(regex)); 
    // return searcher.search(query, 5).totalHits; 
    RegexpQuery query = new RegexpQuery(newTerm(regex)); 
    TopDocs result = searcher.search(query, 5); 

    // my code to print the result instead of just the counts 
    //START 
    ScoreDoc[] docs = result.scoreDocs; 
    for (ScoreDoc scoreDoc : docs) { 
     System.out.println(scoreDoc); 
     System.out.println(scoreDoc.doc); 
     System.out.println(scoreDoc.score); 
     System.out.println(scoreDoc.shardIndex); 
     System.out.println(searcher.getIndexReader().document(scoreDoc.doc)); 
    } 
    System.out.println("---------"); 
    // end 
    return result.totalHits; 
    } 

這個測試只插入一個文檔,這是勝負的樣子,我希望它返回無論是句子或匹配的正則表達式,但一切看起來空文件的標記..

--------- 
doc=0 score=1.0 shardIndex=0 
0 
1.0 
0 
Document<> 
--------- 
doc=0 score=1.0 shardIndex=0 
0 
1.0 
0 
Document<> 

任何人都可以幫助我理解結果中究竟發生了什麼?

回答

2

您需要到現場存儲,以便檢索它。索引的,未存儲的字段可以被搜索,但不會返回結果。許多字段構造函數需要參數來指定是否應該存儲:

doc.add(new TextField("mytext", "some text", Field.Store.YES)); 
0

你的問題是關於Lucene的文檔的一個實例是「空」。

你的情況,空指toString()-Method回報Document<>

這意味着fields-List是空的。所以最有可能你沒有存儲的字段。