Lucene:对同一文档多次调用 UpdateDocument 会导致分数不断增加

Lucene: multiple calls to UpdateDocument for same doc results in ever-increasing scores

我对观察到的某些 Lucene.NET 行为感到非常困惑。我假设在 Java 的 Lucene 中也是如此,但尚未验证。这是一个演示的测试:

[Fact]
public void repro()
{
    var directory = new RAMDirectory();
    var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

    float firstScore, secondScore, thirdScore;

    using (var indexWriter = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
    {
        var document = new Document();
        document.Add(new Field("id", "abc", Field.Store.YES, Field.Index.NOT_ANALYZED));
        document.Add(new Field("field", "some text in the field", Field.Store.NO, Field.Index.ANALYZED));
        indexWriter.UpdateDocument(new Term("id", "abc"), document, analyzer);

        // the more times I call UpdateDocument here, the higher the score is for the subsequent hit
//                indexWriter.UpdateDocument(new Term("id", "abc"), document, analyzer);
        indexWriter.Commit();

        var queryParser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "field", analyzer);
        var parsedQuery = queryParser.Parse("some text in the field");

        using (var indexSearcher = new IndexSearcher(directory, readOnly: true))
        {
            var hits = indexSearcher.Search(parsedQuery, 10);
            Assert.Equal(1, hits.TotalHits);
            firstScore = hits.ScoreDocs[0].Score;
        }

        using (var indexSearcher = new IndexSearcher(directory, readOnly: true))
        {
            var hits = indexSearcher.Search(parsedQuery, 10);
            Assert.Equal(1, hits.TotalHits);
            secondScore = hits.ScoreDocs[0].Score;
        }

        document = new Document();
        document.Add(new Field("id", "abc", Field.Store.YES, Field.Index.NOT_ANALYZED));
        document.Add(new Field("field", "some changed text in the field", Field.Store.NO, Field.Index.ANALYZED));

        // if I call DeleteAll here, then score three is the same as score one and two (which is probably fine, though not quite what I expected either)
//                indexWriter.DeleteAll();

        indexWriter.UpdateDocument(new Term("id", "abc"), document, analyzer);
        indexWriter.Commit();

        using (var indexSearcher = new IndexSearcher(directory, readOnly: true))
        {
            var hits = indexSearcher.Search(parsedQuery, 10);
            Assert.Equal(1, hits.TotalHits);
            thirdScore = hits.ScoreDocs[0].Score;
        }
    }

    // this is fine
    Assert.Equal(firstScore, secondScore);

    // this is not
    Assert.True(thirdScore < secondScore);
}

步骤是:

  1. 将 "some text in the field" 的文档添加到索引中作为其索引文本。
  2. 搜索"some text in the field"两次,记录分数为firstScoresecondScore
  3. 更新文档以便索引文本现在为 "some changed text in the field"
  4. 再次搜索"some text in the field",记录得分为thirdScore
  5. 断言第一个和第二个分数相等,第三个分数小于第一个和第二个

真正奇怪的是 thirdScorefirstScoresecondScore 。这是我发现的:

任何人都可以向我解释这种行为吗?当文档内容和查询都没有改变时,为什么分数会随着文档的后续更新而改变?

首先,在尝试理解为什么以某种方式对某些内容进行评分时,您应该知道的最有用的工具:IndexSearcher.Explain

Explanation explain = indexSearcher.Explain(parsedQuery, hits.ScoreDocs[0].Doc);

其中详细解释了该分数是如何得出的。在这种情况下,两个不同的评分查询看起来非常相似 除了 第三个查询的 idf 分数如下所示:

0.5945349 = idf(docFreq=2, maxDocs=2)

与前两个查询相比:

0.3068528 = idf(docFreq=1, maxDocs=1)

Lucene 更新只是先删除再插入。删除通常只是标记要删除的文档,并等到稍后从索引中实际清除数据。因此,您不会在搜索结果中看到已删除的文档,但它们仍然会影响 docfreq 等统计数据。当您有大量数据时,影响通常很小。

你可以强制索引到ExpungeDeletes看到这个:

indexWriter.UpdateDocument(new Term("id", "abc"), document, analyzer);
indexWriter.Commit();

//arugment=true to block until completed.
indexWriter.ExpungeDeletes(true);
indexWriter.Commit();

然后你应该看到他们都得到相同的分数。

请记住,删除删除可能是一项非常昂贵的操作。实际上,您可能应该在每次更新后都这样做。


至于为什么 "some text in the field" 和 "some changed text in the field" 的文档得分相同,您指的是 lengthNorm 得分因素。 lengthNorm 在索引时计算,并存储在字段的范数中,为了性能,范数以非常有损的方式压缩到 单字节 。总而言之,它们具有三位精度,甚至不到一位有效的小数位。因此,这两者之间的差异不足以在分数中表示。用类似的东西试试:

some more significantly changed text in the field

你应该会看到 lengthNorm 生效。