gpt4 book ai didi

java - Lucene 5.2.1 中的更改分数

转载 作者:行者123 更新时间:2023-12-01 11:12:07 24 4
gpt4 key购买 nike

我想更改 Lucene 5.2.1 中搜索引擎的评分功能。我只想将函数 f 的输出添加到默认的 lucene 分数中。像这样的事情:

myScore = defaultScore + f(field1, field2)

其中f是两个索引文档字段(包含数值)的简单演算。这个link证明应该可以做这种事情,但根本没有提供任何片段。

Finally, you can extend the low level Similarity directly to implement a new retrieval model, or to use external scoring factors particular to your application. For example, a custom Similarity can access per-document values via NumericDocValues and integrate them into the score.

有人知道怎么做吗?非常感谢

最佳答案

我认为 CustomScoreQuery 可能就是你想要的。 CustomScoreQuery的链接是在 Lucene 5.2.1 中。以下是有关尝试在 Scala 中使用 Lucene 3.6.2 解决问题的代码片段。

import org.apache.lucene.index.{ Term, IndexReader }
import org.apache.lucene.search.{ TermQuery, Query }
import org.apache.lucene.search.function.{ CustomScoreProvider, CustomScoreQuery }

class CustomizedScoreProvider(reader: IndexReader) extends
CustomScoreProvider(reader) {

protected override def customScore(doc: Int,
subQueryScore: Float, valSrcScores: Array[Float]): Float = {
try {
// subQueryScore is the default score you get from
// the original Query
val currentDocument = reader.document(doc)

// get the value of two field1, field2,
// make sure the two fields are stored since you have to retrieve the value
val field1Value = currentDocument.get("field1")
val field2Value = currentDocument.get("field2")

// write your own logical to compute the extra score, assuming 0 here
val extraScore = 0F

// ignore the valSrcScores here, the original calculation
// is modifiedScore = subQueryScore*valSrcScores[0]*..
subQueryScore + extraScore
} catch {
case _: Exception => subQueryScore
}
}
}

/**
* Create a CustomScoreQuery over input subQuery.
* @param subQuery the sub query whose scored is being customized. Must not be null.
*/
class CustomizedScoreQuery(val subQuery: Query, reader: IndexReader)
extends CustomScoreQuery(subQuery) {
protected override def getCustomScoreProvider(reader: IndexReader) = {
try {
new CustomizedScoreProvider(reader)
} catch {
case _: Exception => super.getCustomScoreProvider(reader)
}
}
}


object CustomizedScoreQueryTest extends App {

val termQuery = new TermQuery(new Term("field", "xxx"))
// get indexSearch, indexReader and wrap the original query
...
val wrappedQuery = new CustomizedScoreQuery(termQuery, indexReader)
val topDocs = indexSearch.search(wrappedQuery, 5)
...

}

实际上,当我看到你的问题时,我遇到了同样的问题,并最终用 CustomScoreQuery 解决了它。当您使用 CustomScoreQuery 更改默认分数时,例如按相关性排序时,文档顺序将根据修改后的分数进行更改。

无论如何,希望对你有帮助。

关于java - Lucene 5.2.1 中的更改分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223427/

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