Elasticsearch按照id排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangqing84411433/article/details/86711617

文档唯一标识由四个元数据字段组成:
_id:文档的字符串 ID
_type:文档的类型名
_index:文档所在的索引
_uid:_type 和 _id 连接成的 type#id

默认情况下,_uid 是被保存(可取回)和索引(可搜索)的。_type 字段被索引但是没有保存,_id和 _index 字段则既没有索引也没有储存,它们并不是真实存在的。Elasticsearch 使用 _uid 字段来追溯 _id。

GET test_log/course/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_uid": {
        "order": "asc"
      }
    }
  ]
}

案例如下:

GET /test_index/test_type/_search
{
  "sort": [
    {
      "_uid": {
        "order": "asc"
      }
    }
  ]
}

查询结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": null,
    "hits": [
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "1",
        "_score": null,
        "_source": {
          "test_field1": "test_field1",
          "test_field2": "partial update test"
        },
        "sort": [
          "test_type#1"
        ]
      },
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "11",
        "_score": null,
        "_source": {
          "num": 1,
          "tags": []
        },
        "sort": [
          "test_type#11"
        ]
      },
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "3",
        "_score": null,
        "_source": {
          "test_field": "test3"
        },
        "sort": [
          "test_type#3"
        ]
      },
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "6",
        "_score": null,
        "_source": {
          "test_field": "test test"
        },
        "sort": [
          "test_type#6"
        ]
      },
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "7",
        "_score": null,
        "_source": {
          "test_field": "test client 2"
        },
        "sort": [
          "test_type#7"
        ]
      },
      {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "8",
        "_score": null,
        "_source": {
          "test_field": "test client 2"
        },
        "sort": [
          "test_type#8"
        ]
      }
    ]
  }
}

由于_id是字符串类型,所按照id排序为排序为 1  < 11 < 3 < 6 < 7 < 8

猜你喜欢

转载自blog.csdn.net/wangqing84411433/article/details/86711617