根据特定的uniqueid字段在Elasticsearch中获取汇总计数

问题描述

我已经在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
            }
            ]
        }
        }
    ]
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...