# Filter with criteria before sum aggregation

**URL:** <https://discuss.elastic.co/t/filter-with-criteria-before-sum-aggregation/256051>\
**Category:** Elasticsearch\
**Created:** [November 19, 2020, 11:17pm UTC](https://discuss.elastic.co/t/filter-with-criteria-before-sum-aggregation/256051 "2020-11-19T23:17:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![erv](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@erv](https://discuss.elastic.co/u/erv)\
**Post date:** [November 19, 2020, 11:17pm UTC](https://discuss.elastic.co/t/filter-with-criteria-before-sum-aggregation/256051/1 "2020-11-19T23:17:35Z")

</div>

Hey everyone! I'm having a hard time with the following issue. Suppose I have the following three documents:

```auto
 {
     "entity_id": "A",
     "stringField1": "SF1A",
     "stringField2": "SF2",
     "numericField": 10,
     "updated_at": "2020-10-18", 
 }
 {
      "entity_id": "A",
      "stringField1": "SF1A",
      "stringField2": "SF2",
      "numericField": 1000,
      "updated_at": "2020-11-18", 
 }
{
      "entity_id": "B",
      "stringField1": "SF1B",
      "stringField2": "SF2",
      "numericField": 1000,
      "updated_at": "2020-11-18", 
 }

```

I'd like to run an aggregation of `numericField` values for example for a specific value of stringField1/stringField2 but ONLY considering the "newest" document for a given an `entity_id`.

I read here about field collapsing [here](https://discuss.elastic.co/t/es-design-regarding-duplicates-across-indexes/117852), and it worked if I want to do a term query.

If e.g. I wanted to aggregate for `stringField1 == "SF1A"` and `stringField == "SF1B"`, I could do:

```auto
 "aggs": {
     "first_agg": {
         "aggs": {
             "my_numericfield_agg": {
                 "sum": {
                     "field": "numericField"
                 }
             }
         },
         "filter": {
             "term": {
                 "stringField1": "SF1A"
             }
         }
     },
     "second_agg": {
         "aggs": {
             "my_numericfield_agg": {
                 "sum": {
                     "field": "numericField"
                 }
             }
         },
         "filter": {
             "term": {
                 "stringField1": "SF1B"
             }
         }
     }
 }

```

But this would consider **all** (old and new) documents for the same entity\_id.

Is there a way to do this aggregation but only considering **the latest** (by "updated\_at") document **per "entity\_id"**? (This is **before** doing the sum aggregation)

I've read about top hits [here](https://discuss.elastic.co/t/top-hits-bucket-agg-vs-field-collapse-5-3/117040) but couldn't figure out yet how to apply it to my use case.

Thank you very much!

---

<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:** [December 17, 2020, 11:17pm UTC](https://discuss.elastic.co/t/filter-with-criteria-before-sum-aggregation/256051/2 "2020-12-17T23:17:43Z")

</div>

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