# Aggregates on collapse

**URL:** <https://discuss.elastic.co/t/aggregates-on-collapse/258883>\
**Category:** Elasticsearch\
**Created:** [December 16, 2020, 3:17pm UTC](https://discuss.elastic.co/t/aggregates-on-collapse/258883 "2020-12-16T15:17:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philh-gbg](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@Philh-gbg](https://discuss.elastic.co/u/Philh-gbg)\
**Post date:** [December 16, 2020, 3:17pm UTC](https://discuss.elastic.co/t/aggregates-on-collapse/258883/1 "2020-12-16T15:17:33Z")

</div>

New user looking for help, I know that aggregates don't work on the collapse results but I can't seem to find a way to achieve the same affect directly. We have a relatively simple dataset that contains multiple entries for the same key 'referencenumber' and I want to build a set of counts for the final 'status' of each 'referencenumber'. So if they did work together it would look like;

```auto
GET indexname*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createddate": {
              "gte": "2020-12-01",
              "lte": "2020-12-15"
            }
          }
        }
      ]
    }
  },
  "collapse": {
    "field": "referencenumber",
    "inner_hits": {
      "name": "most_recent",
      "size": 1,
      "sort": [
        {
          "timestamp": "desc"
        }
      ]
    }
  },
  "aggs": {
    "JourneyResults": {
      "terms": {
        "field": "journeyresult.status"
      }
    }
  }
}

```

Is there a way to do this only using aggs?

---

<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:** [January 13, 2021, 3:17pm UTC](https://discuss.elastic.co/t/aggregates-on-collapse/258883/2 "2021-01-13T15:17:36Z")

</div>

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