Search in sources :

Example 1 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTopAggregation_does_not_add_subaggregation_from_subAggregationHelper.

@Test
public void buildTopAggregation_does_not_add_subaggregation_from_subAggregationHelper() {
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.empty());
    MinAggregationBuilder subAggregation = AggregationBuilders.min("donut");
    String topAggregationName = randomAlphabetic(10);
    underTest.buildTopAggregation(topAggregationName, topAggregation, NO_EXTRA_FILTER, t -> t.subAggregation(subAggregation));
    verifyZeroInteractions(subAggregationHelper);
}
Also used : MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) Test(org.junit.Test)

Example 2 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTopAggregation_has_empty_filter_when_FiltersComputer_returns_empty_for_TopAggregation.

@Test
public void buildTopAggregation_has_empty_filter_when_FiltersComputer_returns_empty_for_TopAggregation() {
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    SimpleFieldTopAggregationDefinition otherTopAggregation = new SimpleFieldTopAggregationDefinition("acme", false);
    BoolQueryBuilder otherFilter = boolQuery();
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.empty());
    when(filtersComputer.getTopAggregationFilter(otherTopAggregation)).thenReturn(Optional.of(otherFilter));
    MinAggregationBuilder subAggregation = AggregationBuilders.min("donut");
    String topAggregationName = randomAlphabetic(10);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTopAggregation(topAggregationName, topAggregation, NO_EXTRA_FILTER, t -> t.subAggregation(subAggregation));
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isEqualTo(boolQuery()).isNotSameAs(otherFilter);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) Test(org.junit.Test)

Example 3 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation.

@Test
public void buildTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation() {
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    SimpleFieldTopAggregationDefinition otherTopAggregation = new SimpleFieldTopAggregationDefinition("acme", false);
    BoolQueryBuilder computerFilter = boolQuery();
    BoolQueryBuilder otherFilter = boolQuery();
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.of(computerFilter));
    when(filtersComputer.getTopAggregationFilter(otherTopAggregation)).thenReturn(Optional.of(otherFilter));
    MinAggregationBuilder subAggregation = AggregationBuilders.min("donut");
    String topAggregationName = randomAlphabetic(10);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTopAggregation(topAggregationName, topAggregation, NO_EXTRA_FILTER, t -> t.subAggregation(subAggregation));
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isSameAs(computerFilter);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) Test(org.junit.Test)

Example 4 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation_and_extra_one.

@Test
public void buildTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation_and_extra_one() {
    String topAggregationName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    SimpleFieldTopAggregationDefinition otherTopAggregation = new SimpleFieldTopAggregationDefinition("acme", false);
    BoolQueryBuilder computerFilter = boolQuery();
    BoolQueryBuilder otherFilter = boolQuery();
    BoolQueryBuilder extraFilter = boolQuery();
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.of(computerFilter));
    when(filtersComputer.getTopAggregationFilter(otherTopAggregation)).thenReturn(Optional.of(otherFilter));
    MinAggregationBuilder subAggregation = AggregationBuilders.min("donut");
    FilterAggregationBuilder aggregationBuilder = underTest.buildTopAggregation(topAggregationName, topAggregation, t -> t.must(extraFilter), t -> t.subAggregation(subAggregation));
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isEqualTo(computerFilter);
    assertThat(((BoolQueryBuilder) aggregationBuilder.getFilter()).must()).containsExactly(extraFilter);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) Test(org.junit.Test)

Example 5 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder in project graylog2-server by Graylog2.

the class ESPivot method doGenerateQueryPart.

@Override
public void doGenerateQueryPart(SearchJob job, Query query, Pivot pivot, ESGeneratedQueryContext queryContext) {
    LOG.debug("Generating aggregation for {}", pivot);
    final SearchSourceBuilder searchSourceBuilder = queryContext.searchSourceBuilder(pivot);
    final Map<Object, Object> contextMap = queryContext.contextMap();
    final AggTypes aggTypes = new AggTypes();
    contextMap.put(pivot.id(), aggTypes);
    // holds the initial level aggregation to be added to the query
    AggregationBuilder topLevelAggregation = null;
    // holds the last complete bucket aggregation into which subsequent buckets get added
    AggregationBuilder previousAggregation = null;
    // add global rollup series if those were requested
    if (pivot.rollup()) {
        seriesStream(pivot, queryContext, "global rollup").forEach(searchSourceBuilder::aggregation);
    }
    final Iterator<BucketSpec> rowBuckets = pivot.rowGroups().iterator();
    while (rowBuckets.hasNext()) {
        final BucketSpec bucketSpec = rowBuckets.next();
        final String name = queryContext.nextName();
        LOG.debug("Creating row group aggregation '{}' as {}", bucketSpec.type(), name);
        final ESPivotBucketSpecHandler<? extends PivotSpec, ? extends Aggregation> handler = bucketHandlers.get(bucketSpec.type());
        if (handler == null) {
            throw new IllegalArgumentException("Unknown row_group type " + bucketSpec.type());
        }
        final Optional<AggregationBuilder> generatedAggregation = handler.createAggregation(name, pivot, bucketSpec, this, queryContext, query);
        if (generatedAggregation.isPresent()) {
            final AggregationBuilder aggregationBuilder = generatedAggregation.get();
            if (topLevelAggregation == null) {
                topLevelAggregation = aggregationBuilder;
            }
            // always insert the series for the final row group, or for each one if explicit rollup was requested
            if (!rowBuckets.hasNext() || pivot.rollup()) {
                seriesStream(pivot, queryContext, !rowBuckets.hasNext() ? "leaf row" : "row rollup").forEach(aggregationBuilder::subAggregation);
            }
            if (previousAggregation != null) {
                previousAggregation.subAggregation(aggregationBuilder);
            } else {
                searchSourceBuilder.aggregation(aggregationBuilder);
            }
            previousAggregation = aggregationBuilder;
        }
    }
    final Iterator<BucketSpec> colBuckets = pivot.columnGroups().iterator();
    while (colBuckets.hasNext()) {
        final BucketSpec bucketSpec = colBuckets.next();
        final String name = queryContext.nextName();
        LOG.debug("Creating column group aggregation '{}' as {}", bucketSpec.type(), name);
        final ESPivotBucketSpecHandler<? extends PivotSpec, ? extends Aggregation> handler = bucketHandlers.get(bucketSpec.type());
        if (handler == null) {
            throw new IllegalArgumentException("Unknown column_group type " + bucketSpec.type());
        }
        final Optional<AggregationBuilder> generatedAggregation = handler.createAggregation(name, pivot, bucketSpec, this, queryContext, query);
        if (generatedAggregation.isPresent()) {
            final AggregationBuilder aggregationBuilder = generatedAggregation.get();
            // always insert the series for the final row group, or for each one if explicit rollup was requested
            if (!colBuckets.hasNext() || pivot.rollup()) {
                seriesStream(pivot, queryContext, !colBuckets.hasNext() ? "leaf column" : "column rollup").forEach(aggregationBuilder::subAggregation);
            }
            if (previousAggregation != null) {
                previousAggregation.subAggregation(aggregationBuilder);
            } else {
                searchSourceBuilder.aggregation(aggregationBuilder);
            }
            previousAggregation = aggregationBuilder;
        }
    }
    final MinAggregationBuilder startTimestamp = AggregationBuilders.min("timestamp-min").field("timestamp");
    final MaxAggregationBuilder endTimestamp = AggregationBuilders.max("timestamp-max").field("timestamp");
    searchSourceBuilder.aggregation(startTimestamp);
    searchSourceBuilder.aggregation(endTimestamp);
    if (topLevelAggregation == null) {
        LOG.debug("No aggregations generated for {}", pivot);
    }
}
Also used : AggregationBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilder) MaxAggregationBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder) MinAggregationBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) BucketSpec(org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) MaxAggregationBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder) MinAggregationBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder)

Aggregations

MinAggregationBuilder (org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder)4 Test (org.junit.Test)4 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)3 MinAggregationBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder)2 Nonnull (javax.annotation.Nonnull)1 BucketSpec (org.graylog.plugins.views.search.searchtypes.pivot.BucketSpec)1 AggregationBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilder)1 MaxAggregationBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder)1 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)1