lucene的结果是否比预期的要少

时间:2013-03-22 09:02:42

标签: java lucene

对于模糊标题感到抱歉。这是我的问题。我使用以下代码从lucene搜索

中检索结果
scoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
for (int i = 0; i < numTotalHits; i++) {
    int docId = hits[i].doc;
    Document d = searcher.doc(docId);
    System.out.println(i+":File name is: "+d.get("filename"));
    System.out.println(i+":File content is: "+d.get("contents"));
}

现在这段代码会弹出一个异常,表明索引超出了数组范围。 所以我使用下面的代码,它不会导致任何异常

scoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;
for (int i = 0; i < numTotalHits-1; i++) {
    int docId = hits[i].doc;
    Document d = searcher.doc(docId);
    System.out.println(i+":File name is: "+d.get("filename"));
    System.out.println(i+":File content is: "+d.get("contents"));
}

这是否意味着results.totalHits返回results.totalHits-2次搜索结果?

谢谢..

1 个答案:

答案 0 :(得分:3)

totalHits是与您的查询匹配的文档总数,通常与您从lucene获取的文档数量不同,这是您在hits数组中获得的文档数。 / p>

为什么?想想搜索引擎:你执行一个查询,一百万个文档匹配,但你只在一个页面中返回10或20个。一百万是你从lucene得到的totalHits,但是你只会对前10或20个文件提出lucene,这些文件将在hits数组中返回。

totalHitshits.length相同时,只有一种情况:当您的查询与少数或等于执行查询时请求的文档数相匹配的文档数时,10 in以下示例:

TopDocs results = searcher.search(query, 10);