org.elasticsearch.search.aggregations.metrics.min.Min Java Examples
The following examples show how to use
org.elasticsearch.search.aggregations.metrics.min.Min.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchDataQuery.java From frostmourne with MIT License | 6 votes |
private Double findAggregationValue(MetricContract metricContract, SearchResponse searchResponse) { String aggType = metricContract.getAggregation_type(); if (aggType.equalsIgnoreCase("max")) { Max max = searchResponse.getAggregations().get("maxNumber"); return max.getValue(); } if (aggType.equalsIgnoreCase("min")) { Min min = searchResponse.getAggregations().get("minNumber"); return min.getValue(); } if (aggType.equalsIgnoreCase("avg")) { Avg avg = searchResponse.getAggregations().get("avgNumber"); return avg.getValue(); } if (aggType.equalsIgnoreCase("sum")) { Sum sum = searchResponse.getAggregations().get("sumNumber"); return sum.getValue(); } throw new IllegalArgumentException("unsupported aggregation type: " + aggType); }
Example #2
Source File: AggregationResponseHandle.java From jetlinks-community with Apache License 2.0 | 6 votes |
private static <A extends Aggregation> void route(Bucket bucket, A a) { if (a instanceof Terms) { bucket.setBuckets(terms(a)); } else if (a instanceof Range) { bucket.setBuckets(range(a)); } else if (a instanceof Histogram) { bucket.setBuckets(range(a)); } else if (a instanceof Avg) { bucket.setAvg(avg(a)); } else if (a instanceof Min) { bucket.setMin(min(a)); } else if (a instanceof Max) { bucket.setMax(max(a)); } else if (a instanceof Sum) { bucket.setSum(sum(a)); } else if (a instanceof Stats) { stats(bucket, a); } else if (a instanceof ValueCount) { bucket.setValueCount(count(a)); } else { throw new UnsupportedOperationException("不支持的聚合类型"); } }
Example #3
Source File: Search.java From elasticsearch-rest-command with The Unlicense | 5 votes |
public static String getValueFromAggregation(Aggregation a, Function f){ String value = null; switch(f.type){ case Function.SUM : value = String.valueOf(((Sum) a).getValue()); break; case Function.COUNT : value = String.valueOf(((ValueCount) a).getValue()); break; case Function.DC : value = String.valueOf(((Cardinality) a).getValue()); break; case Function.AVG : value = String.valueOf(((Avg) a).getValue()); break; case Function.MAX : value = String.valueOf(((Max) a).getValue()); break; case Function.MIN : value = String.valueOf(((Min) a).getValue()); break; } return value; }
Example #4
Source File: ElasticStatsService.java From staccato with Apache License 2.0 | 4 votes |
/** * Populates an {@link Extent} object with the bounding box for the items matched in the provided query. * * @param index The Elasticsearch index to api * @param query The query that should be executed against the provided index * @return The extent of the items matched during the api */ @Override public Extent getExtent(String index, Map<String, String> query) { StringBuilder sb = new StringBuilder(); if (null != query) { Iterator<Map.Entry<String, String>> it = query.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); sb.append("properties." + entry.getKey() + "=" + entry.getValue()); if (it.hasNext()) { sb.append(" AND "); } } } Optional<QueryBuilder> filter = QueryBuilderHelper.queryBuilder(sb.toString()); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); if (filter.isPresent()) { searchSourceBuilder.query(filter.get()); } searchSourceBuilder .aggregation(AggregationBuilders.min("start").field(FieldName.DATETIME_FULL)) .aggregation(AggregationBuilders.max("end").field(FieldName.DATETIME_FULL)) .aggregation(AggregationBuilders.geoBounds("bbox").field(FieldName.CENTROID)) //we don't want records back, just aggregations .size(0); SearchRequest sr = new SearchRequest().indices(index).source(searchSourceBuilder); log.debug("Getting stats on {} with the following request: \n {}", index, sr.source().toString()); SearchResponse resp; try { resp = restClient.search(sr); } catch (Exception ex) { throw new RuntimeException("Error getting aggregations.", ex); } Min start = resp.getAggregations().get("start"); Max end = resp.getAggregations().get("end"); GeoBounds bboxAggregation = resp.getAggregations().get("bbox"); Extent extent = new Extent(); List<String> temporalExtent = new ArrayList<>(2); Consumer<String> temporalCheck = value -> { if (null != value && !value.toLowerCase().contains("infinity")) { temporalExtent.add(value); } else { temporalExtent.add(null); } }; temporalCheck.accept(start.getValueAsString()); temporalCheck.accept(end.getValueAsString()); extent.getTemporal().getInterval().add(temporalExtent); List<Double> bbox = getBboxExtent(bboxAggregation); if (bbox != null && (bbox.size() == 4 || bbox.size() == 6)) { extent.getSpatial().getBbox().add(bbox); } return extent; }
Example #5
Source File: ElasticStatsService.java From staccato with Apache License 2.0 | 4 votes |
/** * Blocking method to fetch aggregations from Elasticsearch. * * @param index The index to build aggregations for. * @return An {@link ItemStatisticsResponse ItemStatisticsResponse} object containing the aggregated data for the index */ private ItemStatisticsResponse getStatsSync(String index) { SearchRequest sr = new SearchRequest().indices(index).source( new SearchSourceBuilder() .aggregation(AggregationBuilders.min("start").field(FieldName.DATETIME_FULL)) .aggregation(AggregationBuilders.max("end").field(FieldName.DATETIME_FULL)) //.stats(AggregationBuilders.terms("licenses").field(FieldName.LICENSE_FULL).size(5).order(BucketOrder.count(true))) //.stats(AggregationBuilders.terms("providers").field(FieldName.PROVIDER_FULL).size(5).order(BucketOrder.count(true))) .aggregation(AggregationBuilders.geoBounds("bbox").field(FieldName.CENTROID)) //we don't want records back, just aggregations .size(0) ); log.debug("Getting stats on {} with the following request: \n {}", index, sr.source().toString()); SearchResponse resp; try { resp = restClient.search(sr, RequestOptions.DEFAULT); } catch (IOException ex) { return new ItemStatisticsResponse(); } Min start = resp.getAggregations().get("start"); Max end = resp.getAggregations().get("end"); //Terms lics = resp.getAggregations().get("licenses"); //Terms provs = resp.getAggregations().get("providers"); GeoBounds bboxAggregation = resp.getAggregations().get("bbox"); ItemStatisticsResponse stats = new ItemStatisticsResponse(); stats.setType(index); stats.setCount(resp.getHits().totalHits); String endValue = end.getValueAsString(); if (null != endValue && !endValue.toLowerCase().contains("infinity")) { stats.setEnd(endValue); } String startValue = start.getValueAsString(); if (null != startValue && !startValue.toLowerCase().contains("infinity")) { stats.setStart(startValue); } List<Double> bbox = getBboxExtent(bboxAggregation); if (bbox != null && (bbox.size() == 4 || bbox.size() == 6)) { stats.setBounds(bbox); } return stats; }