为什么我会收到 Lucene TopDocs totalHits 类型的错误

Why am I getting an error with Lucene TopDocs totalHits type

我有一个问题 运行 search 演示的 Lucene 官方网站的示例代码。里面有这部分代码:

TopDocs results = searcher.search(query, 5);
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = Math.toIntExact(results.totalHits);

我收到一条错误消息:

TotalHits cannot be converted to long.

如果我说对了

int numTotalHits = results.totalHits ;

我得到同样的错误说:

TotalHits cannot be converted to integer.

我用过的导入有:

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

我已经加载了程序所需的四个Jar文件(lucene.core、common analyzers、demo、queryparser)。

有什么解决办法吗?

在 8.3 版 totalHits is no longer a long, it's now a TotalHits object. The actual number is stored in the value 字段中,因此只需使用 results.totalHits.value 即可。