# How to get a single count only per document when using nested term aggregation?

**URL:** https://discuss.elastic.co/t/how-to-get-a-single-count-only-per-document-when-using-nested-term-aggregation/315774
**Category:** Elasticsearch
**Tags:** eql-elastic-query-language
**Created:** [October 4, 2022, 12:59pm UTC](https://discuss.elastic.co/t/how-to-get-a-single-count-only-per-document-when-using-nested-term-aggregation/315774 "2022-10-04T12:59:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![binoiii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/binoiii/32/97753_2.png) [@binoiii](https://discuss.elastic.co/u/binoiii)
#### Post date: [October 4, 2022, 12:59pm UTC](https://discuss.elastic.co/t/how-to-get-a-single-count-only-per-document-when-using-nested-term-aggregation/315774/1 "2022-10-04T12:59:17Z")

</div>

**Mapping**

```auto
"work_rights": {
  "type": "nested",
  "properties": {
    "country_tid": {
      "type": "keyword"
    },
    "work_right_type": {
      "properties": {
        "tid": {
          "type": "keyword"
        }
      }
    }
  }
}

```

**Data**

```auto
Document1
{
  "doc": {
    "work_rights": [
      {
        "country_tid": "11",
        "work_right_type": [
          {
            "tid": "222"
          }
        ]
      }
    ]
  }
}

Document2
{
  "doc": {
    "work_rights": [
      {
        "country_tid": "12",
        "work_right_type": [
          {
            "tid": "222"
          }
        ]
      },
      {
        "country_tid": "13",
        "work_right_type": [
          {
            "tid": "222"
          },
          {
            "tid": "223"
          }
        ]
      }
    ]
  }
}

```

**Aggregation**

```auto
"aggs": {
  "work_right_tid": {
    "nested": {
      "path": "work_rights"
    },
    "aggs": {
      "work_right_type_tid": {
        "terms": {
          "field": "work_right_tid.work_right_type.tid",
          "size": 10
        }
      }
    }
  }
}

```

**Result**

```auto
{
  "buckets": [
    {
      "key": "222",
      "doc_count": 3
    },
    {
      "key": "223",
      "doc_count": 1
    }
  ]
}

```

**Question**  
The result is correct since nested data type will maintain the relationship of _country\_tid_ and _work\_right\_type_. Thus, **key:222** resulted to **doc\_count:3**.

Is there a way to get only one correct result per document while maintaining the data type as **nested**? (Since it is doable when the data type is set to **object** )  
Example: **key:222** resulted to **doc\_count:2**.

---

<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: [November 1, 2022, 12:59pm UTC](https://discuss.elastic.co/t/how-to-get-a-single-count-only-per-document-when-using-nested-term-aggregation/315774/2 "2022-11-01T12:59:53Z")

</div>

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