private AggregationBuilder<?> geohashGrid(MethodField field) throws SqlParseException { String aggName = gettAggNameFromParamsOrAlias(field); GeoHashGridBuilder geoHashGrid = AggregationBuilders.geohashGrid(aggName); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); switch (kv.key.toLowerCase()) { case "precision": geoHashGrid.precision(Integer.parseInt(value)); break; case "field": geoHashGrid.field(value); break; case "size": geoHashGrid.size(Integer.parseInt(value)); break; case "shard_size": geoHashGrid.shardSize(Integer.parseInt(value)); break; case "alias": case "nested": case "reverse_nested": break; default: throw new SqlParseException("geohash grid err or not define field " + kv.toString()); } } return geoHashGrid; }
private AggregationBuilder<?> termsAgg(MethodField field) throws SqlParseException { String aggName = gettAggNameFromParamsOrAlias(field); TermsBuilder terms = AggregationBuilders.terms(aggName); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); switch (kv.key.toLowerCase()) { case "field": terms.field(value); break; case "size": terms.size(Integer.parseInt(value)); break; case "shard_size": terms.shardSize(Integer.parseInt(value)); break; case "min_doc_count": terms.minDocCount(Integer.parseInt(value)); break; case "alias": case "nested": case "reverse_nested": break; default: throw new SqlParseException("terms aggregation err or not define field " + kv.toString()); } } return terms; }
private AbstractAggregationBuilder addFieldOrScriptToAggregation( MethodField field, ValuesSourceMetricsAggregationBuilder builder) { KVValue kvValue = field.getParams().get(0); if (kvValue.key != null && kvValue.key.equals("script")) { // todo: support different lang script return builder.script(((MethodField) kvValue.value).getParams().get(1).toString()); } else if (kvValue.key != null && (kvValue.key.equals("nested") || kvValue.key.equals("reverse_nested"))) { NestedType nestedType = (NestedType) kvValue.value; builder.field(nestedType.field); AggregationBuilder nestedBuilder; String nestedAggName = nestedType.field + "@NESTED"; if (nestedType.isReverse()) { if (nestedType.path != null && nestedType.path.startsWith("~")) { String realPath = nestedType.path.substring(1); nestedBuilder = AggregationBuilders.nested(nestedAggName).path(realPath); nestedBuilder = nestedBuilder.subAggregation(builder); return AggregationBuilders.reverseNested(nestedAggName + "_REVERSED") .subAggregation(nestedBuilder); } else { nestedBuilder = AggregationBuilders.reverseNested(nestedAggName).path(nestedType.path); } } else { nestedBuilder = AggregationBuilders.nested(nestedAggName).path(nestedType.path); } return nestedBuilder.subAggregation(builder); } return builder.field(kvValue.toString()); }
private HistogramBuilder histogram(MethodField field) throws SqlParseException { String aggName = gettAggNameFromParamsOrAlias(field); HistogramBuilder histogram = AggregationBuilders.histogram(aggName); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); switch (kv.key.toLowerCase()) { case "interval": histogram.interval(Long.parseLong(value)); break; case "field": histogram.field(value); break; case "min_doc_count": histogram.minDocCount(Long.parseLong(value)); break; case "extended_bounds": String[] bounds = value.split(":"); if (bounds.length == 2) histogram.extendedBounds(Long.valueOf(bounds[0]), Long.valueOf(bounds[1])); break; case "alias": case "nested": case "reverse_nested": break; case "order": Histogram.Order order = null; switch (value) { case "key_desc": order = Histogram.Order.KEY_DESC; break; case "count_asc": order = Histogram.Order.COUNT_ASC; break; case "count_desc": order = Histogram.Order.COUNT_DESC; break; case "key_asc": default: order = Histogram.Order.KEY_ASC; break; } histogram.order(order); break; default: throw new SqlParseException("histogram err or not define field " + kv.toString()); } } return histogram; }
/** * 按照时间范围分组 * * @param field * @return * @throws SqlParseException */ private DateHistogramBuilder dateHistogram(MethodField field) throws SqlParseException { String alias = gettAggNameFromParamsOrAlias(field); DateHistogramBuilder dateHistogram = AggregationBuilders.dateHistogram(alias).format(TIME_FARMAT); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); switch (kv.key.toLowerCase()) { case "interval": dateHistogram.interval(new DateHistogram.Interval(kv.value.toString())); break; case "field": dateHistogram.field(value); break; case "format": dateHistogram.format(value); break; case "time_zone": case "pre_zone": dateHistogram.preZone(value); break; case "post_zone": dateHistogram.postZone(value); break; case "post_offset": dateHistogram.postOffset(value); break; case "pre_offset": dateHistogram.preOffset(value); break; case "alias": case "nested": case "reverse_nested": break; default: throw new SqlParseException("date range err or not define field " + kv.toString()); } } return dateHistogram; }
private AggregationBuilder<?> geoBounds(MethodField field) throws SqlParseException { String aggName = gettAggNameFromParamsOrAlias(field); GeoBoundsBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName); String value = null; for (KVValue kv : field.getParams()) { value = kv.value.toString(); switch (kv.key.toLowerCase()) { case "field": boundsBuilder.field(value); break; case "wrap_longitude": boundsBuilder.wrapLongitude(Boolean.getBoolean(value)); break; case "alias": case "nested": case "reverse_nested": break; default: throw new SqlParseException("geo_bounds err or not define field " + kv.toString()); } } return boundsBuilder; }