gpt4 book ai didi

sorting - Elasticsearch-按输入顺序排序

转载 作者:行者123 更新时间:2023-12-03 02:06:44 25 4
gpt4 key购买 nike

我有一个包含以下数据的索引:

{  
"_index":"businesses",
"_type":"business",
"_id":"1",
"_version":1,
"found":true,
"_source":{
"business":{
"account_level_id":"2",
"business_city":"Abington",
"business_country":"United States of America",
}
}
}

查询索引时,我想按account_level_id排序(这是1-5之间的数字)。问题是,我不想按ASC或DESC顺序排序,而是按以下顺序排序:4..3..5..2..1。这是由几年前的错误做法引起的,当时帐户级别在级别4达到最高,但随后添加了值为5的较低级别的帐户。是否有办法告诉ES我希望返回结果具体顺序?

最佳答案

你可以写一个sort based script像(未经测试):

doc['account_level_id'].value == "5" ? 3 : doc['account_level_id'].value == "4" ? 5 : doc['account_level_id'].value == "3" ? 4 : doc['account_level_id'].value == "2" ? 2 : 1;

或者,如果可能的话,您可以创建另一个字段 sort_level,将 account_level_id映射到可以排序的明智值。
{  
"_index":"businesses",
"_type":"business",
"_id":"1",
"_version":1,
"found":true,
"_source":{
"business":{
"account_level_id":"4",
"business_city":"Abington",
"business_country":"United States of America",
"sort_level": 5
}
}
}

关于sorting - Elasticsearch-按输入顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291366/

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