Elasticsearch通过嵌套对象属性进行聚合

时间:2019-05-06 11:31:14

标签: elasticsearch

我有一个elasticsearch索引,我将所有调查响应都存储为json对象。

示例:

{
"@timestamp": "2019-04-29T07:46:34.184Z",
"id": 54448437,
"questionResponses": [
  {
    "questionId": 1000000000,
    "insights": [
      {
        "categoryId": 50071,
        "feature": "experience",
        "tonality": "positive",
        "score": 1,
        "id": 253042338,
        "opinion": "great"
      },
      {
        "categoryId": 50071,
        "feature": "Overall Experience",
        "tonality": "negative",
        "score": -1,
        "id": 253042357,
        "opinion": "very misleading"
      },
      {
        "categoryId": 50015,
        "feature": "video",
        "tonality": "negative",
        "score": -1,
        "id": 253042358,
        "opinion": "misleading"
      },
      {
        "categoryId": 50009,
        "feature": "classes",
        "tonality": "neutral",
        "score": -1,
        "id": 253042364,
        "opinion": "didn't even get to attend."
      }
    ],
    "response": "While I had a great experience with both my girls, promising a NICU and having one available and open are two different things and very misleading. This whole video feels misleading to me. My birth plan was between me and my doctor both times and the hospital was just my location. They do offer classes but they fill super fast and we're very limited that I didn't even get to attend. I loved the attention and care that I received while there ...",
    "id": 425994747
  }
],
"source_name": "Survey 1"
}

现在,我有一个要求,我需要根据每个类别ID的见解得分总和来获取文档计数。

我需要获取正面文件和负面文件计数。

输出应类似于

{"categoryId": 50071,
"positiveDocumentCount": 0,
"neutralDocumentCount": 1,
"negativeDocumentCount": 0}

{"categoryId": 50015,
"positiveDocumentCount": 0,
"neutralDocumentCount": 0,
"negativeDocumentCount": 1}

1 个答案:

答案 0 :(得分:0)

确保questionResponses和questionResponses.insights已嵌套

尝试一下-

{  
   "size":0,
   "aggs":{  
      "questionResponses":{  
         "nested":{  
            "path":"questionResponses"
         },
         "aggs":{  
            "insights":{  
               "nested":{  
                  "path":"questionResponses.insights"
               },
               "aggs":{  
                  "cats":{  
                     "terms":{  
                        "field":"questionResponses.insights.categoryId"
                     },
                     "aggs":{  
                        "positiveDocumentCount":{  
                           "filter":{  
                              "term":{  
                                 "questionResponses.insights.tonality.keyword":"positive"
                              }
                           }
                        },
                        "neutralDocumentCount":{  
                           "filter":{  
                              "term":{  
                                 "questionResponses.insights.tonality.keyword":"neutral"
                              }
                           }
                        },
                        "negativeDocumentCount":{  
                           "filter":{  
                              "term":{  
                                 "questionResponses.insights.tonality.keyword":"negative"
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}