高峰只对攀登它而不是仰望它的人来说才有真正意义。

terms分桶后的结果, 还需要根据每桶的doc_count再次分桶,该如何写后续的逻辑?

Elasticsearch | 作者 felixzxk | 发布于2019年11月09日 | 阅读数:2603

es版本是7.4.1
运行环境是centos7, docker
业务需求如下:
数据源是销售人员的拜访记录, 该记录的doc中包含createTime(拜访时间)和salesmanAutoId(业务员唯一标识);
需要按天分桶, 查出近两天的拜访总数;
然后按salesmanAutoId分桶, 主要是看销售人员的拜访次数;
再根据每个业务员拜访次数的区间分桶, 查看宏观拜访数据: 例如每天2访, 3访, 3访以上的各有多少人
最后这一步我无法实现, 或者是不是我不该按salesmanAutoId分桶?
请大神指点迷津, 万分感谢
以下是DSL
GET visited/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createTime": {
              "gte": "now-1d/d",
              "lte": "now/d"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "visit_per_day": {
      "date_histogram": {
        "field": "createTime",
        "calendar_interval": "day"
      },
      "aggs": {
        "times_per_user": {
          "terms": {
            "field": "salesmanAutoId"
          }
        }
      }
    }
  }
}
以下是结果数据:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 15,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"visit_per_day" : {
"buckets" : [
{
"key_as_string" : "2019-11-07T00:00:00.000Z",
"key" : 1573084800000,
"doc_count" : 6,
"times_per_user" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 620,
"doc_count" : 3
},
{
"key" : 17006,
"doc_count" : 2
},
{
"key" : 17323,
"doc_count" : 1
}
]
}
},
{
"key_as_string" : "2019-11-08T00:00:00.000Z",
"key" : 1573171200000,
"doc_count" : 9,
"times_per_user" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 620,
"doc_count" : 4
},
{
"key" : 17006,
"doc_count" : 3
},
{
"key" : 17323,
"doc_count" : 2
}
]
}
}
]
}
}
}
已邀请:

trycatchfinal

赞同来自:

3访以上,可以使用参数min_doc_count
{
"times_per_user": {
"terms": {
"field": "salesmanAutoId",
"min_doc_count": 3
}
}
}

fantuan

赞同来自:

用bucket selector aggregation 把访问次数分成三个桶,缺点是之后不能再按销售员分桶了。
https://www.elastic.co/guide/e ... .html

要回复问题请先登录注册