2016-03-01 110 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是空的。所以最有可能你没有存储的字段。