我有一个弹性搜索映射,如下所示。提交的金额是关键字类型
{
"claims-service":{
"mappings":{
"properties":{
"requestedAmount":{
"properties":{
"amount":{
"type":"keyword"
}
}
}
}
}
}
}
但是当我尝试使用下面提到的查询对其进行排序时,它没有按升序排序。
GET /claims-service/_search
{
"size": 770,
"query": {
"match_all": {}
},
"sort": [
{
"requestedAmount.amount": {
"order": "asc"
}
}
]
}
我得到的结果如下。正如您清楚地看到的那样,我正在尝试按升序对其进行排序,但它给了我错误的结果。
{
"_index" : "claims-service",
"_type" : "_doc",
"_id" : "79600",
"_score" : null,
"_source" : {
"id" : "79600",
"requestedAmount" : {
"amount" : "101402.310000"
}
},
"sort" : [
"101402.310000"
]
},
{
"_index" : "claims-service",
"_type" : "_doc",
"_id" : "76800",
"_score" : null,
"_source" : {
"id" : "76800",
"requestedAmount" : {
"amount" : "104.160000"
},
"sort" : [
"104.160000"
]
},
{
"_index" : claims-service",
"_type" : "_doc",
"_id" : "72750",
"_score" : null,
"_source" : {
"id" : "72750",
"requestedAmount" : {
"amount" : "104.391000"
},
"sort" : [
"104.391000"
]
},
{
"_index" : "claims-service",
"_type" : "_doc",
"_id" : "79900",
"_score" : null,
"_source" : {
"id" : "79900",
},
"sort" : [
"104909.340000"
]
}
通过查看排序字段可以看出,这些值实际上显示为 ["101402.310000", "104.391000", "104909.340000"]
答案 0 :(得分:1)
属于 keyword
类型,amount
字段按字典顺序(即字母顺序)排序,这与您看到的一致。
如果要按数字顺序排序,则需要使用 numeric data type,例如 double
。您可以保留 keyword
字段并添加类型为 double
的子字段。在更改映射后
PUT claims-service/_mapping
{
"properties": {
"requestedAmount": {
"properties": {
"amount": {
"type": "keyword",
"fields": {
"numeric": {
"type": "double"
}
}
}
}
}
}
}
使用上述命令更改映射后,您可以运行下一个
命令以索引 amount.numeric
子字段
POST claims-service/_update_by_query
然后您就可以使用该新字段对数据进行正确排序。
"sort": [
{
"requestedAmount.amount.numeric": {
"order": "asc"
}
}
]