gpt4 book ai didi

elasticsearch - 从function_score访问查询值以计算新分数

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

我需要自定义ES分数。我需要实现的得分函数是:
score = len(document_term) - len(query_term)
例如,我在ES索引中的文档之一是:

{
"name": "foobar"
}

和搜索查询
{
"query": {
"function_score": {
"query": {
"match": {
"name": {
"query": "foo"
}
}
},
"functions": [
{
"script_score": {
"script": {
"source": "doc['name'].value.length() - ?LEN(query_tem)?"
}
}
}
],
"boost_mode": "replace"
}
}
}

上面的搜索应该提供6-3 = 3的分数。但是我没有找到解决方法来获取查询词的值。

是否可以在function_score上下文中访问查询项的值?

最佳答案

没有直接的方法可以执行此操作,但是您可以通过以下方式实现此目的,即需要在查询的两个不同部分中添加查询参数。

在这一重要说明之前,如果该字段的类型为 doc['myfield'].value ,则不能应用text,相反,您需要将其兄弟字段创建为 keyword ,并在脚本中引用它,下面我再次提到:

对应:

PUT myindex
{
"mappings" : {
"properties" : {
"myfield" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}

样本文件:
POST myquery/_doc/1
{
"myfield": "I've become comfortably numb"
}

查询:
POST <your_index_name>/_search
{
"query": {
"function_score": {
"query": {
"match": {
"myfield": "numb"
}
},
"functions": [
{
"script_score": {
"script": {
"source": "return doc['myfield.keyword'].value.length() - params.myquery.length()",
"params": {
"myquery": "numb" <---- Add the query string here as well
}
}
}
}
],
"boost_mode": "replace"
}
}
}

响应:
{
"took" : 558,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 24.0,
"hits" : [
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 24.0,
"_source" : {
"myfield" : "I've become comfortably numb"
}
}
]
}
}

希望这可以帮助!

关于elasticsearch - 从function_score访问查询值以计算新分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56578291/

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