簡體   English   中英

根據特定的 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
            }
            ]
        }
        }
    ]
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM