ElasticSearch 中一个键中所有嵌套对象的聚合
Aggregations over all nested objects in a key in ElasticSearch
我在索引中有一个文档 'submissions' 看起来像这样,
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "submissions",
"_type" : "_doc",
"_id" : "90_169",
"_score" : 1.0,
"_source" : {
"id" : "90_169",
"locked" : false,
"account_id" : 5,
"campaign_id" : 90,
"contact_id" : 1179,
"submission_id" : 169,
"answers" : [
{
"question_id" : 8451,
"answer_bool" : true
},
{
"question_id" : 8452,
"answer_bool" : false
},
{
"question_id" : 8453,
"answer_bool" : true
},
{
"question_id" : 8454,
"answer_bool" : false
}
]
}
}
]
}
}
这只是一份文件。
我想汇总所有问题以获得最终的桶聚合
它显示每个 question_id.
的真假数
关于如何实现这一目标的任何见解?
您需要使用nested aggregation along with terms and filter聚合
添加包含索引映射、数据、搜索查询和搜索结果的工作示例
索引映射:
{
"mappings": {
"properties": {
"answers": {
"type": "nested"
}
}
}
}
索引数据:
{
"id": "90_169",
"locked": false,
"account_id": 5,
"campaign_id": 90,
"contact_id": 1179,
"submission_id": 169,
"answers": [
{
"question_id": 8451,
"answer_bool": true
},
{
"question_id": 8452,
"answer_bool": false
},
{
"question_id": 8453,
"answer_bool": true
},
{
"question_id": 8454,
"answer_bool": false
}
]
}
{
"id": "90_169",
"locked": false,
"account_id": 5,
"campaign_id": 90,
"contact_id": 1179,
"submission_id": 169,
"answers": [
{
"question_id": 8451,
"answer_bool": true
},
{
"question_id": 8452,
"answer_bool": false
},
{
"question_id": 8453,
"answer_bool": true
},
{
"question_id": 8454,
"answer_bool": true
}
]
}
{
"id": "90_169",
"locked": false,
"account_id": 5,
"campaign_id": 90,
"contact_id": 1179,
"submission_id": 169,
"answers": [
{
"question_id": 8451,
"answer_bool": true
},
{
"question_id": 8452,
"answer_bool": false
},
{
"question_id": 8453,
"answer_bool": true
},
{
"question_id": 8454,
"answer_bool": true
}
]
}
搜索查询:
{
"size": 0,
"aggs": {
"nested_Agg": {
"nested": {
"path": "answers"
},
"aggs": {
"id": {
"terms": {
"field": "answers.question_id"
},
"aggs": {
"true_count": {
"filter": {
"term": {
"answers.answer_bool": "true"
}
}
},
"false_count": {
"filter": {
"term": {
"answers.answer_bool": "false"
}
}
}
}
}
}
}
}
}
搜索结果:
"aggregations": {
"nested_Agg": {
"doc_count": 12,
"id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 8451,
"doc_count": 3,
"false_count": {
"doc_count": 0
},
"true_count": {
"doc_count": 3
}
},
{
"key": 8452,
"doc_count": 3,
"false_count": {
"doc_count": 3
},
"true_count": {
"doc_count": 0
}
},
{
"key": 8453,
"doc_count": 3,
"false_count": {
"doc_count": 0
},
"true_count": {
"doc_count": 3
}
},
{
"key": 8454,
"doc_count": 3,
"false_count": {
"doc_count": 1
},
"true_count": {
"doc_count": 2
}
}
]
}
}
}
我在索引中有一个文档 'submissions' 看起来像这样,
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "submissions",
"_type" : "_doc",
"_id" : "90_169",
"_score" : 1.0,
"_source" : {
"id" : "90_169",
"locked" : false,
"account_id" : 5,
"campaign_id" : 90,
"contact_id" : 1179,
"submission_id" : 169,
"answers" : [
{
"question_id" : 8451,
"answer_bool" : true
},
{
"question_id" : 8452,
"answer_bool" : false
},
{
"question_id" : 8453,
"answer_bool" : true
},
{
"question_id" : 8454,
"answer_bool" : false
}
]
}
}
]
}
}
这只是一份文件。
我想汇总所有问题以获得最终的桶聚合 它显示每个 question_id.
的真假数关于如何实现这一目标的任何见解?
您需要使用nested aggregation along with terms and filter聚合
添加包含索引映射、数据、搜索查询和搜索结果的工作示例
索引映射:
{
"mappings": {
"properties": {
"answers": {
"type": "nested"
}
}
}
}
索引数据:
{
"id": "90_169",
"locked": false,
"account_id": 5,
"campaign_id": 90,
"contact_id": 1179,
"submission_id": 169,
"answers": [
{
"question_id": 8451,
"answer_bool": true
},
{
"question_id": 8452,
"answer_bool": false
},
{
"question_id": 8453,
"answer_bool": true
},
{
"question_id": 8454,
"answer_bool": false
}
]
}
{
"id": "90_169",
"locked": false,
"account_id": 5,
"campaign_id": 90,
"contact_id": 1179,
"submission_id": 169,
"answers": [
{
"question_id": 8451,
"answer_bool": true
},
{
"question_id": 8452,
"answer_bool": false
},
{
"question_id": 8453,
"answer_bool": true
},
{
"question_id": 8454,
"answer_bool": true
}
]
}
{
"id": "90_169",
"locked": false,
"account_id": 5,
"campaign_id": 90,
"contact_id": 1179,
"submission_id": 169,
"answers": [
{
"question_id": 8451,
"answer_bool": true
},
{
"question_id": 8452,
"answer_bool": false
},
{
"question_id": 8453,
"answer_bool": true
},
{
"question_id": 8454,
"answer_bool": true
}
]
}
搜索查询:
{
"size": 0,
"aggs": {
"nested_Agg": {
"nested": {
"path": "answers"
},
"aggs": {
"id": {
"terms": {
"field": "answers.question_id"
},
"aggs": {
"true_count": {
"filter": {
"term": {
"answers.answer_bool": "true"
}
}
},
"false_count": {
"filter": {
"term": {
"answers.answer_bool": "false"
}
}
}
}
}
}
}
}
}
搜索结果:
"aggregations": {
"nested_Agg": {
"doc_count": 12,
"id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 8451,
"doc_count": 3,
"false_count": {
"doc_count": 0
},
"true_count": {
"doc_count": 3
}
},
{
"key": 8452,
"doc_count": 3,
"false_count": {
"doc_count": 3
},
"true_count": {
"doc_count": 0
}
},
{
"key": 8453,
"doc_count": 3,
"false_count": {
"doc_count": 0
},
"true_count": {
"doc_count": 3
}
},
{
"key": 8454,
"doc_count": 3,
"false_count": {
"doc_count": 1
},
"true_count": {
"doc_count": 2
}
}
]
}
}
}