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
          }
        }
      ]
    }
  }
}
相关推荐
吴声子夜歌9 分钟前
ApacheCommons——commons-text(模板替换与文本算法)
java·开发语言·算法·apache
Haooog22 分钟前
Agent 开发中的 Memory:State、短期记忆、长期记忆与 Memory Retrieval
java·agent·memory
砚底藏山河29 分钟前
【量化纯GET实战 #23】多股票相关性:用收益率看板块联动
java·数据库·python·金融·数据分析
非凡ghost39 分钟前
AI修图不“造假“,摄影师的出片效率加速器
服务器·前端·人工智能·电脑
2601_9516152040 分钟前
网页设计模板源码 web前端模板网页源码下载
前端
mmsx40 分钟前
osmdroid 地图实战 03|地图的"分层世界":离线方案、图层模型与 Overlay 体系
android·前端
抠脚小弟44 分钟前
Spring Cache 使用详解:从入门到实战
java·spring
ZGG0031 小时前
MySQL 索引详解:B+ 树、聚簇索引与最左前缀
java·数据库·mysql
Ruiery1 小时前
Linux 6.6内核 PCIe 深度解析(十):PCIe Port Driver(portdrv)— 一个物理 Port,拆出五个逻辑服务
linux·运维·服务器
郭邯1 小时前
从零到一:我用 AI 写了个复利计算器,顺便治好了我的"公式恐惧症"
前端