【问题标题】:SQL having equivalent keyword in ES query DSLSQL 在 ES 查询 DSL 中具有等效关键字
【发布时间】:2021-12-23 16:46:21
【问题描述】:
我将以下查询转换为 DSL 并在 ES 上执行。我找不到合适的聚合并过滤 ES 中开箱即用的聚合结果。作为替代方案,我从 ES 获取每个 id 的“按计数分组”,并将结果过滤为我的应用程序逻辑的一部分,但效率不高。您能提出更合适的解决方案吗?
select distinct id from index where colA = "something" group by id having count(*) > 10;
索引映射
id:(字符串)
colA:(字符串)
【问题讨论】:
标签:
sql
elasticsearch
elasticsearch-aggregation
elasticsearch-dsl
【解决方案1】:
-
Terms aggregation:获取不同的 ID。
-
Bucket selector: 返回文档数超过 10 个的 id
{
"query": {
"bool": {
"filter": [
{
"term": {
"colA.keyword": "something" --> where clause
}
}
]
}
},
"aggs": {
"distinct_id": {
"terms": { --> group by
"field": "id.keyword",
"size": 10
},
"aggs": {
"ids_having_count_morethan_10": {
"bucket_selector": { --> having
"buckets_path": {
"count": "_count"
},
"script": "params.count>10"
}
}
}
}
}
}