javascript - 在弹性搜索中聚合对象数组
问题描述
我试图弄清楚我做错了什么,我收集了以下内容,“子数据集”,“当前查询”,“当前输出”和“所需输出”如果需要更多,请告诉我。
这就是我的数据对象的样子
[{
"survey_answers": [
{
"id": "9ca01568e8dbb247", // As they are, this is the key to groupBy
"option_answer": 5, // Represent the index of the choosen option
"type": "OPINION_SCALE" // Opinion scales are 0-10 (meaning elleven options)
},
{
"id": "ba37125ec32b2a99",
"option_answer": 3,
"type": "LABELED_QUESTIONS" // Labeled questions are 0-x (they can change it from survey to survey)
}
],
"survey_id": "test"
},
{
"survey_answers": [
{
"id": "9ca01568e8dbb247",
"option_answer": 0,
"type": "OPINION_SCALE"
},
{
"id": "ba37125ec32b2a99",
"option_answer": 3,
"type": "LABELED_QUESTIONS"
}
],
"survey_id": "test"
}]
这就是我当前的查询/聚合对象的外观
{
"query": {
"match": {
"survey_id": "test"
}
},
"aggs": {
"agg_survey_answers": {
"terms": {
"field": "survey_answers.id.keyword"
},
"aggs": {
"agg_option_answer": {
"terms": {
"field": "survey_answers.option_answer"
}
}
}
}
}
}
电流输出
{
"agg_survey_answers": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "9ca01568e8dbb247",
"doc_count": 2,
"agg_option_answer": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": 3,
"doc_count": 2
}, {
"key": 0,
"doc_count": 1
}, {
"key": 5,
"doc_count": 1
}]
}
}, {
"key": "ba37125ec32b2a99",
"doc_count": 2,
"agg_option_answer": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": 3,
"doc_count": 2
}, {
"key": 0,
"doc_count": 1
}, {
"key": 5,
"doc_count": 1
}]
}
}]
}
}
期望的输出
{
"agg_survey_answers": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "9ca01568e8dbb247",
"doc_count": 2,
"agg_option_answer": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": 0,
"doc_count": 1
}, {
"key": 5,
"doc_count": 1
}]
}
}, {
"key": "ba37125ec32b2a99",
"doc_count": 2,
"agg_option_answer": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": 3,
"doc_count": 2
}]
}
}]
}
}
解决方案
推荐阅读
- angular - Angular firestore - 在查询集合中的数组后从同一属性字段获取所有数据
- javascript - 单元测试没有任何意义
- javascript - 在谷歌脚本的电子表格中获取所选下拉列表的值
- zap - 提取在 Jenkins 代理上的容器中运行的 ZAP 报告(基于 docker)
- vue.js - 使用 vue 和 axios 从 API 数据中获取每个对象
- javascript - 只执行一次“onscroll”功能
- reactjs - 扩展 React.Component 与扩展组件
- r - 在 Mac Catallina 上安装 RMySQL 包
- javascript - 在一行上重命名导入数组的键(很像解构)
- javascript - 我无法在 React 中将道具从父母传递给孩子