elasticsearch – Count number of inner elements of array property (Including repeated values)

Given I have the following records.

[
    {
        "profile": "123",
        "inner": [
            {
                "name": "John"
            }
        ]
    },
    {
        "profile": "456",
        "inner": [
            {
                "name": "John"
            },
            {
                "name": "John"
            },
            {
                "name": "James"
            }
        ]
    }
]

I want to get something like:

"aggregations": {
    "name": {
        "buckets": [
            {
                "key": "John",
                "doc_count": 3
            },
            {
                "key": "James",
                "doc_count": 1
            }
        ]
    }
}

I’m a beginner using Elasticsearch, and this seems to be a pretty simple operation to do, but I can’t find how to achieve this.
If I try a simple aggs using term, it returns 2 for John, instead of 3.
Example request I’m trying:

{
  "size": 0,
  "aggs": {
    "name": {
      "terms": {
        "field": "inner.name"
      }
    }
  }
}

How can I possibly achieve this?
Additional Info: It will be used on Kibana later.
I can change mapping to whatever I want, but AFAIK Kibana doesn’t like the “Nested” type. 🙁

Read more here: Source link