Unique count of terms aggregations

elasticsearch

Solution

You're looking for the cardinality aggregation which was added in Elasticsearch 1.1. It allows you to request something like this:

{
  "aggs" : {
      "unique_users" : {
          "cardinality" : {
              "field" : "username"
          }
      }
  }
}

Problem

I want to count distinct values of a field from my dataset. For example: The `terms` aggregation gives me the number of occurences by `username`. I want to only count unique usernames, not all. Here's my request: ``` POST appzz/messages/_search { "aggs": { "words": { "terms": { "field": "username" } } }, "size": 0, "from": 0 } ``` Is there a `unique` option or something like that?

Original source