@Override public void runAggregations() { System.out.println("get aggregations:"); System.out.println("connecting cluster..."); Node node = nodeBuilder().clusterName("ron-es").client(true).node(); Client client = node.client(); AbstractAggregationBuilder maxAgg = AggregationBuilders.max("max-km").field("km"); AbstractAggregationBuilder minAgg = AggregationBuilders.min("min-km").field("km"); AbstractAggregationBuilder avgAgg = AggregationBuilders.avg("avg-km").field("km"); AbstractAggregationBuilder sumAgg = AggregationBuilders.sum("sum-km").field("km"); String startTime = Util.getDateTimeNow(); SearchResponse response = client .prepareSearch("vehicles") .setTypes("cars") .addAggregation(maxAgg) .addAggregation(minAgg) .addAggregation(avgAgg) .addAggregation(sumAgg) .execute() .actionGet(); String stopTime = Util.getDateTimeNow(); _timeElapssed = Util.getDateDiff(startTime, stopTime); // SearchResponse response = // client.prepareSearch("vehicles").setTypes("cars").addAggregation(AggregationBuilders.max("max-km").field("km")).execute().actionGet(); client.close(); System.out.println("node closed"); // SearchResponse response = client.prepareSearch().execute().actionGet(); // Stats agg = response.getAggregations().get("km"); // double max = agg.getMax(); System.out.println(response); }
/** * Create aggregation according to the SQL function. * * @param field SQL function * @param parent parentAggregation * @return AggregationBuilder represents the SQL function * @throws SqlParseException in case of unrecognized function */ public AbstractAggregationBuilder makeFieldAgg( MethodField field, AbstractAggregationBuilder parent) throws SqlParseException { groupMap.put(field.getAlias(), new KVValue("FIELD", parent)); ValuesSourceMetricsAggregationBuilder builder; field.setAlias(fixAlias(field.getAlias())); switch (field.getName().toUpperCase()) { case "SUM": builder = AggregationBuilders.sum(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "MAX": builder = AggregationBuilders.max(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "MIN": builder = AggregationBuilders.min(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "AVG": builder = AggregationBuilders.avg(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "STATS": builder = AggregationBuilders.stats(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "EXTENDED_STATS": builder = AggregationBuilders.extendedStats(field.getAlias()); return addFieldOrScriptToAggregation(field, builder); case "PERCENTILES": builder = AggregationBuilders.percentiles(field.getAlias()); addSpecificPercentiles((PercentilesBuilder) builder, field.getParams()); return addFieldOrScriptToAggregation(field, builder); case "TOPHITS": return makeTopHitsAgg(field); case "SCRIPTED_METRIC": return scriptedMetric(field); case "COUNT": groupMap.put(field.getAlias(), new KVValue("COUNT", parent)); return makeCountAgg(field); default: throw new SqlParseException("the agg function not to define !"); } }