ElasticSearch Missing definition for aggregation [unique_users] Error

5.8k Views Asked by At

I am using ES with Ruby on Rails, I am in a situation where I have to list all unique users so below is the code that I am using to get it.

    buckets['unique_users'] = {
      filter: { match_all: {} },
      aggregations: {
        users_count: {
          cardinality: {
            field: :customer_email_key
          }
        }
      }
    }

and below is the response:

unique_users: {
  doc_count: 134,
    users_count: {
      value: 125
    }
  }
}

This is only showing the number of unique users but this query does not show the email listing so where is the issue in this query. Could anyone guide me?

             ==========  New Edits  ==========

Now I am using the code and its working fine with the users listing and it counts.

  def buckets_for_unique_users(buckets, aggregations)
    buckets['unique_users'] = {
      filter: { match_all: {} },
      aggregations: {
        users_count: {
          cardinality: {
            field: :customer_email_key
          }
        },
        details: {
          terms: {
            field: :customer_email_key,
            size: 200
          }
        }
      }
    }
  end

now getting above code response is:

unique_users: {
  doc_count: 134,
  users_count: {
   value: 125
  },
  details: {
    doc_count_error_upper_bound: 0,
    sum_other_doc_count: 0,
    buckets: [
      {
        key: "[email protected]",
        doc_count: 2
      }
      {....}
    ]
}

Now everything is going perfect but there is something in the response that is the little bit confusing so I have to remove that i.e the doc_count: 134 because I want to keep users_count and remove doc_count. So for this what I did I removed filter: { match_all: {} }, from the method and then I got error Missing definition for aggregation [unique_users]

1

There are 1 best solutions below

2
On

You need to use terms aggregation:

GET /_search
{
    "aggs" : {
        "users" : {
            "terms" : { "field" : "customer_email_key" }
        }
    }
}