Search in sources :

Example 21 with Aggregations

use of org.graylog.shaded.elasticsearch7.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 22 with Aggregations

use of org.graylog.shaded.elasticsearch7.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 23 with Aggregations

use of org.graylog.shaded.elasticsearch7.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)

Example 24 with Aggregations

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetAggregation.

@Test
public void testGetAggregation() {
    // Create a mock aggregation.
    Terms aggregation = mock(Terms.class);
    // Create mock aggregations.
    Aggregations aggregations = mock(Aggregations.class);
    when(aggregations.get(AGGREGATION_NAME)).thenReturn(aggregation);
    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    // Call the method under test.
    Terms result = elasticsearchHelper.getAggregation(searchResponse, AGGREGATION_NAME);
    // Verify the external calls.
    verifyNoMoreInteractionsHelper();
    // Validate the result.
    assertEquals(aggregation, result);
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 25 with Aggregations

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetNestedTagTagIndexSearchResponseDtoSearchResponseParameter.

@Test
public void testGetNestedTagTagIndexSearchResponseDtoSearchResponseParameter() {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Aggregations aggregations = mock(Aggregations.class);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    Nested nestedAggregation = mock(Nested.class);
    when(aggregations.get(TAG_FACET_AGGS)).thenReturn(nestedAggregation);
    Aggregations aggregationAggregations = mock(Aggregations.class);
    when(nestedAggregation.getAggregations()).thenReturn(aggregationAggregations);
    Terms subAggregation = mock(Terms.class);
    when(aggregationAggregations.get(TAGTYPE_CODE_AGGREGATION)).thenReturn(subAggregation);
    List<TagTypeIndexSearchResponseDto> result = elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResponse);
    assertThat("Result is null.", result, is(notNullValue()));
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Aggregations

Aggregations (org.elasticsearch.search.aggregations.Aggregations)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)19 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)11 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)11 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)8 Map (java.util.Map)7 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregations (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations)7 SearchType (org.graylog.plugins.views.search.SearchType)6 HashMap (java.util.HashMap)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)5 Date (java.util.Date)4 List (java.util.List)4 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)4 PivotResult (org.graylog.plugins.views.search.searchtypes.pivot.PivotResult)4 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