Aggregation provides multiplied results

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

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 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.

Mark,

Thanks for quick reply!
Will try it!

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