use of io.searchbox.core.search.aggregation.ExtendedStatsAggregation in project graylog2-server by Graylog2.
the class SearchesAdapterES6Test method worksForNullFieldsInAggregationResults.
@Test
public void worksForNullFieldsInAggregationResults() throws Exception {
final ExtendedStatsAggregation extendedStatsAggregation = mock(ExtendedStatsAggregation.class);
when(extendedStatsAggregation.getCount()).thenReturn(null);
when(extendedStatsAggregation.getSum()).thenReturn(null);
when(extendedStatsAggregation.getSumOfSquares()).thenReturn(null);
when(extendedStatsAggregation.getAvg()).thenReturn(null);
when(extendedStatsAggregation.getMin()).thenReturn(null);
when(extendedStatsAggregation.getMax()).thenReturn(null);
when(extendedStatsAggregation.getVariance()).thenReturn(null);
when(extendedStatsAggregation.getStdDeviation()).thenReturn(null);
final FieldStatsResult result = searchesAdapter.createFieldStatsResult(null, extendedStatsAggregation, null, Collections.emptyList(), null, null, 0);
assertThat(result).isNotNull();
assertThat(result.sum()).isEqualTo(Double.NaN);
assertThat(result.sumOfSquares()).isEqualTo(Double.NaN);
assertThat(result.mean()).isEqualTo(Double.NaN);
assertThat(result.min()).isEqualTo(Double.NaN);
assertThat(result.max()).isEqualTo(Double.NaN);
assertThat(result.variance()).isEqualTo(Double.NaN);
assertThat(result.stdDeviation()).isEqualTo(Double.NaN);
assertThat(result.count()).isEqualTo(Long.MIN_VALUE);
assertThat(result.cardinality()).isEqualTo(Long.MIN_VALUE);
}
use of io.searchbox.core.search.aggregation.ExtendedStatsAggregation in project graylog2-server by Graylog2.
the class SearchesAdapterES6 method fieldStats.
@Override
public FieldStatsResult fieldStats(String query, String filter, TimeRange range, Set<String> indices, String field, boolean includeCardinality, boolean includeStats, boolean includeCount) {
final SearchSourceBuilder searchSourceBuilder;
if (filter == null) {
searchSourceBuilder = standardSearchRequest(query, range);
} else {
searchSourceBuilder = filteredSearchRequest(query, filter, range);
}
final FilterAggregationBuilder filterBuilder = AggregationBuilders.filter(AGG_FILTER, standardAggregationFilters(range, filter));
if (includeCount) {
searchSourceBuilder.aggregation(AggregationBuilders.count(AGG_VALUE_COUNT).field(field));
}
if (includeStats) {
searchSourceBuilder.aggregation(AggregationBuilders.extendedStats(AGG_EXTENDED_STATS).field(field));
}
if (includeCardinality) {
searchSourceBuilder.aggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));
}
searchSourceBuilder.aggregation(filterBuilder);
if (indices.isEmpty()) {
return FieldStatsResult.empty(query, searchSourceBuilder.toString());
}
final Search searchRequest = new Search.Builder(searchSourceBuilder.toString()).addType(IndexMapping.TYPE_MESSAGE).addIndex(indices).build();
final io.searchbox.core.SearchResult searchResponse = multiSearch.wrap(searchRequest, () -> "Unable to retrieve fields stats");
final List<ResultMessage> hits = searchResponse.getHits(Map.class, false).stream().map(hit -> ResultMessage.parseFromSource(hit.id, hit.index, (Map<String, Object>) hit.source)).collect(Collectors.toList());
final ExtendedStatsAggregation extendedStatsAggregation = searchResponse.getAggregations().getExtendedStatsAggregation(AGG_EXTENDED_STATS);
final ValueCountAggregation valueCountAggregation = searchResponse.getAggregations().getValueCountAggregation(AGG_VALUE_COUNT);
final CardinalityAggregation cardinalityAggregation = searchResponse.getAggregations().getCardinalityAggregation(AGG_CARDINALITY);
return createFieldStatsResult(valueCountAggregation, extendedStatsAggregation, cardinalityAggregation, hits, query, searchSourceBuilder.toString(), multiSearch.tookMsFromSearchResult(searchResponse));
}
Aggregations