问题描述
我已经在Elasticsearch中创建了索引并为文档建立了索引,但它的工作正常,但是这里的挑战是我必须根据我在下面给出的示例文档的uniqueid来获得category字段的总数。
{
"UserID":"A1001","Category":"initiated","policyno":"5221"
},{
"UserID":"A1001","Category":"pending","policyno":"5222"
},"policyno":"5223"
},{
"UserID":"A1002","Category":"completed","policyno":"5224"
}
**Sample output for UserID - "A1001"**
initiated-1
pending-2
**Sample output for UserID - "A1002"**
completed-1
如何从上述给定的Json文档中获取汇总计数,例如上述示例输出
解决方法
我建议使用术语汇总,如下所示:
{
"size": 0,"aggs": {
"By_ID": {
"terms": {
"field": "UserID.keyword"
},"aggs": {
"By_Category": {
"terms": {
"field": "Category.keyword"
}
}
}
}
}
}
以下是响应的摘要:
"hits" : {
"total" : {
"value" : 4,"relation" : "eq"
},"max_score" : null,"hits" : [ ]
},"aggregations" : {
"By_ID" : {
"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [
{
"key" : "A1001","doc_count" : 3,"By_Category" : {
"doc_count_error_upper_bound" : 0,"buckets" : [
{
"key" : "pending","doc_count" : 2
},{
"key" : "initiated","doc_count" : 1
}
]
}
},{
"key" : "A1002","doc_count" : 1,"buckets" : [
{
"key" : "completed","doc_count" : 1
}
]
}
}
]
}
}