# Aggregation provides multiplied results

**URL:** https://discuss.elastic.co/t/aggregation-provides-multiplied-results/191391
**Category:** Elasticsearch
**Created:** [July 19, 2019, 12:29pm UTC](https://discuss.elastic.co/t/aggregation-provides-multiplied-results/191391 "2019-07-19T12:29:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vova\_Marchev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vova_marchev/32/50532_2.png) [@Vova\_Marchev](https://discuss.elastic.co/u/Vova_Marchev)
#### Post date: [July 19, 2019, 12:29pm UTC](https://discuss.elastic.co/t/aggregation-provides-multiplied-results/191391/1 "2019-07-19T12:29:27Z")

</div>

Hi all!

I have found the next behavior during aggregation.

Let's create a simple document:

```
PUT example
{
    "mappings": {
        "_doc": {
            "properties": {
                "products": {
                    "type": "object",
                    "properties": {
                        "category": {
                            "type": "object",
                            "properties": {
                                "name": {
                                    "type": "keyword",
                                    "store": true
                                },
                                "rank": {
                                    "type": "long",
                                    "store": true
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

```

Than add one document:

```
PUT example/_doc/1
{
    "products": [
        {
            "category": {
                "name": "Category 1",
                "rank": 1
            }
        },
        {
            "category": {
                "name": "Category 2",
                "rank": 2
            }
        }
    ]
}

```

I want to aggregate by category name and category rank, so my aggregation query is:

```
POST example/_search
{
    "aggs": {
        "agg_docs_category_name": {
            "terms": {
                "field": "products.category.name"
            },
            "aggs": {
                "agg_docs_category_rank": {
                    "terms": {
                        "field": "products.category.rank"
                    }
                }
            }
        }
    }
}

```

I expected to receive that I have "Category 1" with rank "1" and "Category 2" with rank "2" for this document, but I received:

```
{
"aggregations": {
        "agg_docs_category_name": {
            "buckets": [
                {
                    "key": "Category 1",
                    "doc_count": 1,
                    "agg_docs_category_rank": {
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 1
                            },
                            {
                                "key": 2,
                                "doc_count": 1
                            }
                        ]
                    }
                },
                {
                    "key": "Category 2",
                    "doc_count": 1,
                    "agg_docs_category_rank": {
                        "buckets": [
                            {
                                "key": 1,
                                "doc_count": 1
                            },
                            {
                                "key": 2,
                                "doc_count": 1
                            }
                        ]
                    }
                }
            ]
        }
    }
}

```

So, looks like ranks and categories just multiplied. How to avoid this?

Thanks in advance,  
Vova

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [July 19, 2019, 12:41pm UTC](https://discuss.elastic.co/t/aggregation-provides-multiplied-results/191391/2 "2019-07-19T12:41:02Z")

</div>

The `object` type essentially flattens any hierarchy into a single Lucene document making a muddle of which field value is paired with which other one. (I have some [old slides](https://www.slideshare.net/MarkHarwood/proposal-for-nested-document-support-in-lucene) that illustrate this).

To avoid that confusion the `nested` type must be used in mappings, searches and aggs to be clear that a part of a structure is referring to an element where the fields are considered a separate logical unit.

---

<div class="post-metadata">

### Author: ![Vova\_Marchev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vova_marchev/32/50532_2.png) [@Vova\_Marchev](https://discuss.elastic.co/u/Vova_Marchev)
#### Post date: [July 19, 2019, 12:51pm UTC](https://discuss.elastic.co/t/aggregation-provides-multiplied-results/191391/3 "2019-07-19T12:51:59Z")

</div>

Mark,

Thanks for quick reply!  
Will try it!

---

<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: [August 16, 2019, 12:52pm UTC](https://discuss.elastic.co/t/aggregation-provides-multiplied-results/191391/4 "2019-08-16T12:52:04Z")

</div>

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