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

标签 c# lucene lucene.net

我对我观察到的一些 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. 将“字段中的一些文本”作为其索引文本的文档添加到索引。
  2. 搜索“some text in the field”两次,将分数记录为 firstScoresecondScore
  3. 更新文档,使索引文本现在是“字段中的一些更改文本”
  4. 再次搜索“some text in the field”,记录分数为thirdScore
  5. 断言第一个和第二个分数相等,第三个分数小于第一个和第二个

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

  • 我对同一文档的索引调用UpdateDocument的次数越多,得分就越高
  • 在执行第三次搜索之前完全删除索引会产生与第一和第二个分数相同的分数。由于索引文本中的额外单词(“已更改”),我的期望会降低一些,但即使分数相等就足够了
  • 抵制 RemoveDocument 并手动删除和添加文档没有区别
  • 在提交后对索引调用 WaitForMerges 没有任何区别

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

最佳答案

首先,当您试图理解为什么以某种方式对某些内容进行评分时,您应该知道最有用的工具: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();

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

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


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

some more significantly changed text in the field

您应该会看到 lengthNorm 生效。

关于c# - Lucene:对同一文档多次调用 UpdateDocument 会导致分数不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28407613/

相关文章:

c# - .Net 的 Java AST 解析器

c# - 当前上下文中不存在名称 'File'

java - 按文档 ID 降序搜索 Lucene

Umbraco Lucene.Net.Index.MergePolicy.MergeException 这是什么原因造成的?

search - 有没有办法防止使用 Sitecore 搜索和 Lucene 进行部分单词匹配?

c# - SQL Server 2012 FileTable 创建文件时性能下降(集成 Lucene.NET)

c# - 带有 ASP.NET MVC 的 C# 中的通知系统/服务

c# - 如何在无法使用线程的情况下使用yield-return 来使用回调系统?

java - solr autoSoftCommit 将打开新的搜索器?

java - 获取@ClassbBridge字段值