org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation. 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: ESQueryResponseHandler.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 获取聚合结果,进行统一封装 //@param functionNames 检索提供的函数别名 //@param innerAggregations
 * es 聚合底层聚合结果
 *
 * @param key         分组组合key
 * @param countCesult 统一统计结果
 */
private void wrapCountResult(Aggregation agregation, String key, Map<String, List<Map<String, Object>>> countCesult) {

    if (agregation instanceof NumericMetricsAggregation.SingleValue) {// max
        // ,min,sum
        final NumericMetricsAggregation.SingleValue numericProperty = (NumericMetricsAggregation.SingleValue) agregation;

        final String functionName = numericProperty.getName();
        final double value = numericProperty.value();

        final HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put(key, value);

        List<Map<String, Object>> groupList = countCesult.get(functionName);
        if (groupList == null) {
            groupList = new ArrayList<>();
        }
        groupList.add(stringObjectHashMap);

        countCesult.put(functionName, groupList);
    }
}
 
Example #2
Source File: ESStatisticResponseHandler.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
public Map<String, Number> handler(final Aggregations aggregations) {
    Map<String, Number> statisticResult = new HashMap<>();
    if (aggregations == null) {
        return null;
    }

    for (Aggregation aggregation : aggregations.asList()) {
        if (aggregation instanceof NumericMetricsAggregation.SingleValue) {
            final NumericMetricsAggregation.SingleValue numericProperty = (NumericMetricsAggregation
                    .SingleValue) aggregation;
            final String functionName = numericProperty.getName();
            final double value = numericProperty.value();

            statisticResult.put(functionName, value);
        }
    }
    return statisticResult;
}
 
Example #3
Source File: SearchFeatureDaoTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Test
public void getFeaturesForPeriod_returnNonEmpty_givenDefaultValue() throws Exception {
    long start = 100L;
    long end = 200L;

    // pre-conditions
    when(ParseUtils.generateInternalFeatureQuery(eq(detector), eq(start), eq(end), eq(xContent))).thenReturn(searchSourceBuilder);
    when(searchResponse.getHits()).thenReturn(new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 1f));

    List<Aggregation> aggList = new ArrayList<>(1);

    NumericMetricsAggregation.SingleValue agg = mock(NumericMetricsAggregation.SingleValue.class);
    when(agg.getName()).thenReturn("deny_max");
    when(agg.value()).thenReturn(0d);

    aggList.add(agg);

    Aggregations aggregations = new Aggregations(aggList);
    when(searchResponse.getAggregations()).thenReturn(aggregations);

    // test
    Optional<double[]> result = searchFeatureDao.getFeaturesForPeriod(detector, start, end);

    // verify
    assertTrue(result.isPresent());
}
 
Example #4
Source File: ProfileThreadSnapshotQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
protected int querySequenceWithAgg(AbstractAggregationBuilder aggregationBuilder, String segmentId, long start, long end) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    sourceBuilder.query(boolQueryBuilder);
    List<QueryBuilder> mustQueryList = boolQueryBuilder.must();

    mustQueryList.add(QueryBuilders.termQuery(ProfileThreadSnapshotRecord.SEGMENT_ID, segmentId));
    mustQueryList.add(QueryBuilders.rangeQuery(ProfileThreadSnapshotRecord.DUMP_TIME).gte(start).lte(end));
    sourceBuilder.size(0);

    sourceBuilder.aggregation(aggregationBuilder);
    SearchResponse response = getClient().search(ProfileThreadSnapshotRecord.INDEX_NAME, sourceBuilder);
    NumericMetricsAggregation.SingleValue agg = response.getAggregations().get(ProfileThreadSnapshotRecord.SEQUENCE);

    return (int) agg.value();
}
 
Example #5
Source File: CSVResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private  boolean allNumericAggregations(Aggregations aggregations) {
    List<Aggregation> aggregationList = aggregations.asList();
    for(Aggregation aggregation : aggregationList){
        if(!(aggregation instanceof NumericMetricsAggregation)){
            return false;
        }
    }
    return true;
}
 
Example #6
Source File: ObjectResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private boolean allNumericAggregations(Aggregations aggregations) {
    List<Aggregation> aggregationList = aggregations.asList();
    for (Aggregation aggregation : aggregationList) {
        if (!(aggregation instanceof NumericMetricsAggregation)) {
            return false;
        }
    }
    return true;
}
 
Example #7
Source File: CSVResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private  void handleAggregations(Aggregations aggregations, List<String> headers, List<List<String>> lines) throws CsvExtractorException {
    if(allNumericAggregations(aggregations)){
        lines.get(this.currentLineIndex).addAll(fillHeaderAndCreateLineForNumericAggregations(aggregations, headers));
        return;
    }
    //aggregations with size one only supported when not metrics.
    List<Aggregation> aggregationList = aggregations.asList();
    if(aggregationList.size() > 1){
        throw new CsvExtractorException("currently support only one aggregation at same level (Except for numeric metrics)");
    }
    Aggregation aggregation = aggregationList.get(0);
    //we want to skip singleBucketAggregations (nested,reverse_nested,filters)
    if(aggregation instanceof SingleBucketAggregation){
        Aggregations singleBucketAggs = ((SingleBucketAggregation) aggregation).getAggregations();
        handleAggregations(singleBucketAggs, headers, lines);
        return;
    }
    if(aggregation instanceof NumericMetricsAggregation){
        handleNumericMetricAggregation(headers, lines.get(currentLineIndex), aggregation);
        return;
    }
    if(aggregation instanceof GeoBounds){
        handleGeoBoundsAggregation(headers, lines, (GeoBounds) aggregation);
        return;
    }
    if(aggregation instanceof TopHits){
        //todo: handle this . it returns hits... maby back to normal?
        //todo: read about this usages
        // TopHits topHitsAggregation = (TopHits) aggregation;
    }
    if(aggregation instanceof MultiBucketsAggregation){
        MultiBucketsAggregation bucketsAggregation = (MultiBucketsAggregation) aggregation;
        String name = bucketsAggregation.getName();
        //checking because it can comes from sub aggregation again
        if(!headers.contains(name)){
            headers.add(name);
        }
        Collection<? extends MultiBucketsAggregation.Bucket> buckets = bucketsAggregation.getBuckets();

        //clone current line.
        List<String> currentLine = lines.get(this.currentLineIndex);
        List<String> clonedLine = new ArrayList<>(currentLine);

        //call handle_Agg with current_line++
        boolean firstLine = true;
        for (MultiBucketsAggregation.Bucket bucket : buckets) {
            //each bucket need to add new line with current line copied => except for first line
            String key = bucket.getKeyAsString();
            if(firstLine){
                firstLine = false;
            }
            else {
                currentLineIndex++;
                currentLine = new ArrayList<String>(clonedLine);
                lines.add(currentLine);
            }
            currentLine.add(key);
            handleAggregations(bucket.getAggregations(),headers,lines);

        }
    }

}
 
Example #8
Source File: ObjectResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private void handleAggregations(Aggregations aggregations, List<String> headers, List<List<Object>> lines) throws ObjectResultsExtractException {
    if (allNumericAggregations(aggregations)) {
        lines.get(this.currentLineIndex).addAll(fillHeaderAndCreateLineForNumericAggregations(aggregations, headers));
        return;
    }
    //aggregations with size one only supported when not metrics.
    List<Aggregation> aggregationList = aggregations.asList();
    if (aggregationList.size() > 1) {
        throw new ObjectResultsExtractException("currently support only one aggregation at same level (Except for numeric metrics)");
    }
    Aggregation aggregation = aggregationList.get(0);
    //we want to skip singleBucketAggregations (nested,reverse_nested,filters)
    if (aggregation instanceof SingleBucketAggregation) {
        Aggregations singleBucketAggs = ((SingleBucketAggregation) aggregation).getAggregations();
        handleAggregations(singleBucketAggs, headers, lines);
        return;
    }
    if (aggregation instanceof NumericMetricsAggregation) {
        handleNumericMetricAggregation(headers, lines.get(currentLineIndex), aggregation);
        return;
    }
    if (aggregation instanceof GeoBounds) {
        handleGeoBoundsAggregation(headers, lines, (GeoBounds) aggregation);
        return;
    }
    if (aggregation instanceof TopHits) {
        //todo: handle this . it returns hits... maby back to normal?
        //todo: read about this usages
        // TopHits topHitsAggregation = (TopHits) aggregation;
    }
    if (aggregation instanceof MultiBucketsAggregation) {
        MultiBucketsAggregation bucketsAggregation = (MultiBucketsAggregation) aggregation;
        String name = bucketsAggregation.getName();
        //checking because it can comes from sub aggregation again
        if (!headers.contains(name)) {
            headers.add(name);
        }
        Collection<? extends MultiBucketsAggregation.Bucket> buckets = bucketsAggregation.getBuckets();

        //clone current line.
        List<Object> currentLine = lines.get(this.currentLineIndex);
        List<Object> clonedLine = new ArrayList<>(currentLine);

        //call handle_Agg with current_line++
        boolean firstLine = true;
        for (MultiBucketsAggregation.Bucket bucket : buckets) {
            //each bucket need to add new line with current line copied => except for first line
            String key = bucket.getKeyAsString();
            if (firstLine) {
                firstLine = false;
            } else {
                currentLineIndex++;
                currentLine = new ArrayList<Object>(clonedLine);
                lines.add(currentLine);
            }
            currentLine.add(key);
            handleAggregations(bucket.getAggregations(), headers, lines);

        }
    }

}
 
Example #9
Source File: ObjectResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private void handleNumericMetricAggregation(List<String> header, List<Object> line, Aggregation aggregation) throws ObjectResultsExtractException {
    String name = aggregation.getName();

    if (aggregation instanceof NumericMetricsAggregation.SingleValue) {
        if (!header.contains(name)) {
            header.add(name);
        }
        line.add(((NumericMetricsAggregation.SingleValue) aggregation).value());
    }
    //todo:Numeric MultiValue - Stats,ExtendedStats,Percentile...
    else if (aggregation instanceof NumericMetricsAggregation.MultiValue) {
        if (aggregation instanceof Stats) {
            String[] statsHeaders = new String[]{"count", "sum", "avg", "min", "max"};
            boolean isExtendedStats = aggregation instanceof ExtendedStats;
            if (isExtendedStats) {
                String[] extendedHeaders = new String[]{"sumOfSquares", "variance", "stdDeviation"};
                statsHeaders = Util.concatStringsArrays(statsHeaders, extendedHeaders);
            }
            mergeHeadersWithPrefix(header, name, statsHeaders);
            Stats stats = (Stats) aggregation;
            line.add(stats.getCount());
            line.add(stats.getSum());
            line.add(stats.getAvg());
            line.add(stats.getMin());
            line.add(stats.getMax());
            if (isExtendedStats) {
                ExtendedStats extendedStats = (ExtendedStats) aggregation;
                line.add(extendedStats.getSumOfSquares());
                line.add(extendedStats.getVariance());
                line.add(extendedStats.getStdDeviation());
            }
        } else if (aggregation instanceof Percentiles) {
            String[] percentileHeaders = new String[]{"1.0", "5.0", "25.0", "50.0", "75.0", "95.0", "99.0"};
            mergeHeadersWithPrefix(header, name, percentileHeaders);
            Percentiles percentiles = (Percentiles) aggregation;
            line.add(percentiles.percentile(1.0));
            line.add(percentiles.percentile(5.0));
            line.add(percentiles.percentile(25.0));
            line.add(percentiles.percentile(50.0));
            line.add(percentiles.percentile(75));
            line.add(percentiles.percentile(95.0));
            line.add(percentiles.percentile(99.0));
        } else {
            throw new ObjectResultsExtractException("unknown NumericMetricsAggregation.MultiValue:" + aggregation.getClass());
        }

    } else {
        throw new ObjectResultsExtractException("unknown NumericMetricsAggregation" + aggregation.getClass());
    }
}