简体   繁体   English

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

[英]Get an aggregate count in elasticsearch based on particular uniqueid field

I have created an index and indexed the document in elasticsearch it's working fine but here the challenge is i have to get an aggregate count of category field based on uniqueid i have given my sample documents below.我已经创建了一个索引并在 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

How to get the aggregate count from above given Json documents like the sample output mentioned above如何从上面给定的 Json 文档中获取聚合计数,例如上面提到的示例输出

I suggest a terms aggregation as shown in the following:我建议使用术语聚合,如下所示:

{
"size": 0,
"aggs": {
    "By_ID": {
    "terms": {
        "field": "UserID.keyword"
    },
    "aggs": {
        "By_Category": {
        "terms": {
            "field": "Category.keyword"
        }
        }
    }
    }
}
}

Here is a snippet of the response:以下是回复的片段:

    "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
            }
            ]
        }
        }
    ]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM