private AggregationBuilder<?> updateAggIfNested(AggregationBuilder<?> lastAgg, Field field) {
   if (field.isNested()) {
     lastAgg =
         AggregationBuilders.nested(field.getName() + "Nested")
             .path(field.getNestedPath())
             .subAggregation(lastAgg);
   }
   return lastAgg;
 }
 private AggregationBuilder createNestedAggregation(Field field) {
   AggregationBuilder nestedBuilder;
   String nestedPath = field.getNestedPath();
   if (field.isReverseNested()) {
     if (nestedPath == null || !nestedPath.startsWith("~"))
       return AggregationBuilders.reverseNested(getNestedAggName(field)).path(nestedPath);
     nestedPath = nestedPath.substring(1);
   }
   nestedBuilder = AggregationBuilders.nested(getNestedAggName(field)).path(nestedPath);
   return nestedBuilder;
 }
 private String getNestedAggName(Field field) {
   String prefix;
   if (field instanceof MethodField) {
     String nestedPath = field.getNestedPath();
     if (nestedPath != null) {
       prefix = nestedPath;
     } else {
       prefix = field.getAlias();
     }
   } else {
     prefix = field.getName();
   }
   return prefix + "@NESTED";
 }