# Elasticsearch: find documents with distinct values and then aggregate over them

**URL:** <https://discuss.elastic.co/t/elasticsearch-find-documents-with-distinct-values-and-then-aggregate-over-them/1953>\
**Category:** Elasticsearch\
**Created:** [June 4, 2015, 5:46pm UTC](https://discuss.elastic.co/t/elasticsearch-find-documents-with-distinct-values-and-then-aggregate-over-them/1953 "2015-06-04T17:46:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iravid](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iravid/32/521_2.png) [@iravid](https://discuss.elastic.co/u/iravid)\
**Post date:** [June 4, 2015, 5:46pm UTC](https://discuss.elastic.co/t/elasticsearch-find-documents-with-distinct-values-and-then-aggregate-over-them/1953/1 "2015-06-04T17:46:48Z")

</div>

(cross-posted at [http://stackoverflow.com/questions/30649740/elasticsearch-find-documents-with-distinct-values-and-then-aggregate-over-them](http://stackoverflow.com/questions/30649740/elasticsearch-find-documents-with-distinct-values-and-then-aggregate-over-them))

Hey everyone, question about aggregations.

My index has a log-like structure: I insert a version of a document whenever an event occurs. For example, here are documents in the index:

```
{ "key": "a", subkey: 0 }
{ "key": "a", subkey: 0 }
{ "key": "a", subkey: 1 }
{ "key": "a", subkey: 1 }
{ "key": "b", subkey: 0 }
{ "key": "b", subkey: 0 }
{ "key": "b", subkey: 1 }
{ "key": "b", subkey: 1 }

```

There are more fields to each document that differentiate between the versions, but they don't matter for the scope of this query. I'm trying to construct a query which is basically equivalent to the following SQL query:

```
SELECT COUNT(*), key, subkey
FROM (SELECT DISTINCT key, subkey FROM t)

```

The answer to this query would obviously be

```
(1, a, 0)
(1, a, 1)
(1, b, 0)
(1, b, 1)

```

How would I replicate this in Elasticsearch? I came up with the following:

```
GET test_index/test_type/_search?search_type=count
{
  "aggregations": {
    "count_aggr": {
      "terms": {
        "field": "concatenated_key"
      },
      "aggs": {
        "sample_doc": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}

```

`concatenated_key` is a concatenation of `key` and `subkey`. This query would create a bucket for each (key, subkey) combination and return a sample document from each bucket. However, I don't know how can I aggregate over the fields of `_source`.

Would appreciate any ideas. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:09am UTC](https://discuss.elastic.co/t/elasticsearch-find-documents-with-distinct-values-and-then-aggregate-over-them/1953/2 "2017-07-06T00:09:34Z")

</div>


