【问题标题】:Elastic search not sorting correctly弹性搜索未正确排序
【发布时间】:2021-01-11 17:32:08
【问题描述】:

我有一个如下的弹性搜索映射。申报金额为关键字类型

{
   "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"]

【问题讨论】:

    标签: elasticsearch


    【解决方案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"
        }
      }
    ]
    

    【讨论】:

    • 哦,好的。谢谢你,瓦尔。我可以将其设为多字段并将主字段标记为文本,将内部字段标记为双倍吗?
    • 嗨 Val,对于双精度数据类型,排序不能正常工作。当我有值时:13.0、2.0、5.0、9.0。排序给我的结果是 9.0,5.0,2.0,13.0。它将 13.0 放在最后。
    猜你喜欢
    • 2019-01-26
    • 2017-02-09
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多