【问题标题】: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】:
    1. Terms aggregation:获取不同的 ID。
    2. 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"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 2015-10-09
      • 1970-01-01
      • 2018-09-04
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多