使用 Apache Lucene 进行布尔查询的结果编号

Result number for Boolean queries with Apache Lucene

在对 Apache Lucene v7.5 进行基准测试时,我注意到一个奇怪的行为: 我使用 Lucene 和 SimpleAnalyzer(没有停用词,没有词干提取)索引了英文维基百科转储(5,677,776 个文档)

然后我使用以下查询搜索索引:

布尔查询 who 的结果数均大于单个词项 的结果数the 和单个术语 who 的结果编号,当它应该是比两者都小。

有解释吗?

代码片段:

analyzer = new SimpleAnalyzer();
MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[]{"title", "content","domain","url"},analyzer);

// Parse
Query q = parser.parse(querystr);

// top-10 results
int hitsPerPage = 10;

IndexReader indexReader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(indexReader);

// Ranker
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);

// Search
searcher.search(q, collector);

// Retrieve the top-10 documents
TopDocs topDocs=collector.topDocs();

ScoreDoc[] hits = topDocs.scoreDocs;
totalHits=topDocs.totalHits;


System.out.println("query: "+querystr + " " + hits.length+" "+String.format("%,d",totalHits));

解释是 the default operator is OR 而不是您假设的 AND。正在搜索具有 thewho 或两者的 the who returns 文档。

the - 5,382,873
who - 1,687,254
the OR who - 5,411,305

即大多数包含 who 的文档也包含 the,除了 28 432 个文档,它们在您检索两者时添加到结果集中。

您可以通过更改默认运算符来更改此行为:

parser.setDefaultOperator(QueryParserBase.AND_OPERATOR)