使用 shuf 来打乱一个文件中的行或是选择文件中一个随机的行。

如何用ElasticSearch实现下面的SQL

Elasticsearch | 作者 spark | 发布于2016年08月20日 | 阅读数:8836

select countryName, count( distinct(countryId) ) as findCount   from city group by countryName having findCount > 1
请问如何用es的agg实现上面的SQL,如果能,请给个例子,谢谢
已邀请:

martindu - 搜披露创始人

赞同来自:

试一试terms aggregation内嵌一个cardinality aggregation,然后外层terms设置min_doc_count为2
 
参考https://www.elastic.co/guide/e ... .html

Todo - 程序猿

赞同来自:

楼主问题解决了吗?能否分享下,这个问题困扰了好久,毫无头绪!

szwx855 - Easy Simple

赞同来自:

{
  "size": 0,
  "aggs": {
    "all": {
      "terms": {
        "field": "cfg_id",
        "min_doc_count": 2
      },
      "aggs": {
        "count(distinct)": { 
          "cardinality": {
            "field": "name"
          }
        }
      }
    }
  }
}

enjoyhot

赞同来自:

不巧最近也在解决类似问题,在更新ES为5.2.2之后的尝试:
    "query" : {
        "constant_score" : {
            "filter" : {
                "range" : { "drt" : { "from" : "2015-06-01", "to" : "2015-06-01" }}
            }
        }
    },
    "size":0,
    "aggs": {
        "top_num": {
            "terms": {
                "field": "imei",
                "order": { "distinct_apps": "desc"}
            },
            "aggs": {
                "distinct_apps":{
                    "cardinality" : {
                        "field" : "app"
                    }
                }
            }
        }
    }
 
发现内存占用高(数据量近千万),不知道有没有经验介绍,谢谢。

要回复问题请先登录注册