Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch How to retrieve the maximum id

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

like image 733
proger2014 Avatar asked Jul 15 '14 12:07

proger2014


1 Answers

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.

like image 59
toringe Avatar answered Oct 30 '22 02:10

toringe