一 准备数据

  1. PUT lqz/doc/1
  2. {
  3. "name":"顾老二",
  4. "age":30,
  5. "from": "gu",
  6. "desc": "皮肤黑、武器长、性格直",
  7. "tags": ["黑", "长", "直"]
  8. }
  9. PUT lqz/doc/2
  10. {
  11. "name":"大娘子",
  12. "age":18,
  13. "from":"sheng",
  14. "desc":"肤白貌美,娇憨可爱",
  15. "tags":["白", "富","美"]
  16. }
  17. PUT lqz/doc/3
  18. {
  19. "name":"龙套偏房",
  20. "age":22,
  21. "from":"gu",
  22. "desc":"mmp,没怎么看,不知道怎么形容",
  23. "tags":["造数据", "真","难"]
  24. }
  25. PUT lqz/doc/4
  26. {
  27. "name":"石头",
  28. "age":29,
  29. "from":"gu",
  30. "desc":"粗中有细,狐假虎威",
  31. "tags":["粗", "大","猛"]
  32. }
  33. PUT lqz/doc/5
  34. {
  35. "name":"魏行首",
  36. "age":25,
  37. "from":"广云台",
  38. "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
  39. "tags":["闭月","羞花"]
  40. }

二 排序查询:sort

2.1 降序:desc

想到排序,出现在脑海中的无非就是升(正)序和降(倒)序。比如我们查询顾府都有哪些人,并根据age字段按照降序,并且,我只想看nmae和age字段:

  1. GET lqz/doc/_search
  2. {
  3. "query": {
  4. "match": {
  5. "from": "gu"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "age": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

上例,在条件查询的基础上,我们又通过sort来做排序,根据age字段排序,是降序呢还是升序,由order字段控制,desc是降序。

结果如下:

  1. {
  2. "took" : 0,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 3,
  12. "max_score" : null,
  13. "hits" : [
  14. {
  15. "_index" : "lqz",
  16. "_type" : "doc",
  17. "_id" : "1",
  18. "_score" : null,
  19. "_source" : {
  20. "name" : "顾老二",
  21. "age" : 30,
  22. "from" : "gu",
  23. "desc" : "皮肤黑、武器长、性格直",
  24. "tags" : [
  25. "黑",
  26. "长",
  27. "直"
  28. ]
  29. },
  30. "sort" : [
  31. 30
  32. ]
  33. },
  34. {
  35. "_index" : "lqz",
  36. "_type" : "doc",
  37. "_id" : "4",
  38. "_score" : null,
  39. "_source" : {
  40. "name" : "石头",
  41. "age" : 29,
  42. "from" : "gu",
  43. "desc" : "粗中有细,狐假虎威",
  44. "tags" : [
  45. "粗",
  46. "大",
  47. "猛"
  48. ]
  49. },
  50. "sort" : [
  51. 29
  52. ]
  53. },
  54. {
  55. "_index" : "lqz",
  56. "_type" : "doc",
  57. "_id" : "3",
  58. "_score" : null,
  59. "_source" : {
  60. "name" : "龙套偏房",
  61. "age" : 22,
  62. "from" : "gu",
  63. "desc" : "mmp,没怎么看,不知道怎么形容",
  64. "tags" : [
  65. "造数据",
  66. "真",
  67. "难"
  68. ]
  69. },
  70. "sort" : [
  71. 22
  72. ]
  73. }
  74. ]
  75. }
  76. }

上例中,结果是以降序排列方式返回的。

2.2 升序:asc

那么想要升序怎么搞呢?

GET lqz/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

上例,想要以升序的方式排列,只需要将order值换为asc就可以了。

结果如下:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "肤白貌美,娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        },
        "sort" : [
          18
        ]
      },
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        },
        "sort" : [
          22
        ]
      },
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "name" : "魏行首",
          "age" : 25,
          "from" : "广云台",
          "desc" : "仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
          "tags" : [
            "闭月",
            "羞花"
          ]
        },
        "sort" : [
          25
        ]
      },
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        },
        "sort" : [
          29
        ]
      },
      {
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        },
        "sort" : [
          30
        ]
      }
    ]
  }
}

上例,可以看到结果是以age从小到大的顺序返回结果。

三 不是什么数据类型都能排序

那么,你可能会问,除了age,能不能以别的属性作为排序条件啊?来试试:

GET lqz/chengyuan/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "name": {
        "order": "asc"
      }
    }
  ]
}

上例,我们以name属性来排序,来看结果:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "lqz",
        "node": "wrtr435jSgi7_naKq2Y_zQ",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
        }
      }
    ],
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    }
  },
  "status": 400
}

结果跟我们想象的不一样,报错了!

注意:在排序的过程中,只能使用可排序的属性进行排序。那么可以排序的属性有哪些呢?

  • 数字
  • 日期

其他的都不行!