# Aggregations - Average - based on some filters

**URL:** <https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837>\
**Category:** Elasticsearch\
**Created:** [March 14, 2018, 5:44am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837 "2018-03-14T05:44:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aneesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aneesh/32/37819_2.png) [@aneesh](https://discuss.elastic.co/u/aneesh)\
**Post date:** [March 14, 2018, 5:44am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837/1 "2018-03-14T05:44:46Z")

</div>

Hi

I have an elastic query

```
get testaggregate/_search
{
  "from":0,
  "size":1000,
  "query":{
    "bool":{
      "must":[
          {"match": {"uCycleId": "111"}},
          {"match": {"uTestId": "222"}},
          {"match": {"TestResultSummarySubSubject.iSubjectId": 100}}
        ]
    }
  },
  "aggs" : {
        "filter" : { "match": { "TestResultSummarySubSubject.iSubjectId": 100 } },
        "avg_testpoints" : { "avg" : { "field" : "TestResultSummarySubSubject.vResultAttribute.Result" } }
    }
}

```

The data saved is like

```
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 2.8768208,
    "hits": [
      {
        "_index": "testaggregate",
        "_type": "testaggregate",
        "_id": "1",
        "_score": 2.8768208,
        "_source": {
          "uTestTokenId": "1",
          "uCycleId": "111",
          "uTestId": "222",
          "TestResultSummary": [
            {
              "uExecutionId": "123",
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 7,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            }
          ],
          "TestResultSummarySubSubject": [
            {
              "iSubjectId": 100,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 10,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            },
            {
              "iSubjectId": 101,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 11,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            },
            {
              "iSubjectId": 102,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 12,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            },
            {
              "iSubjectId": 103,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 13,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            }
          ]
        }
      },
      {
        "_index": "testaggregate",
        "_type": "testaggregate",
        "_id": "2",
        "_score": 2.8768208,
        "_source": {
          "uTestTokenId": "2",
          "uCycleId": "111",
          "uTestId": "222",
          "TestResultSummary": [
            {
              "uExecutionId": "121",
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,     
                  "Result": 8.5,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            }
          ],
          "TestResultSummarySubSubject": [
            {
              "iSubjectId": 100,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 4,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            },
            {
              "iSubjectId": 101,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 5,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            },
            {
              "iSubjectId": 102,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 6,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            },
            {
              "iSubjectId": 103,
              "vResultAttribute": [
                {
                  "PartiallyCorrectTasks": 4,
                  "Result": 7,
                  "TestObjectsCorrect": 16,
                  "TestMaxPoint": 31
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

```

I need to find the average of TestResultSummarySubSubject.vResultAttribute.Result, for those having "TestResultSummarySubSubject.iSubjectId" = 100 in TestResultSummarySubSubject attribute

or

Average: group by iSubjectId

Above query is giving avarage for all without filter by iSubjectId

Thanks  
Aneesh L

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [March 14, 2018, 10:15am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837/2 "2018-03-14T10:15:20Z")

</div>

You need the avg aggregation to be a sub aggregation of the filter aggregation then.

---

<div class="post-metadata">

**Author:** ![aneesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aneesh/32/37819_2.png) [@aneesh](https://discuss.elastic.co/u/aneesh)\
**Post date:** [March 14, 2018, 10:24am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837/3 "2018-03-14T10:24:10Z")

</div>

Yes. How I can do it. Any reference.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [March 14, 2018, 10:40am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837/4 "2018-03-14T10:40:36Z")

</div>

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html#\_structuring\_aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html#_structuring_aggregations), you can put another `aggregations` element on the same level as `filter` and this will be treated as a sub aggregation of your filter aggregation.

---

<div class="post-metadata">

**Author:** ![aneesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aneesh/32/37819_2.png) [@aneesh](https://discuss.elastic.co/u/aneesh)\
**Post date:** [March 15, 2018, 3:45am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837/5 "2018-03-15T03:45:56Z")

</div>

```
get testaggregate/_search
{
  "from":0,
  "size":0,
  "query":{
    "bool":{
      "must":[
          {"match": {"uCycleId": "111"}},
          {"match": {"uTestId": "222"}},
          {"match": {"TestResultSummarySubSubject.iSubjectId": 100}}
        ]
    }
  },
  "aggs" : {
        "subject_id": { "terms": { "field": "TestResultSummarySubSubject.vResultAttribute.iSubjectId" },
        "aggs" : {
            "avg_subject":{ "avg" : { "field" : "TestResultSummarySubSubject.vResultAttribute.Result", "missing": 0} }
        }
        }
    }
}

```

This is not giving proper result

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "subject_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 100,
          "doc_count": 2,
          "avg_subject": {
            "value": 9.75
          }
        },
        {
          "key": 101,
          "doc_count": 2,
          "avg_subject": {
            "value": 9.75
          }
        },
        {
          "key": 102,
          "doc_count": 2,
          "avg_subject": {
            "value": 9.75
          }
        },
        {
          "key": 103,
          "doc_count": 2,
          "avg_subject": {
            "value": 9.75
          }
        }
      ]
    }
  }

```

All the average values are same, Average for all subjects together

---

<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:** [April 12, 2018, 3:46am UTC](https://discuss.elastic.co/t/aggregations-average-based-on-some-filters/123837/6 "2018-04-12T03:46:15Z")

</div>

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