Elasticsearch:排序整数描述 [英] Elasticsearch: sorting integer desc

查看:472
本文介绍了Elasticsearch:排序整数描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用查询对弹性搜索(version 1.1.2)中的整型字段进行排序时:

When sorting by integer field in elasticsearch (version "1.1.2") using query:

{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "cubicCapacity": {
        "order": "asc",
        "ignore_unmapped": true
      }
    }
  ],
  "from": 0,
  "size": 150
}

结果正确,文档按照自然顺序排序(1,2, 5,10)

The result is correct and documents are sorted in natural order (1, 2, 5, 10)

但是,当尝试使用desc完成相同的查询时:

But when trying to complete same query using "desc":

{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "cubicCapacity": {
        "order": "desc",
        "ignore_unmapped": true
      }
    }
  ],
  "from": 0,
  "size": 150
}

结果不正确,文件按一些奇怪的方式排列,但预期为(10,5,2,1)。

The result is not correct and documents are sorted in some strange way, but expected to be (10, 5, 2, 1).

那么为什么这样与desc的匹配无法用自然顺序给出正确的结果?

So why sorting with "desc" could not give a correct result with a natural order?

P.S。当通过asc / desc排序,但使用字符串类型(1,10,2,5)时,desc正确(5,2,10,1)

P.S. When sorting by asc/desc but with a string type (1, 10, 2, 5), "desc" however works correct (5, 2, 10, 1)

推荐答案

你应该在你的号码中添加前导零,所以你将拥有[0001,0010,0002,0005]而不是[1 ,10,2,5]。
您添加的前导零数将取决于您认为的最大值。
例如如果你认为它将低于10亿,所以你应该存储0000000005(9个零)而不是5和0000000010(8个零),而不是10

you should add leading zeros to your numbers, so you will have [0001, 0010, 0002, 0005] instead of [1, 10, 2, 5]. The number of leading zeros you have add to will depend of the max value you think you will have. e.g. if you think it will be under 10 billions so you should store 0000000005 (9 zeros) instead of 5 and 0000000010 (8 zeros) instead of 10

这篇关于Elasticsearch:排序整数描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆