查询需求
我需要根据我传入的账号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
}
}
]
}
}
}