Aggregations - Average - based on some filters

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

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

Yes. How I can do it. Any reference.

See 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.

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

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