# Elasticsearch nested array Aggregation

**URL:** https://discuss.elastic.co/t/elasticsearch-nested-array-aggregation/120294
**Category:** Elasticsearch
**Created:** [February 17, 2018, 7:17am UTC](https://discuss.elastic.co/t/elasticsearch-nested-array-aggregation/120294 "2018-02-17T07:17:43Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lavangiriece](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lavangiriece/32/27860_2.png) [@lavangiriece](https://discuss.elastic.co/u/lavangiriece)
#### Post date: [February 17, 2018, 7:17am UTC](https://discuss.elastic.co/t/elasticsearch-nested-array-aggregation/120294/1 "2018-02-17T07:17:44Z")

</div>

Hello, I'm trying to aggregate a nested array, the count is including the duplicate value also..

Here is my Mapping

```
    {"mappings" : {
      "mention" : {
        "properties" : {
          "Topics" : {
            "type" : "nested",
            "properties" : {
              "CategoryLev1" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          }
        }
      }
    }
}

```

Sample Data

```
{
	"Topics" : [ 
        {
            "CategoryLev1" : "Price"          
        }, 
        {
            "CategoryLev1" : "Price"
        }
        
    ]
}

```

my Search Query

```
{  
   "size":0,
   "from":0,
   
    "aggs":{  
      "Topics":{  
         "nested":{  
            "path":"Topics"
         },
         "aggregations":{  
            "group_by_topic":{  
               "terms":{  
                  "field":"Topics.CategoryLev1.keyword",
                  "size":50,
                  "min_doc_count":1,
                  "shard_min_doc_count":0,
                  "show_term_doc_count_error":false,
                  "order":[  
                     {  
                        "_count":"desc"
                     },
                     {  
                        "_term":"asc"
                     }
                  ]
               }
            }
         }
      }
   }
}

```

Actual Output

```
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "Topics": {
            "doc_count": 2,
            "group_by_topic": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "Price",
                        "doc_count": 2
                    }
                ]
            }
        }
    }
}

```

Expected Output

```
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "Topics": {
            "doc_count": 2,
            "group_by_topic": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "Price",
                        "doc_count": **1**
                    }
                ]
            }
        }
    }
}

```

There is only 1 document, but duplicate inside the nested array.. So the count should be 1

---

<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: [March 17, 2018, 7:17am UTC](https://discuss.elastic.co/t/elasticsearch-nested-array-aggregation/120294/2 "2018-03-17T07:17:53Z")

</div>

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