Aggregate doc_count per document and not per nested item?

Hi, my documents have the following document structure (I changed the document type to frog and colors per year here to make it more clear):

{
    "id": "489191",
    "type": "frog",
    "color_types": [
        {
            "year": "1990",
            "colors": [
                "red",
                "green"
            ]
        },
        {
            "year": "1991",
            "colors": [
                "red",
                "blue"
            ]
        }
    ]
}

Now if I do an aggregation on this as folows, I get 2 for doc_count on red.

{
    "aggs": {
        "color_aggregation": {
            "nested": {
                "path": "color_types"
            },
            "aggs": {
                "nested": {
                    "terms": {
                        "field": "color_types.colors",
                            "order": {
                            "_term": "desc"
                        },
                        "size": 10
                    }
                }
            }
        }
    }
}

These are the results:

{
    "key": "red",
    "doc_count": 2
},
{
    "key": "green",
    "doc_count": 1
},
{
    "key": "blue",
    "doc_count": 1
},

As you can see I get 2 for red, whereas there is only one frog. I know this is normal behaviour, but how can I change the doc_count to count the number of documents (frogs) and not the number of nested items?

Thanks in advance for your help.

OK it seems like this works

{
    "aggs": {
        "color_aggregation": {
            "nested": {
                "path": "color_types"
            },
            "aggs": {
                "nested": {
                    "terms": {
                        "field": "color_types.colors",
                            "order": {
                            "_term": "desc"
                        },
                        "size": 10
                    },
                     "aggs": {
                         "reversed_nested": {
                             "reverse_nested": {
                         }
                      }
                    }
                }
            }
        }
    }
}

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