Bootstrap

Elasticsearch 查询最大时间(qbit)

前言

  • 实现 select max(date) 效果

  • 本文对 Elasticsearch 7.13 适用

方式一

  • query 查询(可保持写入时时区)

// 查询语句
GET my_index/_search
{
  "sort": [
    {
      "update_time": {
        "order": "desc"
      }
    }
  ],
  "size": 1,
  "_source": ["update_time"]
}

// 查询结果
{
  "hits" : [
    {
      "_index" : "my_index",
      "_type" : "_doc",
      "_id" : "123",
      "_score" : null,
      "_source" : {
        "update_time" : "2021-12-15T03:48:19+08"
      },
      "sort" : [
        1639511299000
      ]
    }
  ]
}

方式二

  • max 聚合(返回 UTC 时区)

// 查询语句
GET my_index/_search
{
  "size": 0, 
  "aggs": {
    "NAME": {
      "max": {
        "field": "update_time"
      }
    }
  }
}

// 查询结果
{
  "aggregations" : {
    "NAME" : {
      "value" : 1.639511299E12,
      "value_as_string" : "2021-12-14T19:48:19.000Z"
    }
  }
}

本文出自