gpt4 book ai didi

lucene - Lucene 评分问题

转载 作者:行者123 更新时间:2023-12-02 05:14:38 26 4
gpt4 key购买 nike

我对 Lucene 的评分功能有一个问题,我无法弄清楚。到目前为止,我已经能够编写这段代码来重现它。

package lucenebug;

import java.util.Arrays;
import java.util.List;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.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;

public class Test {
private static final String TMP_LUCENEBUG_INDEX = "/tmp/lucenebug_index";

public static void main(String[] args) throws Throwable {
SimpleAnalyzer analyzer = new SimpleAnalyzer();
IndexWriter w = new IndexWriter(TMP_LUCENEBUG_INDEX, analyzer, true);
List<String> names = Arrays
.asList(new String[] { "the rolling stones",
"rolling stones (karaoke)",
"the rolling stones tribute",
"rolling stones tribute band",
"karaoke - the rolling stones" });
try {
for (String name : names) {
System.out.println("#name: " + name);
Document doc = new Document();
doc.add(new Field("name", name, Field.Store.YES,
Field.Index.TOKENIZED));
w.addDocument(doc);
}
System.out.println("finished adding docs, total size: "
+ w.docCount());

} finally {
w.close();
}

IndexSearcher s = new IndexSearcher(TMP_LUCENEBUG_INDEX);
QueryParser p = new QueryParser("name", analyzer);
Query q = p.parse("name:(rolling stones)");
System.out.println("--------\nquery: " + q);

TopDocs topdocs = s.search(q, null, 10);
for (ScoreDoc sd : topdocs.scoreDocs) {
System.out.println("" + sd.score + "\t"
+ s.doc(sd.doc).getField("name").stringValue());
}
}
}

我运行它得到的输出是:

finished adding docs, total size: 5
--------
query: name:rolling name:stones
0.578186 the rolling stones
0.578186 rolling stones (karaoke)
0.578186 the rolling stones tribute
0.578186 rolling stones tribute band
0.578186 karaoke - the rolling stones

我只是不明白为什么滚石滚石致敬具有相同的相关性。根据lucene的documentation ,一个字段拥有的 token 越多,归一化因子应该越小,因此 the Rolling Stones 贡品 的得分应该低于 the Rolling Stones

有什么想法吗?

最佳答案

长度归一化因子的计算方式为1/sqrt(numTerms)(您可以在DefaultSimilarity中看到这一点

该结果不直接存储在索引中。该值乘以指定字段的提升值。然后将最终结果编码为 8 位,如 Similarity.encodeNorm() 中所述。这是一种有损编码,这意味着精细的细节会丢失。

如果您想查看长度规范化的实际效果,请尝试使用以下句子创建文档。

the rolling stones tribute a b c d e f g h i j k 

这将在您可以看到的长度标准化值中产生足够的差异。

现在,如果您的字段按照您使用的示例只有很少的标记,您可以根据您自己的公式为文档/字段设置提升值,这对于短字段来说本质上是更高的提升。或者,您可以创建自定义相似度并覆盖legthNorm()方法。

关于lucene - Lucene 评分问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1673572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com