如何在es中实现以下sql查询?

时间:2016-08-20 02:57:46

标签: elasticsearch lucene

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

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

谁知道如何在es?

中实施

感谢您的回答!

1 个答案:

答案 0 :(得分: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-fieldnot_analyzed部分。