Versions (relevant - OpenSearch/Dashboard/Server OS/Browser): 2.19.4
Describe the issue:
Hi,
I’m attempting to perform a terms aggregation on a nested field. I was able to get a result using the following query, however the result produced is a nested structure with buckets inside buckets.
GET /test_nested_field/_search?size=0
{
"aggs": {
"book": {
"nested": {
"path": "book"
},
"aggs": {
"title": {
"terms": {
"field": "book.title.keyword"
},
"aggs": {
"version": {
"terms": {
"field": "book.version.keyword"
}
}
}
}
}
}
}
}
Is there a way to write the aggregation query so that the results keep the nested field contents grouped together? Ideally, the results would look something like:
{
"book": {
"buckets": [
{
"key": {"title": "a", "version": "v1.0"}
"doc_count": 1
},
{
"key": {"title": "b", "version": "v1.0"}
"doc_count": 1
},
{
"key": {"title": "b", "version": "v2.0"}
"doc_count": 2
},
]
}
}
instead of:
{
"book": {
"title": {
"buckets": [
{
"key": "a",
"version": {
"buckets": [
{
"key": "v1.0",
"doc_count": 1
}
]
}
},
{
"key": "b",
"version": {
"buckets": [
{
"key": "v1.0",
"doc_count": 1
},
{
"key": "v2.0",
"doc_count": 2
}
]
}
}
]
}
}
}
This is the mapping and some sample documents:
PUT /test_nested_field
{
"mappings": {
"properties": {
"book": {
"type": "nested"
}
}
}
}
POST /test_nested_field/_doc
{
"project": "One",
"book": {
"title": "a",
"version": "v1.0"
}
}
POST /test_nested_field/_doc
{
"project": "Two",
"book": {
"title": "b",
"version": "v1.0"
}
}
POST /test_nested_field/_doc
{
"project": "Three",
"book": {
"title": "b",
"version": "v2.0"
}
}
POST /test_nested_field/_doc
{
"project": "Four",
"book": {
"title": "b",
"version": "v2.0"
}
}
Thank you.