Search in sources :

Example 21 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project graylog2-server by Graylog2.

the class ESMaxHandler method doCreateAggregation.

@Nonnull
@Override
public Optional<AggregationBuilder> doCreateAggregation(String name, Pivot pivot, Max maxSpec, ESPivot searchTypeHandler, ESGeneratedQueryContext queryContext) {
    final MaxAggregationBuilder max = AggregationBuilders.max(name).field(maxSpec.field());
    record(queryContext, pivot, maxSpec, name, MaxAggregation.class);
    return Optional.of(max);
}
Also used : MaxAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder) Nonnull(javax.annotation.Nonnull)

Example 22 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project graylog2-server by Graylog2.

the class ESDateRangeHandler method doCreateAggregation.

@Nonnull
@Override
public Optional<AggregationBuilder> doCreateAggregation(String name, Pivot pivot, DateRangeBucket dateRangeBucket, ESPivot searchTypeHandler, ESGeneratedQueryContext esGeneratedQueryContext, Query query) {
    final DateRangeAggregationBuilder builder = AggregationBuilders.dateRange(name).field(dateRangeBucket.field());
    dateRangeBucket.ranges().forEach(r -> {
        final String from = r.from().map(AbstractDateTime::toString).orElse(null);
        final String to = r.to().map(AbstractDateTime::toString).orElse(null);
        if (from != null && to != null) {
            builder.addRange(from, to);
        } else if (to != null) {
            builder.addUnboundedTo(to);
        } else if (from != null) {
            builder.addUnboundedFrom(from);
        }
    });
    builder.format("date_time");
    builder.keyed(false);
    record(esGeneratedQueryContext, pivot, dateRangeBucket, name, DateRangeAggregation.class);
    return Optional.of(builder);
}
Also used : DateRangeAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.range.DateRangeAggregationBuilder) Nonnull(javax.annotation.Nonnull)

Example 23 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.

the class RuleIndex method search.

public SearchIdResult<String> search(RuleQuery query, SearchOptions options) {
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    QueryBuilder qb = buildQuery(query);
    Map<String, QueryBuilder> filters = buildFilters(query);
    if (!options.getFacets().isEmpty()) {
        for (AggregationBuilder aggregation : getFacets(query, options, qb, filters).values()) {
            sourceBuilder.aggregation(aggregation);
        }
    }
    setSorting(query, sourceBuilder);
    setPagination(options, sourceBuilder);
    BoolQueryBuilder fb = boolQuery();
    for (QueryBuilder filterBuilder : filters.values()) {
        fb.must(filterBuilder);
    }
    sourceBuilder.query(boolQuery().must(qb).filter(fb));
    SearchRequest esSearch = EsClient.prepareSearch(TYPE_RULE).source(sourceBuilder);
    return new SearchIdResult<>(client.search(esSearch), input -> input, system2.getDefaultTimeZone().toZoneId());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.join.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchIdResult(org.sonar.server.es.SearchIdResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 24 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.

the class RuleIndex method getFacets.

private static Map<String, AggregationBuilder> getFacets(RuleQuery query, SearchOptions options, QueryBuilder queryBuilder, Map<String, QueryBuilder> filters) {
    Map<String, AggregationBuilder> aggregations = new HashMap<>();
    StickyFacetBuilder stickyFacetBuilder = stickyFacetBuilder(queryBuilder, filters);
    addDefaultFacets(query, options, aggregations, stickyFacetBuilder);
    addStatusFacetIfNeeded(options, aggregations, stickyFacetBuilder);
    if (options.getFacets().contains(FACET_SEVERITIES)) {
        aggregations.put(FACET_SEVERITIES, stickyFacetBuilder.buildStickyFacet(FIELD_RULE_SEVERITY, FACET_SEVERITIES, Severity.ALL.toArray()));
    }
    addActiveSeverityFacetIfNeeded(query, options, aggregations, stickyFacetBuilder);
    return aggregations;
}
Also used : AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) HashMap(java.util.HashMap) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder)

Example 25 with AggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTopAggregation_adds_subAggregation_from_lambda_parameter.

@Test
public void buildTopAggregation_adds_subAggregation_from_lambda_parameter() {
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    AggregationBuilder[] subAggs = IntStream.range(0, 1 + new Random().nextInt(12)).mapToObj(i -> AggregationBuilders.min("subAgg_" + i)).toArray(AggregationBuilder[]::new);
    String topAggregationName = randomAlphabetic(10);
    AggregationBuilder aggregationBuilder = underTest.buildTopAggregation(topAggregationName, topAggregation, NO_EXTRA_FILTER, t -> Arrays.stream(subAggs).forEach(t::subAggregation));
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getSubAggregations()).hasSize(subAggs.length);
    assertThat(aggregationBuilder.getSubAggregations()).containsExactlyInAnyOrder(subAggs);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) NO_OTHER_SUBAGGREGATION(org.sonar.server.es.searchrequest.TopAggregationHelper.NO_OTHER_SUBAGGREGATION) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) Stream(java.util.stream.Stream) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) NO_EXTRA_FILTER(org.sonar.server.es.searchrequest.TopAggregationHelper.NO_EXTRA_FILTER) Optional(java.util.Optional) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Mockito.mock(org.mockito.Mockito.mock) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Random(java.util.Random) Test(org.junit.Test)

Aggregations

AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)39 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)28 Nonnull (javax.annotation.Nonnull)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)10 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)10 SumAggregationBuilder (org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder)8 CardinalityAggregationBuilder (org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder)8 ArrayList (java.util.ArrayList)7 RangeAggregationBuilder (org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder)7 DateRangeAggregationBuilder (org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder)7 Map (java.util.Map)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 GeoGridAggregationBuilder (org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder)6 DateHistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)6 HistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder)6 PercentilesAggregationBuilder (org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder)6 ExtendedStatsAggregationBuilder (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder)6 BucketSelectorPipelineAggregationBuilder (org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)6 HashMap (java.util.HashMap)5