I have an index that contains documents about movies and the movies are tagged with their genre and main actors. Like this:
{
"title": "The fast and the furious",
"tags": [
{
"groupName": "Genre",
"tags": [ "Action", "Racing"]
},
{
"groupName": "Actors",
"tags": ["Vin Diesel", "Paul Walker"]
}
]
},
{
"title": "2 fast 2 furious",
"tags": [
{
"groupName": "Genre",
"tags": [ "Action"]
},
{
"groupName": "Actors",
"tags": ["Paul Walker", "Ludacris"]
}
]
}
Now I want to use aggregation to aggregate first on groupName then on the tags-list, in order to create a list like this:
Genre
- Action (2)
- Racing (1)
Actors
- Paul Walker (2)
- Vin Diesel (1)
- Ludacris (1)
I have tried to do the following aggregation:
POST mymovies/_search
{
"query": {
"match_all": {}
},
"aggs": {
"movie_groupName": {
"terms": {
"field": "tags.groupName",
"size": 20
},
"aggs": {
"movie_tags": {
"terms": {
"field": "tags.tags",
"size": 20
}
}
}
}
}
}
This is the result I expected:
"aggregations": {
"movie_groupName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Genre",
"doc_count": 2,
"movice_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets":
[
{
"key": "Action",
"doc_count": 2
},
{
"key": "Racing",
"doc_count": 1
}
]
}
},
{
"key": "Actors",
"doc_count": 2,
"movice_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets":
[
{
"key": "Paul Walker",
"doc_count": 2
},
{
"key": "Vin Diesel",
"doc_count": 1
},
{
"key": "Ludacris",
"doc_count": 1
}
]
}
}
]
}
}
But this is the result I get back:
"aggregations": {
"movie_groupName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Genre",
"doc_count": 2,
"movice_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets":
[
{
"key": "Action",
"doc_count": 2
},
{
"key": "Paul Walker",
"doc_count": 2
},
{
"key": "Racing",
"doc_count": 1
},
{
"key": "Vin Diesel",
"doc_count": 1
},
{
"key": "Ludacris",
"doc_count": 1
}
]
}
},
{
"key": "Actors",
"doc_count": 2,
"movice_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets":
[
{
"key": "Action",
"doc_count": 2
},
{
"key": "Paul Walker",
"doc_count": 2
},
{
"key": "Racing",
"doc_count": 1
},
{
"key": "Vin Diesel",
"doc_count": 1
},
{
"key": "Ludacris",
"doc_count": 1
}
]
}
}
]
}
}
Giving me a list that will look like this (which clearly is not what I want):
Genre
- Paul Walker (2)
- Action (2)
- Racing (1)
- Vin Diesel (1)
- Ludacris (1)
Actors
- Paul Walker (2)
- Action (2)
- Racing (1)
- Vin Diesel (1)
- Ludacris (1)
What am I doing wrong?