Search in sources :

Example 1 with ParsedExtendedStats

use of org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats in project vind by RBMHTechnology.

the class ResultUtils method getStatsFacetResults.

private static Pair<String, StatsFacetResult> getStatsFacetResults(Map<String, Aggregation> aggregations, Facet.StatsFacet statsFacet) {
    final FieldDescriptor field = statsFacet.getField();
    Object min = null;
    Object max = null;
    Double sum = null;
    Long count = null;
    Long missing = null;
    Double sumOfSquares = null;
    Object mean = null;
    Double stddev = null;
    Map<Double, Double> percentiles = null;
    List distinctValues = null;
    Long countDistinct = null;
    Long cardinality = null;
    final Optional<ParsedExtendedStats> statsAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName())).map(Map.Entry::getValue).map(agg -> (ParsedExtendedStats) agg).findFirst();
    if (statsAggregation.isPresent() && statsFacet.getSum()) {
        sum = statsAggregation.get().getSum();
    }
    if (statsAggregation.isPresent() && statsFacet.getMin()) {
        min = DocumentUtil.castForDescriptor(statsAggregation.get().getMin(), field, FieldDescriptor.UseCase.Facet);
    }
    if (statsAggregation.isPresent() && statsFacet.getMax()) {
        max = DocumentUtil.castForDescriptor(statsAggregation.get().getMax(), field, FieldDescriptor.UseCase.Facet);
    }
    if (statsAggregation.isPresent() && statsFacet.getCount()) {
        count = statsAggregation.get().getCount();
    }
    if (statsFacet.getMissing()) {
        final Optional<Aggregation> statsMissingAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_missing")).map(Map.Entry::getValue).findFirst();
        if (statsMissingAggregation.isPresent()) {
            missing = ((ParsedMissing) statsMissingAggregation.get()).getDocCount();
        }
    }
    if (statsAggregation.isPresent() && statsFacet.getSumOfSquares()) {
        sumOfSquares = statsAggregation.get().getSumOfSquares();
    }
    if (statsAggregation.isPresent() && statsFacet.getMean()) {
        mean = DocumentUtil.castForDescriptor(statsAggregation.get().getAvg(), field, FieldDescriptor.UseCase.Facet);
    }
    if (statsAggregation.isPresent() && statsFacet.getStddev()) {
        stddev = statsAggregation.get().getStdDeviation();
    }
    if (ArrayUtils.isNotEmpty(statsFacet.getPercentiles())) {
        final Optional<Aggregation> statsPercentilesAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_percentiles")).map(Map.Entry::getValue).findFirst();
        if (statsPercentilesAggregation.isPresent()) {
            percentiles = Streams.stream(((ParsedPercentiles) statsPercentilesAggregation.get()).iterator()).collect(Collectors.toMap((Percentile p) -> Double.valueOf(p.getPercent()), (Percentile p) -> Double.valueOf(p.getValue())));
        }
    }
    if (statsFacet.getDistinctValues()) {
        final Optional<Aggregation> statsValuesAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_values")).map(Map.Entry::getValue).findFirst();
        if (statsValuesAggregation.isPresent()) {
            distinctValues = ((ParsedTerms) statsValuesAggregation.get()).getBuckets().stream().filter(bucket -> bucket.getDocCount() > 0).map(MultiBucketsAggregation.Bucket::getKey).map(o -> DocumentUtil.castForDescriptor(o, field, FieldDescriptor.UseCase.Facet)).collect(Collectors.toList());
        }
    }
    if (statsFacet.getCountDistinct()) {
        final Optional<Aggregation> statsValuesAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_values")).map(Map.Entry::getValue).findFirst();
        if (statsValuesAggregation.isPresent()) {
            countDistinct = ((ParsedTerms) statsValuesAggregation.get()).getBuckets().stream().filter(bucket -> bucket.getDocCount() > 0).count();
        }
    }
    if (statsFacet.getCardinality()) {
        final Optional<Aggregation> statsCardinalityAggregation = aggregations.entrySet().stream().filter(entry -> entry.getKey().endsWith(statsFacet.getFacetName() + "_cardinality")).map(Map.Entry::getValue).findFirst();
        if (statsCardinalityAggregation.isPresent()) {
            cardinality = ((ParsedCardinality) statsCardinalityAggregation.get()).getValue();
        }
    }
    final StatsFacetResult statsFacetResult = new StatsFacetResult(field, min, max, sum, count, missing, sumOfSquares, mean, stddev, percentiles, distinctValues, countDistinct, cardinality);
    return Pair.of(statsFacet.getFacetName(), statsFacetResult);
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) StatsFacetResult(com.rbmhtechnology.vind.api.result.facet.StatsFacetResult) GetResponse(org.elasticsearch.action.get.GetResponse) Arrays(java.util.Arrays) FacetValue(com.rbmhtechnology.vind.api.result.facet.FacetValue) ParsedDateRange(org.elasticsearch.search.aggregations.bucket.range.ParsedDateRange) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) IntervalFacetResult(com.rbmhtechnology.vind.api.result.facet.IntervalFacetResult) Pair(org.apache.commons.lang3.tuple.Pair) Facet(com.rbmhtechnology.vind.api.query.facet.Facet) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) QueryFacetResult(com.rbmhtechnology.vind.api.result.facet.QueryFacetResult) ParsedSingleValueNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.ParsedSingleValueNumericMetricsAggregation) SearchHit(org.elasticsearch.search.SearchHit) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Collection(java.util.Collection) ParsedCardinality(org.elasticsearch.search.aggregations.metrics.ParsedCardinality) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) Streams(com.google.common.collect.Streams) GetResult(com.rbmhtechnology.vind.api.result.GetResult) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) RealTimeGet(com.rbmhtechnology.vind.api.query.get.RealTimeGet) List(java.util.List) ParsedPercentiles(org.elasticsearch.search.aggregations.metrics.ParsedPercentiles) Stream(java.util.stream.Stream) ParsedMissing(org.elasticsearch.search.aggregations.bucket.missing.ParsedMissing) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Optional(java.util.Optional) SubdocumentFacetResult(com.rbmhtechnology.vind.api.result.facet.SubdocumentFacetResult) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CollectionUtils(org.apache.commons.collections.CollectionUtils) Percentile(org.elasticsearch.search.aggregations.metrics.Percentile) Aggregation(org.elasticsearch.search.aggregations.Aggregation) ParsedFilters(org.elasticsearch.search.aggregations.bucket.filter.ParsedFilters) TermFacetResult(com.rbmhtechnology.vind.api.result.facet.TermFacetResult) FacetResults(com.rbmhtechnology.vind.api.result.FacetResults) ParsedTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms) RangeFacetResult(com.rbmhtechnology.vind.api.result.facet.RangeFacetResult) ParsedDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram) ParsedExtendedStats(org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats) Document(com.rbmhtechnology.vind.api.Document) ParsedRange(org.elasticsearch.search.aggregations.bucket.range.ParsedRange) PivotFacetResult(com.rbmhtechnology.vind.api.result.facet.PivotFacetResult) DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) Collections(java.util.Collections) ParsedHistogram(org.elasticsearch.search.aggregations.bucket.histogram.ParsedHistogram) ParsedExtendedStats(org.elasticsearch.search.aggregations.metrics.ParsedExtendedStats) Percentile(org.elasticsearch.search.aggregations.metrics.Percentile) ParsedTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms) StatsFacetResult(com.rbmhtechnology.vind.api.result.facet.StatsFacetResult) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) ParsedSingleValueNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.ParsedSingleValueNumericMetricsAggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Streams (com.google.common.collect.Streams)1 Document (com.rbmhtechnology.vind.api.Document)1 Facet (com.rbmhtechnology.vind.api.query.facet.Facet)1 RealTimeGet (com.rbmhtechnology.vind.api.query.get.RealTimeGet)1 FacetResults (com.rbmhtechnology.vind.api.result.FacetResults)1 GetResult (com.rbmhtechnology.vind.api.result.GetResult)1 FacetValue (com.rbmhtechnology.vind.api.result.facet.FacetValue)1 IntervalFacetResult (com.rbmhtechnology.vind.api.result.facet.IntervalFacetResult)1 PivotFacetResult (com.rbmhtechnology.vind.api.result.facet.PivotFacetResult)1 QueryFacetResult (com.rbmhtechnology.vind.api.result.facet.QueryFacetResult)1 RangeFacetResult (com.rbmhtechnology.vind.api.result.facet.RangeFacetResult)1 StatsFacetResult (com.rbmhtechnology.vind.api.result.facet.StatsFacetResult)1 SubdocumentFacetResult (com.rbmhtechnology.vind.api.result.facet.SubdocumentFacetResult)1 TermFacetResult (com.rbmhtechnology.vind.api.result.facet.TermFacetResult)1 DocumentFactory (com.rbmhtechnology.vind.model.DocumentFactory)1 FieldDescriptor (com.rbmhtechnology.vind.model.FieldDescriptor)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1