Sorting 如何在Elasticsearch中使用脚本对结果进行排序时获得_分数而不是空值
当使用脚本对查询结果进行排序时,为什么Elasticsearch会给出null而不是实际分数 我使用这个简单的脚本进行测试Sorting 如何在Elasticsearch中使用脚本对结果进行排序时获得_分数而不是空值,sorting,elasticsearch,elastic-stack,Sorting,elasticsearch,Elastic Stack,当使用脚本对查询结果进行排序时,为什么Elasticsearch会给出null而不是实际分数 我使用这个简单的脚本进行测试 PUT _scripts/simple_sorting { "script" :{ "lang": "painless", "source": """ return Math.random(); """ } } 问题是 GET some_index/_search { "explain": true, "stor
PUT _scripts/simple_sorting
{
"script" :{
"lang": "painless",
"source": """
return Math.random();
"""
}
}
问题是
GET some_index/_search
{
"explain": true,
"stored_fields": [
"_source"
],
"sort": {
"_script":{
"type" : "number",
"script" : {
"id": "simple_sorting"
},
"order" : "desc"
}
},
"query" : {
"bool": {
"should": [
{
"match": {
"tm_applied_for": {
"query": "bisire"
}
}
}
]
}
}
}
查询结果如下所示
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 20,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_shard" : "[some_index][0]",
"_node" : "UIMgEAZNRzmIpRGyQt232g",
"_index" : "some_index",
"_type" : "_doc",
"_id" : "1171229",
"_score" : null,
"_source" : {
"status" : "Registered",
"proprietor_name
.
.
.
.
"@timestamp" : "2020-03-27T20:05:25.753Z",
"tm_applied_for_anan" : "BISLERI"
},
"sort" : [
0.28768208622932434
],
您可以看到max\u score和\u score值为空。但它在排序数组中给出了一个值,elasticsearch根据该值对文档进行排序
我希望返回Elasticsearch在使用脚本排序之前为查询提供的原始分数,而不是null
同样,当我更改脚本简单排序时,如下所示。我在排序数组(比如0.234…)中得到了一些值,这些值不等于我没有使用脚本进行排序时返回的值(比如12.1234…)
PUT _scripts/simple_sorting
{
"script" :{
"lang": "painless",
"source": """
return _score;
"""
}
}
为什么两次的_分数值不一样
当Elasticsearch明确表示我可以在使用脚本排序时访问\u score
PUT _scripts/simple_sorting
{
"script" :{
"lang": "painless",
"source": """
return _score;
"""
}
}
当我使用脚本进行排序时,我期望发生的是
1) max_score和_score保持Elasticsearch给定的状态,而不是变为空
2) 根据Math.random()值进行排序。这是elasticsearch的默认行为,因为您使用自己的逻辑对结果进行排序,因此它会忽略分数。为了仍能将分数集
track\u scores
参数设置为true。这将为您提供elasticsearch计算的相关性分数
GET some_index/_search
{
"explain": true,
"stored_fields": [
"_source"
],
"sort": {
"_script": {
"type": "number",
"script": {
"id": "simple_sorting"
},
"order": "desc"
}
},
"query": {
"bool": {
"should": [
{
"match": {
"tm_applied_for": {
"query": "bisire"
}
}
}
]
}
},
"track_scores": true
}