import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.Aggregation; //process ES Response, it will iterate only once though // sr = searchResponse for (Aggregation maxAggs : sr.getAggregations()) { Max max = (Max) maxAggs; double maxValue = max.getValue(); System.out.println("maxValue => " + maxValue); }
private void parseStatsAgg(Aggregation aggregation, Map<String, Object> resultObject, List<String> statsKeys) { if (aggregation instanceof Min) { Min stats = (Min) aggregation; resultObject.put(stats.getName(), getDoubleVal(stats.getValue())); statsKeys.add(stats.getName()); } if (aggregation instanceof Max) { Max stats = (Max) aggregation; resultObject.put(stats.getName(), getDoubleVal(stats.getValue())); statsKeys.add(stats.getName()); } if (aggregation instanceof Sum) { Sum stats = (Sum) aggregation; resultObject.put(stats.getName(), getDoubleVal(stats.getValue())); statsKeys.add(stats.getName()); } if (aggregation instanceof Avg) { Avg stats = (Avg) aggregation; resultObject.put(stats.getName(), getDoubleVal(stats.getValue())); statsKeys.add(stats.getName()); } }
private long retrieveMaxTimestamp(Terms.Bucket bucket, String targetAggregationName) { Aggregation maxAggregation = bucket.getAggregations().get(targetAggregationName); if (!(maxAggregation instanceof Max)) { LOGGER.warning("Could not retrieve maximum timestamp when calculating statistics for Objects."); return -1; } // Retrieve maximum timestamp from the max aggregation. return Math.round(Max.class.cast(maxAggregation).getValue()); }
double maxValue = avg.getValue();
private Map<String, Double> getFieldBoundaries(){ SearchRequestBuilder boundsRequest = this.client.prepareSearch(this.index) .setTypes("datum") .setSearchType(SearchType.COUNT) .addAggregation( AggregationBuilders.min("minX").field(this.xField) ).addAggregation( AggregationBuilders.min("minY").field(this.yField) ).addAggregation( AggregationBuilders.max("maxX").field(this.xField) ).addAggregation( AggregationBuilders.max("maxY").field(this.yField) ); SearchResponse searchResponse = boundsRequest.execute().actionGet(); Aggregations aggregations = searchResponse.getAggregations(); Min minX = aggregations.get("minX"); Min minY = aggregations.get("minY"); Max maxX = aggregations.get("maxX"); Max maxY = aggregations.get("maxY"); Map<String, Double> boundsMap = new HashMap<>(); boundsMap.put("minX", minX.getValue()); boundsMap.put("minY", minY.getValue()); boundsMap.put("maxX", maxX.getValue()); boundsMap.put("maxY", maxY.getValue()); return boundsMap; }
/** * Ensures the audit sequence returns an UID greater or equal than the maximum log entry id. */ protected void ensureUIDSequencer(ESClient esClient) { boolean auditIndexExists = esClient.indexExists(getESIndexName()); if (!auditIndexExists) { return; } // Get max log entry id SearchRequest request = createSearchRequest(); request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()) .aggregation(AggregationBuilders.max("maxAgg").field("id"))); SearchResponse searchResponse = esClient.search(request); Max agg = searchResponse.getAggregations().get("maxAgg"); long maxLogEntryId = (long) agg.getValue(); // Get next sequence id UIDGeneratorService uidGeneratorService = Framework.getService(UIDGeneratorService.class); UIDSequencer seq = uidGeneratorService.getSequencer(); seq.init(); long nextSequenceId = seq.getNextLong(SEQ_NAME); // Increment sequence to max log entry id if needed if (nextSequenceId < maxLogEntryId) { log.info(String.format("Next UID returned by %s sequence is %d, initializing sequence to %d", SEQ_NAME, nextSequenceId, maxLogEntryId)); seq.initSequence(SEQ_NAME, maxLogEntryId); } }