Aggregation query on nested objects

Hello All,

I have the following json object

 {
 "responses": [
{
  "question": {
    "name": "Please rate your experience"
  },
  "answers": [
    {
      "name": "4/10"
    }
  ]
},
{
  "question": {
    "name": "Designation"
  },
  "answers": [
    {
      "name": "Student"
    }
  ]
},
{
  "question": {
    "name": "Did you attempt any experiments ?"
  },
  "answers": [
    {
      "name": "No"
    }
  ]
  }
 ]
}

Here I would like to get the count of each unique answer to its corresponding question

I tried out following aggregation query

{
        "aggs": {
            "questions": {
                "terms": {
                    "field": "responses.question.name",
                    "order" : { "_count" : "asc" },
                    "size": 0
                    },
                    "aggs": {
            "answers": {
                "terms": {
                    "field": "responses.answers.name"
                    }
                    
            }
          
            
        }
                    
            }
          
            
        }
    }

This is listing count of all the available answers for every questions as below

 {
          "took": 59,
        "timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
        {
            "_index": "post_question",
            "_type": "feedback",
            "_id": "AV5qHAAXBWUeGnRiAgw2",
            "_score": 1,
            "_source": {
                "responses": [
                    {
                        "question": {
                            "name": "Please rate your experience"
                        },
                        "answers": [
                            {
                                "name": "4/10"
                            }
                        ]
                    },
                    {
                        "question": {
                            "name": "Designation"
                        },
                        "answers": [
                            {
                                "name": "Student"
                            }
                        ]
                    },
                    {
                        "question": {
                            "name": "Did you attempt any experiments ?"
                        },
                        "answers": [
                            {
                                "name": "No"
                            }
                        ]
                    }
                ]
            }
        }
    ]
},
"aggregations": {
    "questions": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "Designation",
                "doc_count": 1,
                "answers": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "4/10",
                            "doc_count": 1
                        },
                        {
                            "key": "No",
                            "doc_count": 1
                        },
                        {
                            "key": "Student",
                            "doc_count": 1
                        }
                    ]
                }
            },
            {
                "key": "Did you attempt any experiments ?",
                "doc_count": 1,
                "answers": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "4/10",
                            "doc_count": 1
                        },
                        {
                            "key": "No",
                            "doc_count": 1
                        },
                        {
                            "key": "Student",
                            "doc_count": 1
                        }
                    ]
                }
            },
            {
                "key": "Please rate your experience",
                "doc_count": 1,
                "answers": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                        {
                            "key": "4/10",
                            "doc_count": 1
                        },
                        {
                            "key": "No",
                            "doc_count": 1
                        },
                        {
                            "key": "Student",
                            "doc_count": 1
                        }
                    ]
                }
            }
        ]
    }
   }
   }

Can somebody look into and help me out

Have a look at nested objects and nested aggregations

--Alex

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