Elasticsearch How to retrieve the maximum id

elasticsearch

Solution

You can also use Max Aggregation (since 1.3) to get the maximum value of a given field in a document.

curl localhost:9200/myindex/mytype/_search -d
  '{
    "aggs" : {
      "max_id" : {
        "max" : { 
          "field" : "id"
        }
      }
    },
    "size":0
  }'

Will return:

{
  "took":7,
  "timed_out":false,
  "_shards": {
    "total":6,
    "successful":6,
    "failed":0
  },
  "hits": {
    "total":22,
    "max_score":0.0,
    "hits": []
  },
  "aggregations": {
    "max_id": {
      "value": 27.0
    }
  }
}

In my case the maximum id is 27.

Problem

How to retrieve the maximum id for one type index effectively?

Original source