Search in sources :

Example 56 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method getAggregationResultsByName.

private static Map<String, List<Aggregation>> getAggregationResultsByName(ElasticsearchSearchQueryBase query, Iterable<Aggregation> aggs) {
    Map<String, List<Aggregation>> aggsByName = new HashMap<>();
    if (aggs == null) {
        return aggsByName;
    }
    for (Aggregation agg : aggs) {
        if (agg.getName().equals(ElasticsearchSearchQueryBase.TOP_HITS_AGGREGATION_NAME)) {
            continue;
        }
        String aggName = query.getAggregationName(agg.getName());
        List<Aggregation> l = aggsByName.computeIfAbsent(aggName, k -> new ArrayList<>());
        l.add(agg);
    }
    return aggsByName;
}
Also used : MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation)

Example 57 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method reduceTermsResults.

private static TermsResult reduceTermsResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey = new HashMap<>();
    for (Aggregation agg : aggs) {
        if (agg instanceof Terms) {
            Terms h = (Terms) agg;
            for (Terms.Bucket b : h.getBuckets()) {
                TopHits exactMatchTopHits = b.getAggregations().get(ElasticsearchSearchQueryBase.TOP_HITS_AGGREGATION_NAME);
                String mapKey = bucketKeyToString(b.getKey(), exactMatchTopHits);
                List<MultiBucketsAggregation.Bucket> existingBucketByName = bucketsByKey.computeIfAbsent(mapKey, k -> new ArrayList<>());
                existingBucketByName.add(b);
            }
        } else {
            throw new VertexiumException("Aggregation is not a terms: " + agg.getClass().getName());
        }
    }
    return new MultiBucketsAggregationReducer<TermsResult, TermsBucket>() {

        @Override
        protected TermsBucket createBucket(Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets) {
            return new TermsBucket(key, count, nestedResults);
        }

        @Override
        protected TermsResult bucketsToResults(List<TermsBucket> buckets) {
            return new TermsResult(buckets);
        }
    }.reduce(query, bucketsByKey);
}
Also used : InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) VertexiumException(org.vertexium.VertexiumException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Example 58 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method reducePercentilesResults.

private static PercentilesResult reducePercentilesResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    List<Percentile> results = new ArrayList<>();
    if (aggs.size() != 1) {
        throw new VertexiumException("Unexpected number of aggregations. Expected 1 but found: " + aggs.size());
    }
    Aggregation agg = aggs.get(0);
    if (agg instanceof Percentiles) {
        Percentiles percentiles = (Percentiles) agg;
        StreamUtils.stream(percentiles).filter(percentile -> !Double.isNaN(percentile.getValue())).forEach(percentile -> results.add(new Percentile(percentile.getPercent(), percentile.getValue())));
    } else {
        throw new VertexiumException("Aggregation is not a percentile: " + agg.getClass().getName());
    }
    return new PercentilesResult(results);
}
Also used : MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) SearchHit(org.elasticsearch.search.SearchHit) InternalDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram) InternalGeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid) java.util(java.util) InternalHistogram(org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram) InternalExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.InternalExtendedStats) VertexiumException(org.vertexium.VertexiumException) SearchHits(org.elasticsearch.search.SearchHits) InternalTerms(org.elasticsearch.search.aggregations.bucket.terms.InternalTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) GeoRect(org.vertexium.type.GeoRect) Range(org.elasticsearch.search.aggregations.bucket.range.Range) org.vertexium.query(org.vertexium.query) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalRange(org.elasticsearch.search.aggregations.bucket.range.InternalRange) SearchResponse(org.elasticsearch.action.search.SearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) StreamUtils(org.vertexium.util.StreamUtils) Percentiles(org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles) GeoPoint(org.vertexium.type.GeoPoint) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) Percentiles(org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles) VertexiumException(org.vertexium.VertexiumException)

Example 59 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method reduceGeohashResults.

private static GeohashResult reduceGeohashResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey = new HashMap<>();
    for (Aggregation agg : aggs) {
        if (agg instanceof GeoHashGrid) {
            GeoHashGrid h = (GeoHashGrid) agg;
            for (GeoHashGrid.Bucket b : h.getBuckets()) {
                List<MultiBucketsAggregation.Bucket> existingBucket = bucketsByKey.computeIfAbsent(b.getKey(), k -> new ArrayList<>());
                existingBucket.add(b);
            }
        } else {
            throw new VertexiumException("Aggregation is not a geohash: " + agg.getClass().getName());
        }
    }
    return new MultiBucketsAggregationReducer<GeohashResult, GeohashBucket>() {

        @Override
        protected GeohashBucket createBucket(final Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets) {
            GeoPoint geoPoint = getAverageGeoPointFromBuckets(buckets);
            return new GeohashBucket(key.toString(), count, geoPoint, nestedResults) {

                @Override
                public GeoRect getGeoCell() {
                    org.elasticsearch.common.geo.GeoPoint northWest = new org.elasticsearch.common.geo.GeoPoint();
                    org.elasticsearch.common.geo.GeoPoint southEast = new org.elasticsearch.common.geo.GeoPoint();
                    GeohashUtils.decodeCell(key.toString(), northWest, southEast);
                    return new GeoRect(new GeoPoint(northWest.getLat(), northWest.getLon()), new GeoPoint(southEast.getLat(), southEast.getLon()));
                }
            };
        }

        @Override
        protected GeohashResult bucketsToResults(List<GeohashBucket> buckets) {
            return new GeohashResult(buckets);
        }
    }.reduce(query, bucketsByKey);
}
Also used : GeoRect(org.vertexium.type.GeoRect) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) GeoPoint(org.vertexium.type.GeoPoint) VertexiumException(org.vertexium.VertexiumException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalGeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid)

Example 60 with Aggregation

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method reduceHistogramResults.

private static HistogramResult reduceHistogramResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey = new HashMap<>();
    for (Aggregation agg : aggs) {
        if (agg instanceof Histogram) {
            Histogram h = (Histogram) agg;
            org.vertexium.query.Aggregation queryAgg = query.getAggregationByName(query.getAggregationName(h.getName()));
            boolean isCalendarFieldQuery = queryAgg != null && queryAgg instanceof CalendarFieldAggregation;
            for (Histogram.Bucket b : h.getBuckets()) {
                if (isCalendarFieldQuery && b.getKey().toString().equals("-1.0")) {
                    continue;
                }
                List<MultiBucketsAggregation.Bucket> l = bucketsByKey.computeIfAbsent(b.getKey(), k -> new ArrayList<>());
                l.add(b);
            }
        } else {
            throw new VertexiumException("Aggregation is not a histogram: " + agg.getClass().getName());
        }
    }
    return new MultiBucketsAggregationReducer<HistogramResult, HistogramBucket>() {

        @Override
        protected HistogramBucket createBucket(Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets) {
            return new HistogramBucket(key, count, nestedResults);
        }

        @Override
        protected HistogramResult bucketsToResults(List<HistogramBucket> buckets) {
            return new HistogramResult(buckets);
        }
    }.reduce(query, bucketsByKey);
}
Also used : InternalDateHistogram(org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram) InternalHistogram(org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) org.vertexium.query(org.vertexium.query) VertexiumException(org.vertexium.VertexiumException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)53 HashMap (java.util.HashMap)25 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 List (java.util.List)20 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)19 ArrayList (java.util.ArrayList)17 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)17 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)16 Script (org.elasticsearch.script.Script)13 ScriptedMetric (org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric)13 Map (java.util.Map)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)10 Test (org.junit.Test)8 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 BucketSpec (org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec)6 DateTime (org.joda.time.DateTime)6 Aggregation (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregation)5 VertexiumException (org.vertexium.VertexiumException)5 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)4 IOException (java.io.IOException)4