Lucene 已经支持 TF/IDF 评分,当然,默认情况下,所以我不太确定我理解你在寻找什么。
这听起来有点像您想根据查询本身中的 TF/IDF 来权衡查询词。因此,让我们考虑其中的两个要素:
因此,IDF 在这种情况下实际上没有意义,而 TF 已经为您完成了。所以,你真的不需要做任何事情。
请记住,虽然还有其他得分元素! coord 因素在这里很重要。
-
a b a 匹配四个查询词(a b a a,但不匹配 c d)
-
a b c 匹配五个查询词(a b a c a,但不匹配 d)
因此,该特定评分元素将对第二个文档评分更高。
这里是文档a b a 的explain(参见IndexSearcher.explain)输出:
0.26880693 = (MATCH) product of:
0.40321037 = (MATCH) sum of:
0.10876686 = (MATCH) weight(text:a in 0) [DefaultSimilarity], result of:
0.10876686 = score(doc=0,freq=2.0 = termFreq=2.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.42039964 = fieldWeight in 0, product of:
1.4142135 = tf(freq=2.0), with freq of:
2.0 = termFreq=2.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=0)
0.07690979 = (MATCH) weight(text:b in 0) [DefaultSimilarity], result of:
0.07690979 = score(doc=0,freq=1.0 = termFreq=1.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.29726744 = fieldWeight in 0, product of:
1.0 = tf(freq=1.0), with freq of:
1.0 = termFreq=1.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=0)
0.10876686 = (MATCH) weight(text:a in 0) [DefaultSimilarity], result of:
0.10876686 = score(doc=0,freq=2.0 = termFreq=2.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.42039964 = fieldWeight in 0, product of:
1.4142135 = tf(freq=2.0), with freq of:
2.0 = termFreq=2.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=0)
0.10876686 = (MATCH) weight(text:a in 0) [DefaultSimilarity], result of:
0.10876686 = score(doc=0,freq=2.0 = termFreq=2.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.42039964 = fieldWeight in 0, product of:
1.4142135 = tf(freq=2.0), with freq of:
2.0 = termFreq=2.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=0)
0.6666667 = coord(4/6)
对于文档a b c:
0.43768594 = (MATCH) product of:
0.52522314 = (MATCH) sum of:
0.07690979 = (MATCH) weight(text:a in 1) [DefaultSimilarity], result of:
0.07690979 = score(doc=1,freq=1.0 = termFreq=1.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.29726744 = fieldWeight in 1, product of:
1.0 = tf(freq=1.0), with freq of:
1.0 = termFreq=1.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=1)
0.07690979 = (MATCH) weight(text:b in 1) [DefaultSimilarity], result of:
0.07690979 = score(doc=1,freq=1.0 = termFreq=1.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.29726744 = fieldWeight in 1, product of:
1.0 = tf(freq=1.0), with freq of:
1.0 = termFreq=1.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=1)
0.07690979 = (MATCH) weight(text:a in 1) [DefaultSimilarity], result of:
0.07690979 = score(doc=1,freq=1.0 = termFreq=1.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.29726744 = fieldWeight in 1, product of:
1.0 = tf(freq=1.0), with freq of:
1.0 = termFreq=1.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=1)
0.217584 = (MATCH) weight(text:c in 1) [DefaultSimilarity], result of:
0.217584 = score(doc=1,freq=1.0 = termFreq=1.0
), product of:
0.435168 = queryWeight, product of:
1.0 = idf(docFreq=1, maxDocs=2)
0.435168 = queryNorm
0.5 = fieldWeight in 1, product of:
1.0 = tf(freq=1.0), with freq of:
1.0 = termFreq=1.0
1.0 = idf(docFreq=1, maxDocs=2)
0.5 = fieldNorm(doc=1)
0.07690979 = (MATCH) weight(text:a in 1) [DefaultSimilarity], result of:
0.07690979 = score(doc=1,freq=1.0 = termFreq=1.0
), product of:
0.25872254 = queryWeight, product of:
0.5945349 = idf(docFreq=2, maxDocs=2)
0.435168 = queryNorm
0.29726744 = fieldWeight in 1, product of:
1.0 = tf(freq=1.0), with freq of:
1.0 = termFreq=1.0
0.5945349 = idf(docFreq=2, maxDocs=2)
0.5 = fieldNorm(doc=1)
0.8333333 = coord(5/6)
请注意,根据需要,与术语 a 的匹配项在第一个文档中获得更高的权重,您还会看到每个独立的 a 单独评估并添加到分数中。
但是,还要注意坐标的差异,以及第二个文档中术语“c”的 idf 上的差异。这些分数影响只是消除了通过添加相同术语的倍数所获得的提升。如果您在查询中添加足够多的as,它们最终会交换位置。 c 上的匹配只是被评估为远更重要的结果。