Elasticsearch - 分组聚合 - 2个字段
Elasticsearch - Grouping aggregation - 2 fields
我的映射是
{
"myapp": {
"mappings": {
"attempts": {
"properties": {
"answers": {
"properties": {
"question_id": {
"type": "long"
},
"status": {
"type": "long"
}
}
},
"exam_id": {
"type": "long"
}
}
}
}
}
}
我想按 question_id 和状态
分组
我想知道每个 question_id 有多少状态为 1 或 2
P.S。 2次尝试可以有相同的问题
首先,您需要更新映射并将 answers
设为 nested 字段。不填写 nested
将使答案失去 question_id
字段和 status
字段之间的相关性。
{
"myapp": {
"mappings": {
"attempts": {
"properties": {
"answers": {
"type":"nested", <-- Update here
"properties": {
"question_id": {
"type": "long"
},
"status": {
"type": "long"
}
}
},
"exam_id": {
"type": "long"
}
}
}
}
}
}
您可以在子聚合中使用状态,如下所示
"aggs": {
"nested_qid_agg": {
"nested": {
"path": "answers"
},
"aggs": {
"qid": {
"terms": {
"field": "answers.question_id",
"size": 0
},
"aggs": {
"status": {
"terms": {
"field": "answers.status",
"size": 0
}
}
}
}
}
}
}
我的映射是
{
"myapp": {
"mappings": {
"attempts": {
"properties": {
"answers": {
"properties": {
"question_id": {
"type": "long"
},
"status": {
"type": "long"
}
}
},
"exam_id": {
"type": "long"
}
}
}
}
}
}
我想按 question_id 和状态
分组我想知道每个 question_id 有多少状态为 1 或 2
P.S。 2次尝试可以有相同的问题
首先,您需要更新映射并将 answers
设为 nested 字段。不填写 nested
将使答案失去 question_id
字段和 status
字段之间的相关性。
{
"myapp": {
"mappings": {
"attempts": {
"properties": {
"answers": {
"type":"nested", <-- Update here
"properties": {
"question_id": {
"type": "long"
},
"status": {
"type": "long"
}
}
},
"exam_id": {
"type": "long"
}
}
}
}
}
}
您可以在子聚合中使用状态,如下所示
"aggs": {
"nested_qid_agg": {
"nested": {
"path": "answers"
},
"aggs": {
"qid": {
"terms": {
"field": "answers.question_id",
"size": 0
},
"aggs": {
"status": {
"terms": {
"field": "answers.status",
"size": 0
}
}
}
}
}
}
}