Help Needed: Nested Aggregations with Java API Client v8
I'm working on a project using Elasticsearch with the Java API Client v8, and I'm struggling to correctly implement nested aggregations. I'd greatly appreciate any help or guidance.
Index Mapping
Here's the relevant part of my index mapping:
{
"actions" : {
"mappings" : {
"properties" : {
"actionRoles" : {
"type" : "nested",
"properties" : {
"roleName" : {
"type" : "keyword"
}
}
},
// ... other fields ...
}
}
}
}
I need to find out how to make this TODO
fun toNativeQuery(): NativeQuery {
val criteriaQuery = CriteriaQuery(criteria)
val queryBuilder = NativeQuery.builder().withQuery(criteriaQuery)
//...
queryBuilder.withAggregation(
AGG_ACTION_ROLE,
//TODO: Add the nested aggregation for 'actionRoles.roleName'
)
//...
return queryBuilder.build()
}
I should be able to get this aggregation generated, which is working in Kibana
{
"aggregations": {
"actionRoles": {
"nested": {
"path": "actionRoles"
},
"aggregations": {
"roleNames": {
"terms": {
"field": "actionRoles.roleName",
"size": 1000
}
}
}
}
}
Since subAggregation has been removed in elasticsearch-rest-client 8, I couldn't make it work for now.
I guess I have to do something like
queryBuilder.withAggregation(
AGG_ACTION_ROLE,
//TODO: Add the nested aggregation for 'actionRoles'
AggregationBuilders.nested {
//how to do the nested aggregation here?
}
)
Any ideas guys? Thanks a lot!