Search in sources :

Example 16 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project graylog2-server by Graylog2.

the class ESPivotTest method searchResultForAllMessagesIncludesPivotTimerangeForNoDocuments.

@Test
public void searchResultForAllMessagesIncludesPivotTimerangeForNoDocuments() throws InvalidRangeParametersException {
    DateTimeUtils.setCurrentMillisFixed(1578584665408L);
    final long documentCount = 0;
    returnDocumentCount(queryResult, documentCount);
    final Aggregations mockMetricAggregation = createTimestampRangeAggregations(0d, 0d);
    when(queryResult.getAggregations()).thenReturn(mockMetricAggregation);
    when(query.effectiveTimeRange(pivot)).thenReturn(RelativeRange.create(0));
    final SearchType.Result result = this.esPivot.doExtractResult(job, query, pivot, queryResult, aggregations, queryContext);
    final PivotResult pivotResult = (PivotResult) result;
    assertThat(pivotResult.effectiveTimerange()).isEqualTo(AbsoluteRange.create(DateTime.parse("1970-01-01T00:00:00.000Z"), DateTime.parse("2020-01-09T15:44:25.408Z")));
}
Also used : PivotResult(org.graylog.plugins.views.search.searchtypes.pivot.PivotResult) Aggregations(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations) SearchType(org.graylog.plugins.views.search.SearchType) Test(org.junit.Test)

Example 17 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project snow-owl by b2ihealthcare.

the class EsDocumentSearcher method aggregate.

@Override
public <T> Aggregation<T> aggregate(AggregationBuilder<T> aggregation) throws IOException {
    final String aggregationName = aggregation.getName();
    final EsClient client = admin.client();
    final DocumentMapping mapping = admin.mappings().getMapping(aggregation.getFrom());
    final EsQueryBuilder esQueryBuilder = new EsQueryBuilder(mapping, admin.settings(), admin.log());
    final QueryBuilder esQuery = esQueryBuilder.build(aggregation.getQuery());
    final SearchRequest req = new SearchRequest(admin.getTypeIndex(mapping));
    final SearchSourceBuilder reqSource = req.source().query(esQuery).size(0).trackScores(false).trackTotalHitsUpTo(Integer.MAX_VALUE);
    // field selection
    final boolean fetchSource = applySourceFiltering(aggregation.getFields(), mapping, reqSource);
    reqSource.aggregation(toEsAggregation(mapping, aggregation, fetchSource));
    SearchResponse response = null;
    try {
        response = client.search(req);
    } catch (Exception e) {
        admin.log().error("Couldn't execute aggregation", e);
        throw new IndexException("Couldn't execute aggregation: " + e.getMessage(), null);
    }
    ImmutableMap.Builder<Object, Bucket<T>> buckets = ImmutableMap.builder();
    Aggregations topLevelAggregations = response.getAggregations();
    Nested nested = topLevelAggregations.get(nestedAggName(aggregation));
    Terms aggregationResult;
    if (nested != null) {
        aggregationResult = nested.getAggregations().get(aggregationName);
    } else {
        aggregationResult = topLevelAggregations.get(aggregationName);
    }
    for (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket bucket : aggregationResult.getBuckets()) {
        final TopHits topHits;
        if (nested != null) {
            final ReverseNested reverseNested = bucket.getAggregations().get(reverseNestedAggName(aggregation));
            topHits = reverseNested.getAggregations().get(topHitsAggName(aggregation));
        } else {
            topHits = bucket.getAggregations().get(topHitsAggName(aggregation));
        }
        Hits<T> hits;
        if (topHits != null) {
            hits = toHits(aggregation.getSelect(), List.of(aggregation.getFrom()), aggregation.getFields(), fetchSource, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount(), null, topHits.getHits());
        } else {
            hits = new Hits<>(Collections.emptyList(), null, aggregation.getBucketHitsLimit(), (int) bucket.getDocCount());
        }
        buckets.put(bucket.getKey(), new Bucket<>(bucket.getKey(), hits));
    }
    return new Aggregation<>(aggregationName, buckets.build());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) EsQueryBuilder(com.b2international.index.es.query.EsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Aggregation(com.b2international.index.aggregations.Aggregation) TopHits(org.elasticsearch.search.aggregations.metrics.TopHits) EsQueryBuilder(com.b2international.index.es.query.EsQueryBuilder) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) EsClient(com.b2international.index.es.client.EsClient) DocumentMapping(com.b2international.index.mapping.DocumentMapping) FormattedRuntimeException(com.b2international.commons.exceptions.FormattedRuntimeException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse) Bucket(com.b2international.index.aggregations.Bucket)

Example 18 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project elasticsearch by elastic.

the class QuerySearchResult method consumeAggs.

/**
     * Returns and nulls out the aggregation for this search results. This allows to free up memory once the aggregation is consumed.
     * @throws IllegalStateException if the aggregations have already been consumed.
     */
public Aggregations consumeAggs() {
    if (aggregations == null) {
        throw new IllegalStateException("aggs already consumed");
    }
    Aggregations aggs = aggregations;
    aggregations = null;
    return aggs;
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations)

Example 19 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project elasticsearch by elastic.

the class GeoDistanceIT method testGeoDistanceAggregation.

public void testGeoDistanceAggregation() throws IOException {
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "TestPosition").startObject("location").field("lat", src_lat).field("lon", src_lon).endObject().endObject()).get();
    refresh();
    SearchRequestBuilder search = client().prepareSearch("test");
    String name = "TestPosition";
    search.setQuery(QueryBuilders.matchAllQuery()).setTypes("type1").addAggregation(AggregationBuilders.geoDistance(name, new GeoPoint(tgt_lat, tgt_lon)).field("location").unit(DistanceUnit.MILES).addRange(0, 25000));
    // no hits please
    search.setSize(0);
    SearchResponse response = search.get();
    Aggregations aggregations = response.getAggregations();
    assertNotNull(aggregations);
    InternalGeoDistance geoDistance = aggregations.get(name);
    assertNotNull(geoDistance);
    List<? extends Range.Bucket> buckets = ((Range) geoDistance).getBuckets();
    assertNotNull("Buckets should not be null", buckets);
    assertEquals("Unexpected number of buckets", 1, buckets.size());
    assertEquals("Unexpected doc count for geo distance", 1, buckets.get(0).getDocCount());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Aggregations(org.elasticsearch.search.aggregations.Aggregations) InternalGeoDistance(org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistance) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 20 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project molgenis by molgenis.

the class AggregateResponseParser method parseAggregations.

private Map<Object, Object> parseAggregations(Attribute aggAttr1, Attribute aggAttr2, Attribute aggAttrDistinct, Aggregations aggs) {
    Map<Object, Object> counts = new HashMap<>();
    boolean isAttr1Nested = AggregateUtils.isNestedType(aggAttr1);
    boolean isAttr1Nillable = aggAttr1.isNillable();
    if (isAttr1Nested)
        aggs = removeNesting(aggs);
    Terms terms = getTermsAggregation(aggs, aggAttr1);
    for (Terms.Bucket bucket : terms.getBuckets()) {
        Object key = bucket.getKey();
        Object value;
        if (aggAttr2 != null) {
            Map<Object, Long> subCounts = new HashMap<>();
            boolean isAttr2Nested = AggregateUtils.isNestedType(aggAttr2);
            boolean isAttr2Nillable = aggAttr2.isNillable();
            Aggregations subAggs = bucket.getAggregations();
            if (isAttr1Nested)
                subAggs = removeReverseNesting(subAggs);
            if (isAttr2Nested)
                subAggs = removeNesting(subAggs);
            Terms subTerms = getTermsAggregation(subAggs, aggAttr2);
            for (Terms.Bucket subBucket : subTerms.getBuckets()) {
                Object subKey = subBucket.getKey();
                Long subValue;
                if (aggAttrDistinct != null) {
                    boolean isAttrDistinctNested = AggregateUtils.isNestedType(aggAttrDistinct);
                    Aggregations distinctAggs = subBucket.getAggregations();
                    if (isAttr2Nested)
                        distinctAggs = removeReverseNesting(distinctAggs);
                    if (isAttrDistinctNested)
                        distinctAggs = removeNesting(distinctAggs);
                    Cardinality distinctAgg = getDistinctAggregation(distinctAggs, aggAttrDistinct);
                    subValue = distinctAgg.getValue();
                } else {
                    subValue = subBucket.getDocCount();
                }
                subCounts.put(subKey, subValue);
            }
            if (isAttr2Nillable) {
                Missing subMissing = getMissingAggregation(subAggs, aggAttr2);
                String subKey = null;
                Long subValue;
                if (aggAttrDistinct != null) {
                    boolean isAttrDistinctNested = AggregateUtils.isNestedType(aggAttrDistinct);
                    Aggregations subDistinctAggs = subMissing.getAggregations();
                    if (isAttr2Nested)
                        subDistinctAggs = removeReverseNesting(subDistinctAggs);
                    if (isAttrDistinctNested)
                        subDistinctAggs = removeNesting(subDistinctAggs);
                    Cardinality distinctAgg = getDistinctAggregation(subDistinctAggs, aggAttrDistinct);
                    subValue = distinctAgg.getValue();
                } else {
                    subValue = subMissing.getDocCount();
                }
                subCounts.put(subKey, subValue);
            }
            value = subCounts;
        } else {
            if (aggAttrDistinct != null) {
                boolean isAttrDistinctNested = AggregateUtils.isNestedType(aggAttrDistinct);
                Aggregations distinctAggs = bucket.getAggregations();
                if (isAttr1Nested)
                    distinctAggs = removeReverseNesting(distinctAggs);
                if (isAttrDistinctNested)
                    distinctAggs = removeNesting(distinctAggs);
                Cardinality distinctAgg = getDistinctAggregation(distinctAggs, aggAttrDistinct);
                value = distinctAgg.getValue();
            } else {
                value = bucket.getDocCount();
            }
        }
        counts.put(key, value);
    }
    if (isAttr1Nillable) {
        Missing missing = getMissingAggregation(aggs, aggAttr1);
        String key = null;
        Object value;
        if (aggAttr2 != null) {
            Map<Object, Long> subCounts = new HashMap<>();
            boolean isAttr2Nested = AggregateUtils.isNestedType(aggAttr2);
            boolean isAttr2Nillable = aggAttr2.isNillable();
            Aggregations subAggs = missing.getAggregations();
            if (isAttr1Nested)
                subAggs = removeReverseNesting(subAggs);
            if (isAttr2Nested)
                subAggs = removeNesting(subAggs);
            Terms subTerms = getTermsAggregation(subAggs, aggAttr2);
            for (Terms.Bucket subBucket : subTerms.getBuckets()) {
                Object subKey = subBucket.getKey();
                Long subValue;
                if (aggAttrDistinct != null) {
                    boolean isAttrDistinctNested = AggregateUtils.isNestedType(aggAttrDistinct);
                    Aggregations distinctAggs = subBucket.getAggregations();
                    if (isAttr2Nested)
                        distinctAggs = removeReverseNesting(distinctAggs);
                    if (isAttrDistinctNested)
                        distinctAggs = removeNesting(distinctAggs);
                    Cardinality distinctAgg = getDistinctAggregation(distinctAggs, aggAttrDistinct);
                    subValue = distinctAgg.getValue();
                } else {
                    subValue = subBucket.getDocCount();
                }
                subCounts.put(subKey, subValue);
            }
            if (isAttr2Nillable) {
                Missing subMissing = getMissingAggregation(subAggs, aggAttr2);
                String subKey = null;
                Long subValue;
                if (aggAttrDistinct != null) {
                    boolean isAttrDistinctNested = AggregateUtils.isNestedType(aggAttrDistinct);
                    Aggregations subDistinctAggs = subMissing.getAggregations();
                    if (isAttr2Nested)
                        subDistinctAggs = removeReverseNesting(subDistinctAggs);
                    if (isAttrDistinctNested)
                        subDistinctAggs = removeNesting(subDistinctAggs);
                    Cardinality distinctAgg = getDistinctAggregation(subDistinctAggs, aggAttrDistinct);
                    subValue = distinctAgg.getValue();
                } else {
                    subValue = subMissing.getDocCount();
                }
                subCounts.put(subKey, subValue);
            }
            value = subCounts;
        } else {
            if (aggAttrDistinct != null) {
                boolean isAttrDistinctNested = AggregateUtils.isNestedType(aggAttrDistinct);
                Aggregations distinctAggs = missing.getAggregations();
                if (isAttr1Nested)
                    distinctAggs = removeReverseNesting(distinctAggs);
                if (isAttrDistinctNested)
                    distinctAggs = removeNesting(distinctAggs);
                Cardinality distinctAgg = getDistinctAggregation(distinctAggs, aggAttrDistinct);
                value = distinctAgg.getValue();
            } else {
                value = missing.getDocCount();
            }
        }
        counts.put(key, value);
    }
    return counts;
}
Also used : Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) Cardinality(org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality) Aggregations(org.elasticsearch.search.aggregations.Aggregations) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms)

Aggregations

Aggregations (org.elasticsearch.search.aggregations.Aggregations)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)19 Test (org.junit.Test)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)11 ArrayList (java.util.ArrayList)10 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)8 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregations (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations)7 Map (java.util.Map)6 HashMap (java.util.HashMap)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)5 SearchType (org.graylog.plugins.views.search.SearchType)5 Date (java.util.Date)4 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)4 List (java.util.List)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 Aggregation (org.elasticsearch.search.aggregations.Aggregation)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 PivotResult (org.graylog.plugins.views.search.searchtypes.pivot.PivotResult)3