弹性搜索中两个日期之间的分组依据并过滤max(date) [英] Group by and filter max(date) between two dates in elastic search

查看:73
本文介绍了弹性搜索中两个日期之间的分组依据并过滤max(date)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们可以在弹性搜索中按customer_id分组。

Currently we are able to group by customer_id in elastic search.

以下是文档结构

{
    "order_id":"6",
    "customer_id":"1",
    "customer_name":"shailendra",
    "mailing_addres":"shailendra@gmail.com",
    "actual_order_date":"2000-04-30",
    "is_veg":"0",
    "total_amount":"2499",
    "store_id":"276",
    "city_id":"12",
    "payment_mode":"cod",
    "is_elite":"0",
    "product":["1","2"],
    "coupon_id":"",
    "client_source":"1",
    "vendor_id":"",
    "vendor_name: "",
    "brand_id":"",
    "third_party_source":""

}

现在我们需要过滤组以查找文档

Now we need to filter the group to find the documents


  1. 两个日期之间的最后订购日期

  2. 两个日期之间的第一订购日期

我们如何实现呢?

推荐答案

您可以尝试以下查询。在每个客户群中,我们进一步过滤两个日期(这里是2016年8月)之间的所有文档,然后在date字段中运行 stats 汇总。 min 的值将是第一个订购日期,而 max 的值将是最后一个订购日期。

You can try with the query below. Within each customer bucket, we further filter all document between two dates (here I've taken the month of August 2016) and then we run a stats aggregation on the date field. The min value will be the first order date and the max value will be the last order date.

{
  "aggs": {
    "customer_ids": {
      "terms": {
        "field": "customer_id"
      },
      "aggs": {
        "date_filter": {
          "filter": {
            "range": {
              "actual_order_date": {
                "gt": "2016-08-01T00:00:00.000Z",
                "lt": "2016-09-01T00:00:00.000Z"
              }
            }
          },
          "aggs": {
            "min_max": {
              "stats": {
                "field": "actual_order_date"
              }
            }
          }
        }
      }
    }
  }
}

这篇关于弹性搜索中两个日期之间的分组依据并过滤max(date)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆