# Terms aggregation in the same document

**URL:** https://discuss.elastic.co/t/terms-aggregation-in-the-same-document/107282
**Category:** Elasticsearch
**Created:** [November 11, 2017, 6:39pm UTC](https://discuss.elastic.co/t/terms-aggregation-in-the-same-document/107282 "2017-11-11T18:39:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![yeela](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yeela/32/24099_2.png) [@yeela](https://discuss.elastic.co/u/yeela)
#### Post date: [November 11, 2017, 6:39pm UTC](https://discuss.elastic.co/t/terms-aggregation-in-the-same-document/107282/1 "2017-11-11T18:39:53Z")

</div>

Hi,  
Is that possible to count the value of fields in the same document?  
for example, i have this data:

```
"hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "yeela_checks",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "group": "fans",
          "user": [
            {
              "first": "John",
              "last": "Smith"
            },
            {
              "first": "Maria",
              "last": "Williams"
            },
            {
              "first": "Zack",
              "last": "Farro"
            }
          ]
        }
      },
      {
        "_index": "yeela_checks",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "group": "fans",
          "user": [
            {
              "first": "John",
              "last": "Smith"
            },
            {
              "first": "Alice",
              "last": "White"
            }
          ]
        }
      },
      {
        "_index": "yeela_checks",
        "_type": "doc",
        "_id": "3",
        "_score": 1,
        "_source": {
          "group": "fans",
          "user": [
            {
              "first": "John",
              "last": "Smith"
            },
            {
              "first": "John",
              "last": "Smith"
            },
            {
              "first": "John",
              "last": "Smith"
            }
          ]
        }
      }
    ]
  }

```

and i want to count with terms aggregation the field "user.first.keyword", so I've made this query:

```
GET yeela_checks/_search
{
  "size": 0,
  "aggs": {
    "users_count": {
      "terms": {
        "field": "user.first.keyword",
        "size": 10,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}

```

and the result i get is this:

```
 "aggregations": {
    "users_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "John",
          "doc_count": 3
        },
        {
          "key": "Alice",
          "doc_count": 1
        },
        {
          "key": "Maria",
          "doc_count": 1
        },
        {
          "key": "Zack",
          "doc_count": 1
        }
      ]
    }
  }

```

I expected to receive for the "John" value the count of 5 and not 3.  
How could i do it?

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: [December 9, 2017, 6:39pm UTC](https://discuss.elastic.co/t/terms-aggregation-in-the-same-document/107282/2 "2017-12-09T18:39:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
