嵌套对象上的聚合查询

时间:2017-09-10 09:08:59

标签: elasticsearch

我有以下json对象,我试图找出所有问题的总答案

{
   "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"
            }
         ]
      }
   ]
}

在这里,我想得到相应问题的每个唯一答案的计数

我尝试了以下聚合查询

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

这是列出每个问题的所有可用答案的计数,如下所示

{
   "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
                     }
                  ]
               }
            }
         ]
      }
   }
}

有人可以调查并帮助我

1 个答案:

答案 0 :(得分:0)

您的数据模型是什么?它只包含pacman -Syuu patch ln -s "c:\python27\python.exe" /usr/bin/python export BAZEL_SH=c:/tools/msys64/usr/bin/bash.exe "C:\Documents and Settings\All Users\chocolatey\bin\bazel.exe" build --config=opt --config=win-cuda //tensorflow/tools/pip_package:build_pip_package 吗?如果是这样,请考虑将每个响应编入索引作为单独的文档。然后,您可以对问题进行responses聚合,然后在terms聚合内对问题进行分组。您将在top hits中看到答案的计数。见https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html