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;
}
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());
}
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;
}
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);
}
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()));
}
Aggregations