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
          }
        }
      ]
    }
  }
}
相关推荐
kyriewen22 分钟前
别再乱用useEffect了——你写的10个里有8个不该存在
前端·javascript·react.js
Ivanqhz33 分钟前
Rust &‘static str浅析
java·前端·javascript·rust
IT_陈寒1 小时前
SpringBoot这个分页坑,我踩了三天才爬出来
前端·人工智能·后端
颜酱1 小时前
05 | 召回前置准备:根据业务数据库生成各数据库(读取配置阶段)
前端·人工智能·后端
zandy10112 小时前
衡石 Agentic BI的ReAct 推理框架在 Agentic BI 中的工程化实践
前端·javascript·react.js
罗超驿2 小时前
JavaEE进阶之路:从Web架构原理到HTML标签全解析
前端·html·web·javaee
西安小哥2 小时前
从前端到AI工程师:一场跨越鸿沟的真实蜕变之旅
前端
Prince4182 小时前
侧边栏收起缩放适配方案
前端
用户7783366132112 小时前
serpbase + Cloudflare R2 边缘持久化实战
前端·人工智能
এ慕ོ冬℘゜2 小时前
jQuery attr() 方法超详细讲解:属性获取、赋值、实战踩坑全解
前端·javascript·jquery