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

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

谁知道如何在es中实现?

感谢您的回答 !

最佳答案

您可以使用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字段是带有not_analyzed部分的multi-field

10-04 22:49