gpt4 book ai didi

elasticsearch - 通过减去两个数字字段对elasticsearch进行排序

转载 作者:行者123 更新时间:2023-12-03 01:18:39 25 4
gpt4 key购买 nike

是否可以通过两个字段之间的减法对结果进行排序?

例如。有两个字段:“价格”,“lastPrice”。我将按价格-lastPrice对文档进行排序。

可以直接完成此操作,还是唯一的方法就是创建一个新字段?

最佳答案

您可以使用脚本排序

数据:

[
{
"_index" : "index19",
"_type" : "_doc",
"_id" : "LI9zpnEB1jFeu7E5l58r",
"_score" : 1.0,
"_source" : {
"title" : "a",
"price" : 10,
"lastPrice" : 15
}
},
{
"_index" : "index19",
"_type" : "_doc",
"_id" : "LY9zpnEB1jFeu7E5xJ_A",
"_score" : 1.0,
"_source" : {
"title" : "b",
"price" : 15,
"lastPrice" : 20
}
},
{
"_index" : "index19",
"_type" : "_doc",
"_id" : "Lo95pnEB1jFeu7E5Sp9w",
"_score" : 1.0,
"_source" : {
"title" : "b",
"price" : 20,
"lastPrice" : 21
}
}
]

查询:
{
"sort": [
{
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "doc['lastPrice'].value-doc['price'].value"
},
"order": "asc"
}
}
]
}


结果:
[
{
"_index" : "index19",
"_type" : "_doc",
"_id" : "Lo95pnEB1jFeu7E5Sp9w",
"_score" : null,
"_source" : {
"title" : "b",
"price" : 20,
"lastPrice" : 21
},
"sort" : [
1.0
]
},
{
"_index" : "index19",
"_type" : "_doc",
"_id" : "LI9zpnEB1jFeu7E5l58r",
"_score" : null,
"_source" : {
"title" : "a",
"price" : 10,
"lastPrice" : 15
},
"sort" : [
5.0
]
},
{
"_index" : "index19",
"_type" : "_doc",
"_id" : "LY9zpnEB1jFeu7E5xJ_A",
"_score" : null,
"_source" : {
"title" : "b",
"price" : 15,
"lastPrice" : 20
},
"sort" : [
5.0
]
}
]

关于elasticsearch - 通过减去两个数字字段对elasticsearch进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61384134/

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