# Aggregate query on filtered array of objects across documents

**URL:** <https://discuss.elastic.co/t/aggregate-query-on-filtered-array-of-objects-across-documents/242207>\
**Category:** Elasticsearch\
**Created:** [July 22, 2020, 1:57pm UTC](https://discuss.elastic.co/t/aggregate-query-on-filtered-array-of-objects-across-documents/242207 "2020-07-22T13:57:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya\_Anirudh\_T](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aditya_anirudh_t/32/72601_2.png) [@Aditya\_Anirudh\_T](https://discuss.elastic.co/u/Aditya_Anirudh_T)\
**Post date:** [July 22, 2020, 1:57pm UTC](https://discuss.elastic.co/t/aggregate-query-on-filtered-array-of-objects-across-documents/242207/1 "2020-07-22T13:57:41Z")

</div>

Hi all,

I want to do a term aggregation on a field inside an array of objects, after filtering the array of objects on two other fields. For example, let's say I have the below documents in my database

```auto
[
    {
        "objects": [
            {
                "key1": 1,
                "key2": 2,
                "key3": 3
            }
        ]
    },
    {
        "objects": [
            {
                "key1": 1,
                "key2": 2,
                "key3": 4
            }
        ]
    },
    {
        "objects": [
            {
                "key1": 1,
                "key2": 5,
                "key3": 3
            }
        ]
    }
]

```

I want to do a "term" aggregation on `key3` field where `key1` and `key2` are `1` and `2` respectively.

In this example, the query should return a count of 1 for 3 and 4 each.

The filter itself needs a nested and query and then I am stuck in figuring out how to build rest of the aggregate query inside it.

---

<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:** [August 19, 2020, 1:57pm UTC](https://discuss.elastic.co/t/aggregate-query-on-filtered-array-of-objects-across-documents/242207/2 "2020-08-19T13:57:46Z")

</div>

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