# Aggregating array of objects

**URL:** <https://discuss.elastic.co/t/aggregating-array-of-objects/160745>\
**Category:** Elasticsearch\
**Created:** [December 13, 2018, 1:26pm UTC](https://discuss.elastic.co/t/aggregating-array-of-objects/160745 "2018-12-13T13:26:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp90](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sp90](https://discuss.elastic.co/u/sp90)\
**Post date:** [December 13, 2018, 1:26pm UTC](https://discuss.elastic.co/t/aggregating-array-of-objects/160745/1 "2018-12-13T13:26:42Z")

</div>

Im trying to figure out what im doing wrong, i have collected the following, **"Sub set of data"** , **"Current query"** , **"Current output"** & **"Desired output"** if more is required please let me know.

**This is how my data objects look**

```
[{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247", // As they are, this is the key to groupBy
      "option_answer": 5, // Represent the index of the choosen option
      "type": "OPINION_SCALE" // Opinion scales are 0-10 (meaning elleven options)
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS" // Labeled questions are 0-x (they can change it from survey to survey)
    }
  ],
  "survey_id": "test"
},
{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247",
      "option_answer": 0,
      "type": "OPINION_SCALE"
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS"
    }
  ],
  "survey_id": "test"
}]

```

**This is how my current query/agg object looks**

```
{
  "query": {
    "match": {
      "survey_id": "test"
    }
  },
  "aggs": {
    "agg_survey_answers": {
      "terms": {
        "field": "survey_answers.id.keyword"
      },
      "aggs": {
        "agg_option_answer": {
          "terms": {
            "field": "survey_answers.option_answer"
          }
        }
      }
    }
  }
}

```

**Current output**

```
{
  "agg_survey_answers": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [{
      "key": "9ca01568e8dbb247",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 3,
          "doc_count": 2
        }, {
          "key": 0,
          "doc_count": 1
        }, {
          "key": 5,
          "doc_count": 1
        }]
      }
    }, {
      "key": "ba37125ec32b2a99",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 3,
          "doc_count": 2
        }, {
          "key": 0,
          "doc_count": 1
        }, {
          "key": 5,
          "doc_count": 1
        }]
      }
    }]
  }
}

```

**Desired output**

```
{
  "agg_survey_answers": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [{
      "key": "9ca01568e8dbb247",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 0,
          "doc_count": 1
        }, {
          "key": 5,
          "doc_count": 1
        }]
      }
    }, {
      "key": "ba37125ec32b2a99",
      "doc_count": 2,
      "agg_option_answer": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [{
          "key": 3,
          "doc_count": 2
        }]
      }
    }]
  }
}
```

---

<div class="post-metadata">

**Author:** ![balumurari1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/balumurari1/32/39203_2.png) [@balumurari1](https://discuss.elastic.co/u/balumurari1)\
**Post date:** [December 14, 2018, 6:23am UTC](https://discuss.elastic.co/t/aggregating-array-of-objects/160745/2 "2018-12-14T06:23:37Z")

</div>

Hello simon,

can you please elaborate ,what type of query to be generated with field names like,  
Ex:  
I need to groupby id to get survey\_answers.... etc

so that it will be easy to form query and give you the solution.

---

<div class="post-metadata">

**Author:** ![sp90](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sp90](https://discuss.elastic.co/u/sp90)\
**Post date:** [December 14, 2018, 8:20am UTC](https://discuss.elastic.co/t/aggregating-array-of-objects/160745/3 "2018-12-14T08:20:18Z")

</div>

So given this document structure

```
{
  "survey_answers": [
    {
      "id": "9ca01568e8dbb247",
      "option_answer": 0,
      "type": "OPINION_SCALE"
    },
    {
      "id": "ba37125ec32b2a99",
      "option_answer": 3,
      "type": "LABELED_QUESTIONS"
    }
  ],
  "survey_id": "test"
}

```

I would like to aggregate each object in the "survey\_answers" array with the same array in other database entries

right now it counts across all the objects in "survey\_answers" but it should only count like this:

entry1 -\> survey\_answers[0]  
Should be aggregated with  
entry2 -\> survey\_answers[0]

whereas it counts it all together right now so it doesn't care about the index in survey\_answers it just count all sub objects together in one big mashup

---

<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 11, 2019, 8:20am UTC](https://discuss.elastic.co/t/aggregating-array-of-objects/160745/4 "2019-01-11T08:20:19Z")

</div>

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