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

j5fpnvbx  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(290)

我已经创建了一个索引,并在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文档(如上面提到的示例输出)中获取聚合计数

dz6r00yl

dz6r00yl1#

我建议使用如下所示的术语聚合:

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

相关问题