0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

i have a count( distinct(countryId) ) function ,it seems need pipeline agge to having filter count
There's a 1:1 relationship between countryId and countryName right? so count(distinct(countryId)) == count(distinct(countryName)) right?
my data count(distinct(countryId)) != count(distinct(countryName)) , so maybe use min_doc_count is incorrect
So as I understand you have the same country name which can have different country ids? That's funny, but ok, I'll update my answer
min_doc_count instead min_doc_size can execute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.