gpt4 book ai didi

elasticsearch - Elastic Search 中 function_score 的语法

转载 作者:行者123 更新时间:2023-11-29 02:47:26 26 4
gpt4 key购买 nike

我是 Elastic Search 的新手。我在 ElasticSearch 中为电影艺术家( Actor 和导演)编制了索引,并且简单的文本搜索工作正常,例如,如果我使用以下语法搜索“史蒂文”

{"query":
{"query_string":
{"query":"steven"}
}
}

...我得到以下结果,很好:

1. Steven Conrad       - Popularity (from document) = 487 - elasticsearch _score = 3,2589545
2. Steven Knight - Popularity (from document) = 487 - elasticsearch _score = 3,076738
3. Steven Waddington - Popularity (from document) = 431 - elasticsearch _score = 2,4931839
4. Steven E. De Souza - Popularity (from document) = 534 - elasticsearch _score = 2,4613905
5. Steven R. Monroe - Popularity (from document) = 293 - elasticsearch _score = 2,4613905
6. Steven Mackintosh - Popularity (from document) = 363 - elasticsearch _score = 2,2812681
7. Steven Wright - Popularity (from document) = 356 - elasticsearch _score = 2,2812681
8. Steven Soderbergh - Popularity (from document) = 5947 - elasticsearch _score = 2,270944
9. Steven Seagal - Popularity (from document) = 1388 - elasticsearch _score = 2,270944
10. Steven Bauer - Popularity (from document) = 714 - elasticsearch _score = 2,270944

但是,正如您在上面看到的,我的文档中有一个流行度数字字段,并且在搜索“steven”时,我想要最受欢迎的艺术家(Steven Soderbergh、Steven Seagal ...)排在第一位。

理想情况下,我想按 popularity * _score 对上面的结果进行排序

我很确定我必须使用 Elastic Search 的 function_score 功能,但我无法弄清楚确切的语法。

我尝试使用以下语法进行“改进”搜索

{
"query": {
"custom_score": {
"query": {
"query_string": {
"query": "steven"
}
},
"script": "_score * doc['popularity']"
}
}
}

但是我得到一个异常(从下面的错误消息中提取:)

org.elasticsearch.search.query.QueryPhaseExecutionException: [my_index][4]: query[filtered(function score (_all:steven,function=script[_score * doc['popularity']], params [null]))->cache(_type:artist)],from[0],size[10]: Query Failed [Failed to execute main query]
// ....
Caused by: java.lang.RuntimeException: uncomparable values <<1.9709579>> and <<org.elasticsearch.index.fielddata.ScriptDocValues$Longs@7c5b73bc>>
// ...
... 9 more
Caused by: java.lang.ClassCastException: org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Float
at java.lang.Float.compareTo(Float.java:33)
at org.elasticsearch.common.mvel2.math.MathProcessor.doOperationNonNumeric(MathProcessor.java:266)

我的印象是我使用的语法不正确

正确的语法应该是什么?或者还有什么我想念的吗?提前致谢

编辑我的表映射定义如下:

"mappings" : {
"artist" : {
"_all" : {
"auto_boost" : true
},
"properties" : {
"first_name" : {
"type" : "string",
"index" : "not_analyzed",
"analyzer" : "standard"
},
"last_name" : {
"type" : "string",
"boost" : 2.0,
"index" : "not_analyzed",
"norms" : {
"enabled" : true
},
"analyzer" : "standard"
},
"popularity" : {
"type" : "integer"
}
}
}
}

最佳答案

您是否错过了 doc['...'] 附近的 .value

这对我有用(我存储了没有映射的整数):

$ curl -XPUT localhost:9200/test/test/a -d '{"name":"steven", "popularity": 666}'
{"_index":"test","_type":"test","_id":"a","_version":1,"created":true}

$ curl -XPUT localhost:9200/test/test/b -d '{"name":"steven", "popularity": 42}'
{"_index":"test","_type":"test","_id":"b","_version":1,"created":true}

$ curl -XPOST localhost:9200/test/test/_search\?pretty -d '{ "query": { "custom_score": { "query": { "match_all": {}}, "script": "_score * doc[\"popularity\"].value" } } }'
{
"took" : 83,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 666.0,
"hits" : [ {
"_index" : "test",
"_type" : "test",
"_id" : "a",
"_score" : 666.0, "_source" : {"name":"steven", "popularity": 666}
}, {
"_index" : "test",
"_type" : "test",
"_id" : "b",
"_score" : 42.0, "_source" : {"name":"steven", "popularity": 42}
} ]
}
}

关于elasticsearch - Elastic Search 中 function_score 的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039092/

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