/**
   * 分组查的聚合函数
   *
   * @param field
   * @return
   * @throws SqlParseException
   */
  public AggregationBuilder<?> makeGroupAgg(Field field) throws SqlParseException {
    if (field instanceof MethodField) {

      MethodField methodField = (MethodField) field;
      if (methodField.getName().equals("filter")) {
        Map<String, Object> paramsAsMap = methodField.getParamsAsMap();
        Where where = (Where) paramsAsMap.get("where");
        return AggregationBuilders.filter(paramsAsMap.get("alias").toString())
            .filter(FilterMaker.explan(where));
      }
      return makeRangeGroup(methodField);
    } else {
      TermsBuilder termsBuilder = AggregationBuilders.terms(field.getName()).field(field.getName());
      groupMap.put(field.getName(), new KVValue("KEY", termsBuilder));
      return termsBuilder;
    }
  }
 private AggregationBuilder<?> updateAggIfNested(AggregationBuilder<?> lastAgg, Field field) {
   if (field.isNested()) {
     lastAgg =
         AggregationBuilders.nested(field.getName() + "Nested")
             .path(field.getNestedPath())
             .subAggregation(lastAgg);
   }
   return lastAgg;
 }
  private void setFields(List<Field> fields) {
    if (select.getFields().size() > 0) {
      ArrayList<String> includeFields = new ArrayList<>();

      for (Field field : fields) {
        if (field != null) {
          includeFields.add(field.getName());
        }
      }

      request.setFetchSource(includeFields.toArray(new String[includeFields.size()]), null);
    }
  }
 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";
 }
 private void explanFields(
     SearchRequestBuilder request, List<Field> fields, AggregationBuilder<?> groupByAgg)
     throws SqlParseException {
   for (Field field : fields) {
     if (field instanceof MethodField) {
       AbstractAggregationBuilder makeAgg = aggMaker.makeFieldAgg((MethodField) field, groupByAgg);
       if (groupByAgg != null) {
         groupByAgg.subAggregation(makeAgg);
       } else {
         request.addAggregation(makeAgg);
       }
     } else if (field instanceof Field) {
       request.addField(field.getName());
     } else {
       throw new SqlParseException("it did not support this field method " + field);
     }
   }
 }