In case it might help someone, my tech lead could help and provided this solution that is indeed working
fun toNativeQuery(): NativeQuery {
val criteriaQuery = CriteriaQuery(criteria)
val queryBuilder = NativeQuery.builder().withQuery(criteriaQuery)
//...
val subAggRoleName =
AggregationBuilders.terms { it.field("${ActionEntity::actionRoles.name}.${ActionRoleEntity::roleName.name}") }
// AggregationBuilder.nested does support sub-aggregations,
// so we need to build it with the Aggregation.Builder()
val aggNestedActionRolesName =
Aggregation
.Builder()
.nested { it.path(ActionEntity::actionRoles.name) }
.aggregations(
mapOf("roleNames" to subAggRoleName),
).build()
queryBuilder.withAggregation(AGG_ACTION_ROLE, aggNestedActionRolesName)
//...
return queryBuilder.build()
}