根据特定的 uniqueid 字段获取 elasticsearch 中的聚合计数

Get an aggregate count in elasticsearch based on particular uniqueid field

我已经创建了一个索引并在 elasticsearch 中为文档编制了索引它工作正常但这里的挑战是我必须根据 uniqueid 获得类别字段的总计数我在下面给出了我的示例文档。

{
"UserID":"A1001",
"Category":"initiated",
"policyno":"5221"
},
{
"UserID":"A1001",
"Category":"pending",
"policyno":"5222"
},
{
"UserID":"A1001",
"Category":"pending",
"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,
            "sum_other_doc_count" : 0,
            "buckets" : [
            {
                "key" : "pending",
                "doc_count" : 2
            },
            {
                "key" : "initiated",
                "doc_count" : 1
            }
            ]
        }
        },
        {
        "key" : "A1002",
        "doc_count" : 1,
        "By_Category" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
            {
                "key" : "completed",
                "doc_count" : 1
            }
            ]
        }
        }
    ]
    }
}