how to implement the following sql query in es?

2019-09-15 10:00发布

I want to query similarly the following SQL query:

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

Who know how to implement in es ?

thanks for your answer !

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-15 10:43

You'd do this with a terms aggregation with min_doc_count: 2 like this

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

Note that the countryName field should be not_analyzed in order for this to work, or the countryName field is a multi-field with a not_analyzed part.

查看更多
登录 后发表回答