繁体   English   中英

elasticsearch-唯一记录的最新文档的聚合统计信息

[英]elasticsearch - aggregation stats on latest document of unique records

大约有300,000个唯一用户/客户。 每个下订单都有一个文件,因此我们有数百万个文件。

每个订单文件如下所示

{
   "customer_id" : 1001,
   "order_amount" : 15.00,
   "timestamp" : 1465450000, //epoch time when order was placed
}

我需要每个唯一客户记录(customer_id)最新订单上的“统计汇总”指标,即对于每个客户,请获取最新的订单金额并执行统计汇总(忽略旧订单)

在Elasticsearch中这可能吗?

如果我正确地理解了您的要求,则应该可以进行以下操作。 由于我们有权访问查询,因此我们可以采取任何措施来限制数据集。 在我的示例中,我只是说时间戳> = 1365440000:

{
    "size": 0,
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "timestamp": {
                        "gte": 1365440000
                    }
                }
            }
        }
    },
    "aggs": {
        "customers": {
            "terms": {
                "field": "customer_id"
            },
            "aggs": {
                "order_stats": {
                    "stats": {
                        "field": "order_amount"
                    }
                }
            }
        }
    }
}

结果如下:

{
    "took": 32,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 8,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "customers": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
        {
            "key": 1001,
            "doc_count": 4,
            "order_stats": {
                "count": 4,
                "min": 13,
                "max": 15,
                "avg": 13.875,
                "sum": 55.5
            }
        },
        {
            "key": 1002,
            "doc_count": 4,
            "order_stats": {
                "count": 4,
                "min": 13.5,
                "max": 15.5,
                "avg": 14.625,
                "sum": 58.5
            }
          }
        ]
      }
   }
}

希望能帮助到你。

暂无
暂无

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

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