# Aggregation on latest n records

**URL:** <https://discuss.elastic.co/t/aggregation-on-latest-n-records/271169>\
**Category:** Elasticsearch\
**Created:** [April 25, 2021, 6:00am UTC](https://discuss.elastic.co/t/aggregation-on-latest-n-records/271169 "2021-04-25T06:00:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![behzad\_dadashpour](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/behzad_dadashpour/32/46415_2.png) [@behzad\_dadashpour](https://discuss.elastic.co/u/behzad_dadashpour)\
**Post date:** [April 25, 2021, 6:00am UTC](https://discuss.elastic.co/t/aggregation-on-latest-n-records/271169/1 "2021-04-25T06:00:29Z")

</div>

Hi

Is there any way to do aggregation on latest N records?

this solution didn't work

```auto
{
   "query": {...},
   "size": N,
   "order": ...,
   "aggs": {
       ....
   }
}

```

for more detail: I want to get last 10 records from my records where "service\_name" field is "x" and then aggregate on these 10 records to find out how many of these records have "success" in "resp\_code" field.

my data is something like this:

```auto
[
  {
    "_index": "logs",
    "_type": "_doc",
    "_id": "1232525",
    "_score": 1,
    "_source": {
      "resp_body": "",
      "client_ip": "127.0.0.1",
      "resp_time": "2021-04-15T10:24:51+01:00",
      "@timestamp": "2021-04-15T05:55:00.452Z",
      "resp_code": "412",
      "service_name": "service1",
      "log_id": "1232525"
    }
  },
  {
    "_index": "logs",
    "_type": "_doc",
    "_id": "1232524",
    "_score": 1,
    "_source": {
      "resp_body": "",
      "client_ip": "127.0.0.1",
      "resp_time": "2021-04-15T10:23:51+01:00",
      "@timestamp": "2021-04-15T05:53:00.452Z",
      "resp_code": "0",
      "service_name": "service2",
      "log_id": "1232524"
    }
  },
  {
    "_index": "logs",
    "_type": "_doc",
    "_id": "1232523",
    "_score": 1,
    "_source": {
      "resp_body": "",
      "client_ip": "127.0.0.1",
      "resp_time": "2021-04-15T10:22:51+01:00",
      "@timestamp": "2021-04-15T05:52:00.452Z",
      "resp_code": "0",
      "service_name": "service1",
      "log_id": "1232523"
    }
  },
  {
    "_index": "logs",
    "_type": "_doc",
    "_id": "1232522",
    "_score": 1,
    "_source": {
      "resp_body": "",
      "client_ip": "127.0.0.1",
      "resp_time": "2021-04-15T10:21:51+01:00",
      "@timestamp": "2021-04-15T05:51:00.452Z",
      "resp_code": "0",
      "service_name": "service1",
      "log_id": "1232522"
    }
  },
  {
    "_index": "logs",
    "_type": "_doc",
    "_id": "1232521",
    "_score": 1,
    "_source": {
      "resp_body": "",
      "client_ip": "127.0.0.1",
      "resp_time": "2021-04-15T10:20:51+01:00",
      "@timestamp": "2021-04-15T05:50:00.452Z",
      "resp_code": "0",
      "service_name": "service2",
      "log_id": "1232521"
    }
  }
]

```

for example: I want to get last 2 records with "service\_name=service1" and find how many records of those have "resp\_code=0"

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 25, 2021, 9:17am UTC](https://discuss.elastic.co/t/aggregation-on-latest-n-records/271169/2 "2021-04-25T09:17:45Z")

</div>

Welcome!

I'm not sure you can do that. That being said, it should be easy to compute this on the client side as you just have to iterate over the hits.

---

<div class="post-metadata">

**Author:** ![behzad\_dadashpour](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/behzad_dadashpour/32/46415_2.png) [@behzad\_dadashpour](https://discuss.elastic.co/u/behzad_dadashpour)\
**Post date:** [April 25, 2021, 9:32am UTC](https://discuss.elastic.co/t/aggregation-on-latest-n-records/271169/3 "2021-04-25T09:32:14Z")

</div>

Hi David!

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 23, 2021, 9:32am UTC](https://discuss.elastic.co/t/aggregation-on-latest-n-records/271169/4 "2021-05-23T09:32:58Z")

</div>

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