Condition in aggregation does not filter the hit result

Hi guys.

I am trying to get all the record having the latest created time so I come up with this query:

  "query": {
  "term": {
    "username": "xxx"
  "aggs": {
    "sessions": {
      "terms" : { "field" : "id", "size": 1000 },
      "aggs": {
        "created_time": {
          "min": {
            "field": "created_time"

But the hit array still return two records with different created time, only the aggs section return the latest created time records.
Is there any way to filter the hits with created time? Because I need to get all the fields in hit array missing in aggregation array.

Take a look at the top_hits aggregation

However, I think another way to solve your problem would be to use field collapsing instead, where you sort by created time.

Thank you Alex.

Both top_hits and field collapsing return expected result.

But I think top hits solution is more suitable for my situation because I need aggregation result as well.


1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.