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
          }
        }
      ]
    }
  }
}
相关推荐
XWalnut10 小时前
LeetCode刷题 day20
java·算法·leetcode
cui_ruicheng10 小时前
Linux网络编程(八):基于TCP实现CommandServer
linux·服务器·网络·tcp/ip
不吃土豆的马铃薯10 小时前
网络 IO 核心(同步/异步)概念笔记
服务器·c语言·开发语言·网络·c++·笔记
张小凡vip10 小时前
python的__init__.py说明
开发语言·前端·python
GIS66880010 小时前
零基础webgis开发入门:HTML/CSS/JavaScript前端核心基础①
前端·css·html
zhaokuangkuang_10 小时前
Java学习
java·学习·算法
JiaWen技术圈11 小时前
React 19 Fiber 架构 深度解析
前端·react.js·架构
暗冰ཏོ11 小时前
《Vue + React + Java + PHP 项目部署到服务器完整指南》
java·服务器·vue.js·react.js·项目部署
大阳光男孩11 小时前
【UniApp小程序开发】解决无法使用Vue自定义指令的完美替代方案:权限组件封装
前端·vue.js·uni-app