use of org.elasticsearch.search.aggregations.metrics.Min in project graylog2-server by Graylog2.
the class ESPivot method extractEffectiveTimeRange.
private AbsoluteRange extractEffectiveTimeRange(SearchResponse queryResult, Query query, Pivot pivot) {
final Min min = queryResult.getAggregations().get("timestamp-min");
final Double from = min.getValue();
final Max max = queryResult.getAggregations().get("timestamp-max");
final Double to = max.getValue();
final TimeRange pivotRange = query.effectiveTimeRange(pivot);
return AbsoluteRange.create(isAllMessagesTimeRange(pivotRange) && from != 0 ? new DateTime(from.longValue(), DateTimeZone.UTC) : query.effectiveTimeRange(pivot).getFrom(), isAllMessagesTimeRange(pivotRange) && to != 0 ? new DateTime(to.longValue(), DateTimeZone.UTC) : query.effectiveTimeRange(pivot).getTo());
}
use of org.elasticsearch.search.aggregations.metrics.Min in project sonarqube by SonarSource.
the class IssueIndex method getMinCreatedAt.
private OptionalLong getMinCreatedAt(AllFilters filters) {
String facetNameAndField = CREATED_AT.getFieldName();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(0);
BoolQueryBuilder esFilter = boolQuery();
filters.stream().filter(Objects::nonNull).forEach(esFilter::must);
if (esFilter.hasClauses()) {
sourceBuilder.query(QueryBuilders.boolQuery().filter(esFilter));
}
sourceBuilder.aggregation(AggregationBuilders.min(facetNameAndField).field(facetNameAndField));
SearchRequest request = EsClient.prepareSearch(TYPE_ISSUE.getMainType()).source(sourceBuilder);
Min minValue = client.search(request).getAggregations().get(facetNameAndField);
double actualValue = minValue.getValue();
if (Double.isInfinite(actualValue)) {
return OptionalLong.empty();
}
return OptionalLong.of((long) actualValue);
}
use of org.elasticsearch.search.aggregations.metrics.Min in project graylog2-server by Graylog2.
the class IndicesAdapterES7 method indexRangeStatsOfIndex.
@Override
public IndexRangeStats indexRangeStatsOfIndex(String index) {
final FilterAggregationBuilder builder = AggregationBuilders.filter("agg", QueryBuilders.existsQuery(Message.FIELD_TIMESTAMP)).subAggregation(AggregationBuilders.min("ts_min").field(Message.FIELD_TIMESTAMP)).subAggregation(AggregationBuilders.max("ts_max").field(Message.FIELD_TIMESTAMP)).subAggregation(AggregationBuilders.terms("streams").size(Integer.MAX_VALUE).field(Message.FIELD_STREAMS));
final SearchSourceBuilder query = SearchSourceBuilder.searchSource().aggregation(builder).size(0);
final SearchRequest request = new SearchRequest().source(query).indices(index).searchType(SearchType.DFS_QUERY_THEN_FETCH).indicesOptions(IndicesOptions.lenientExpandOpen());
final SearchResponse result = client.execute((c, requestOptions) -> c.search(request, requestOptions), "Couldn't build index range of index " + index);
if (result.getTotalShards() == 0 || result.getAggregations() == null) {
throw new IndexNotFoundException("Couldn't build index range of index " + index + " because it doesn't exist.");
}
final Filter f = result.getAggregations().get("agg");
if (f == null) {
throw new IndexNotFoundException("Couldn't build index range of index " + index + " because it doesn't exist.");
} else if (f.getDocCount() == 0L) {
LOG.debug("No documents with attribute \"timestamp\" found in index <{}>", index);
return IndexRangeStats.EMPTY;
}
final Min minAgg = f.getAggregations().get("ts_min");
final long minUnixTime = new Double(minAgg.getValue()).longValue();
final DateTime min = new DateTime(minUnixTime, DateTimeZone.UTC);
final Max maxAgg = f.getAggregations().get("ts_max");
final long maxUnixTime = new Double(maxAgg.getValue()).longValue();
final DateTime max = new DateTime(maxUnixTime, DateTimeZone.UTC);
// make sure we return an empty list, so we can differentiate between old indices that don't have this information
// and newer ones that simply have no streams.
final Terms streams = f.getAggregations().get("streams");
final List<String> streamIds = streams.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).collect(toList());
return IndexRangeStats.create(min, max, streamIds);
}
use of org.elasticsearch.search.aggregations.metrics.Min in project graylog2-server by Graylog2.
the class ESPivotTest method createTimestampRangeAggregations.
private Aggregations createTimestampRangeAggregations(Double min, Double max) {
final Min timestampMinAggregation = mock(Min.class);
when(timestampMinAggregation.getValue()).thenReturn(min);
when(timestampMinAggregation.getName()).thenReturn("timestamp-min");
final Max timestampMaxAggregation = mock(Max.class);
when(timestampMaxAggregation.getValue()).thenReturn(max);
when(timestampMaxAggregation.getName()).thenReturn("timestamp-max");
return new Aggregations(ImmutableList.of(timestampMinAggregation, timestampMaxAggregation));
}
Aggregations