elasticsearch对字段进行截取字符串进行聚合

最终dsl

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "taskCreatedDate": {     // es 存储时用的utc
              "gte": "2019-09-30T16:00:00Z",  
              "lte": "2019-11-30T16:00:00Z"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "sales_per_month": {
      "date_histogram": {   //es 聚合显示时候,主要添加时区装换,否则容易出现跨月显示问题
        "field": "taskCreatedDate",
        "interval": "month",
        "time_zone": "+08:00",
        "format": "yyyy-MM"
      },
      "aggs": {
        "domain_report": {
          "terms": {
            "script": {
              "source": "def email = doc['email'].value;def beginIndex =email.indexOf('@');def domain = email.substring(beginIndex+1);return domain"
            },
            "size": 10,    // 设置条目数量
            "order": {
              "_count": "desc"    
            }
          },
          "aggs": {
            "domain_report_count": {
              "value_count": {
                "field": "_index"
              }
            },
            "hard_bounce": {
              "filter": {
                "term": {
                  "actionStatus": "HARD_BOUNCE"
                }
              },
              "aggs": {
                "hard_bounce_count": {
                  "value_count": {
                    "field": "_index"
                  }
                }
              }
            },
            "system_bounce": {
              "filter": {
                "term": {
                  "actionStatus": "SYSTEM_BOUNCE"
                }
              },
              "aggs": {
                "system_bounce_count": {
                  "value_count": {
                    "field": "_index"
                  }
                }
              }
            },
            "hard_percentage": {
              "bucket_script": {
                "buckets_path": {
                  "hard_bounce_count": "hard_bounce>hard_bounce_count",
                  "delivery_count": "domain_report_count"
                },
                "script": "params.hard_bounce_count/ params.delivery_count* 100"
              }
            },
            "system_hard_percentage": {
              "bucket_script": {
                "buckets_path": {
                  "system_bounce_count": "system_bounce>system_bounce_count",
                  "delivery_count": "domain_report_count"
                },
                "script": "params.system_bounce_count/ params.delivery_count* 100"
              }
            }
          }
        }
      }
    }
  }
}

原始需求

对所有退信邮箱的域名进行terms聚合,email 采用keword 存储.一般情况terms 只对特定确定字段进行聚合操作。本需求需要对这个特定字段进行部分截取,然后对这个截取后得到的域名进行聚合统计。在关系型数据库中一般可以通过视图来解决。经过一定的官方文档阅读,es terms agg api居然提供了terms agg script 功能 https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-terms-aggregation.html #script 章节,采用painless 的语法进行脚本定制.

遇到的坑

  1. 获取 email 值一定要加 .value 方法,不然会报Unable to find dynamic method [substring] with [1] arguments for class [org.elasticsearch.index.fielddata.ScriptDocValues.Strings] 异常,明明 email的mapping 设置的是keyword,莫非和String不是一个东西?
  2. substring 的 第二个s要小写;
  3. 跨月聚合时,注意添加时区偏移;
  4. bucket_script 的参数变量一定要是value类型的,一般使用count,sum等聚合

资料

painless api

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值