我正在处理 TestRegexpQuery 的单元测试在 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<>
任何人都可以帮助我了解关于结果的真实情况吗?
最佳答案
您需要存储该字段才能检索它。可以搜索已索引、未存储的字段,但不会随结果一起返回。许多字段构造函数采用一个参数来指定是否应存储它:
doc.add(new TextField("mytext", "some text", Field.Store.YES));
关于java - Lucene 返回空正则表达式搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731541/