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