【问题标题】:Get an aggregate count in elasticsearch based on particular uniqueid field根据特定的 uniqueid 字段在 elasticsearch 中获取聚合计数
【发布时间】:2020-09-02 14:34:23
【问题描述】:

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

{
"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 文档(如上面提到的示例输出)中获取聚合计数

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-dsl


    【解决方案1】:

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

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

    这是响应的 sn-p:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-31
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多