【问题标题】:how to implement the following sql query in es?如何在es中实现下面的sql查询?
【发布时间】:2016-08-20 02:57:45
【问题描述】:

我想类似地查询以下 SQL 查询:

select countryName, count( distinct(countryId) ) as findCount   from city group by countryName having findCount > 1

谁知道如何在es中实现?

感谢您的回答!

【问题讨论】:

    标签: elasticsearch lucene


    【解决方案1】:

    您可以使用 terms 聚合和 min_doc_count: 2 像这样

    {
      "size": 0,
      "aggs": {
        "countries": {
          "terms": {
            "field": "countryName"
          },
          "aggs": {
            "ids": {
              "terms": {
                "field": "countryId",
                "min_doc_count": 2
              }
            }
          }
        }
      }
    }
    

    请注意,countryName 字段应为 not_analyzed 以使其正常工作,或者 countryName 字段是带有 multi-field 部分的 multi-field

    【讨论】:

    • 我有一个 count( distinct(countryId) ) 函数,似乎需要管道 agge 才能拥有过滤器计数
    • countryId 和 countryName 之间存在 1:1 的关系,对吗?所以count(distinct(countryId)) == count(distinct(countryName)) 对吗?
    • 我的数据计数(distinct(countryId)) != count(distinct(countryName)) ,所以可能使用 min_doc_count 不正确
    • 所以据我了解,你有相同的国家名称,可以有不同的国家 ID?这很有趣,但是好的,我会更新我的答案
    • min_doc_count 而不是 min_doc_size 可以执行
    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2011-06-07
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多