Čamo
Čamo

Reputation: 4172

How to get max date value of returned result in Elasticsearch

Can somebody tell me please how to get latest date value from current result set with regards to limit in Elasticsearch? It means not the max value of all item regardless of limit but only the one from limited result. I have this condition:

{
    "query": {
        "range": {
            "publishDate": {"lte": "2018-10-10"}
        }    
    },
    "size": 20
}

Need max value from current limited result. Not other out of this range.

Thanks.

Upvotes: 1

Views: 4842

Answers (1)

Val
Val

Reputation: 217344

You can do this with a max metric aggregation

{
  "query": {
    "range": {
      "publishDate": {
        "lte": "2018-10-10"
      }
    }
  },
  "size": 20,
  "aggs": {
    "max_date": {                      <--- add this
      "max": {
        "field": "publishDate"
      }
    }
  }
}

Upvotes: 3

Related Questions