25 Elasticsearch Terms Aggregation 实战

查询需求

我需要根据我传入的账号id集合和时间范围去查询,查询的结果是要根据账号id分组去统计总花费,曝光量,点击量,询盘量,询盘成本(花费/询盘量)。

DSL分析输出

1、插入参数是账号id集合和一个时间范围,所以我们选择terms 和 range 来进行过滤

2、要统计所以需要aggs聚合

3、聚合的时候是要按照账号id分组一定不要忘了

所以最后正确输出的DSL为

XML 复制代码
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
          { "terms": {
      "account_id.keyword": [
        "1111111"
      ]
    }}
     
           ,
   {
      "range": {
      "report_date": {
        "gte": "2026-03-03",
        "lte": "2026-05-03"
      }
    }
   }
      ]
    }
  },
  
  "aggs": {
    "by_account": {
      "terms": {
        "field": "account_id.keyword",
        "size": 1000
      },
      "aggs": {
        "total_sum_inquiry":   { "sum": { "field": "inquiry" } },
        "total_sum_clicks":    { "sum": { "field": "clicks" } },
        "total_sum_exposure":  { "sum": { "field": "exposure" } },
        "total_sum_cost_fee":  { "sum": { "field": "cost_fee" } },
        "total_inquiry_cost": {
        "bucket_script": {
            "buckets_path": {
            "total_cost": "total_sum_cost_fee",
            "total_inquiry": "total_sum_inquiry"
          },
           "script": "if (params.total_inquiry == 0) return 0; return params.total_cost / params.total_inquiry;"
          }
        }
      }
    }
  }
  
}

DSL常见异常

agg中使用script错误

DSL语句

XML 复制代码
  "aggs": {
    "total_sum_inquiry":   { "sum": { "field": "inquiry" } },
    "total_sum_clicks":    { "sum": { "field": "clicks" } },
    "total_sum_exposure":  { "sum": { "field": "exposure" } },
    "total_sum_cost_fee":  { "sum": { "field": "cost_fee" } },
    "total_inquiry_cost": {
      "bucket_script": {
        "buckets_path": {
          "total_cost": "total_sum_cost_fee",
          "total_inquiry": "total_sum_inquiry"
        },
        "script": "if (params.total_inquiry == 0) return 0; return params.total_cost / params.total_inquiry;"
      }
    }
  }

查询异常

java 复制代码
{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: bucket_script aggregation [total_inquiry_cost] must be declared inside of another aggregation;"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: bucket_script aggregation [total_inquiry_cost] must be declared inside of another aggregation;"
  },
  "status" : 400
}

改正

其实异常信息已经很明确的告诉了我们,我们这个script字段要在另一个agg中才能使用。实际就是我们要聚合的时候,一定是根据某个字段去分组以后才聚合,所以这里我们不要忘了。

XML 复制代码
  "aggs": {
    "by_account": {
      "terms": {
        "field": "third_account_id.keyword",
        "size": 1000
      },
      "aggs": {
        "total_sum_inquiry":   { "sum": { "field": "inquiry" } },
        "total_sum_clicks":    { "sum": { "field": "clicks" } },
        "total_sum_exposure":  { "sum": { "field": "exposure" } },
        "total_sum_cost_fee":  { "sum": { "field": "cost_fee" } },
        "total_inquiry_cost": {
        "bucket_script": {
            "buckets_path": {
            "total_cost": "total_sum_cost_fee",
            "total_inquiry": "total_sum_inquiry"
          },
           "script": "if (params.total_inquiry == 0) return 0; return params.total_cost / params.total_inquiry;"
          }
        }
      }
    }
  }

结果展示

XML 复制代码
{
  "took" : 34,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 72,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "by_account" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1111111",
          "doc_count" : 72,
          "total_sum_clicks" : {
            "value" : 26140.0
          },
          "total_sum_cost_fee" : {
            "value" : 6514.8421
          },
          "total_sum_exposure" : {
            "value" : 910966.0
          },
          "total_sum_inquiry" : {
            "value" : 195911.0
          },
          "total_inquiry_cost" : {
            "value" : 0.03325409037777358
          }
        }
      ]
    }
  }
}
相关推荐
橙子圆12322 分钟前
JUC之集合类不安全
java·开发语言
utmhikari34 分钟前
【AI原生】借助AI之力优化个人博客前端样式
前端·css·hexo·next·个人博客·ai-native·vibe coding
小凯在掘金1 小时前
为什么箭头函数不能作为构造函数?
前端·javascript
谢亮_vipxieliang1 小时前
Stream API 不是银弹:常见坑与性能建议
java·开发语言
..Dauntless..1 小时前
【Linux】进程地址空间初步理解
linux·运维·服务器
源码宝2 小时前
Web‑PACS 云影像源码|Java+Vue3 医院影像管理系统完整方案
java·源码·影像pacs·成品源码·医院影像系统
Julien20042 小时前
Docker 网络(一)
linux·运维·服务器·ssh·学习方法
ShineWinsu2 小时前
对于Redis:string类型的解析
java·c++·redis·分布式·缓存·面试·string
杨云龙UP2 小时前
一次数据库查询缓慢故障复盘:大表数据增长、SQL全表扫描导致系统响应异常
linux·运维·服务器·数据库·sql·mysql
JustHappy2 小时前
「vConsole MCP🛠️」我让 AI 直接看见任何 H5 的日志和请求帮你 debug
前端·javascript·程序员