Search in sources :

Example 36 with AggregationBuilder

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

the class TopAggregationHelperTest method buildTermTopAggregation_adds_subAggregation_from_lambda_parameter.

@Test
public void buildTermTopAggregation_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);
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    AggregationBuilder[] allSubAggs = Stream.concat(Arrays.stream(subAggs), Stream.of(termSubAgg)).toArray(AggregationBuilder[]::new);
    AggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, NO_EXTRA_FILTER, t -> Arrays.stream(subAggs).forEach(t::subAggregation));
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getSubAggregations()).hasSize(allSubAggs.length);
    assertThat(aggregationBuilder.getSubAggregations()).containsExactlyInAnyOrder(allSubAggs);
}
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) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) 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)

Example 37 with AggregationBuilder

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

the class IssueIndex method addSecurityCategoryFacetIfNeeded.

private static void addSecurityCategoryFacetIfNeeded(String param, Facet facet, SearchOptions options, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest, Object[] selectedValues) {
    if (!options.getFacets().contains(param)) {
        return;
    }
    AggregationBuilder aggregation = aggregationHelper.buildTermTopAggregation(facet.getName(), facet.getTopAggregationDef(), facet.getNumberOfTerms(), filter -> filter.must(termQuery(FIELD_ISSUE_TYPE, VULNERABILITY.name())), t -> aggregationHelper.getSubAggregationHelper().buildSelectedItemsAggregation(facet.getName(), facet.getTopAggregationDef(), selectedValues).ifPresent(t::subAggregation));
    esRequest.aggregation(aggregation);
}
Also used : SumAggregationBuilder(org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder)

Example 38 with AggregationBuilder

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

the class IssueIndex method getCreatedAtFacet.

private Optional<AggregationBuilder> getCreatedAtFacet(IssueQuery query, TopAggregationHelper aggregationHelper, AllFilters allFilters) {
    long startTime;
    boolean startInclusive;
    PeriodStart createdAfter = query.createdAfter();
    if (createdAfter == null) {
        OptionalLong minDate = getMinCreatedAt(allFilters);
        if (!minDate.isPresent()) {
            return Optional.empty();
        }
        startTime = minDate.getAsLong();
        startInclusive = true;
    } else {
        startTime = createdAfter.date().getTime();
        startInclusive = createdAfter.inclusive();
    }
    Date createdBefore = query.createdBefore();
    long endTime = createdBefore == null ? system.now() : createdBefore.getTime();
    Duration timeSpan = new Duration(startTime, endTime);
    DateHistogramInterval bucketSize = computeDateHistogramBucketSize(timeSpan);
    FilterAggregationBuilder topAggregation = aggregationHelper.buildTopAggregation(CREATED_AT.getName(), CREATED_AT.getTopAggregationDef(), NO_EXTRA_FILTER, t -> {
        AggregationBuilder dateHistogram = AggregationBuilders.dateHistogram(CREATED_AT.getName()).field(CREATED_AT.getFieldName()).dateHistogramInterval(bucketSize).minDocCount(0L).format(DateUtils.DATETIME_FORMAT).timeZone(Optional.ofNullable(query.timeZone()).orElse(system.getDefaultTimeZone().toZoneId())).extendedBounds(new LongBounds(startInclusive ? startTime : (startTime + 1), endTime - 1L));
        addEffortAggregationIfNeeded(query, dateHistogram);
        t.subAggregation(dateHistogram);
    });
    return Optional.of(topAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SumAggregationBuilder(org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) PeriodStart(org.sonar.server.issue.index.IssueQuery.PeriodStart) OptionalLong(java.util.OptionalLong) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) Duration(org.joda.time.Duration) Date(java.util.Date) LongBounds(org.elasticsearch.search.aggregations.bucket.histogram.LongBounds)

Example 39 with AggregationBuilder

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

the class IssueIndex method newSecurityReportSubAggregations.

private static AggregationBuilder newSecurityReportSubAggregations(AggregationBuilder categoriesAggs, boolean includeCwe, @Nullable Collection<String> cwesInCategory) {
    AggregationBuilder aggregationBuilder = addSecurityReportIssueCountAggregations(categoriesAggs);
    if (includeCwe) {
        final TermsAggregationBuilder cwesAgg = AggregationBuilders.terms(AGG_CWES).field(FIELD_ISSUE_CWE).size(MAX_FACET_SIZE);
        if (cwesInCategory != null) {
            cwesAgg.includeExclude(new IncludeExclude(cwesInCategory.toArray(new String[0]), new String[0]));
        }
        categoriesAggs.subAggregation(addSecurityReportIssueCountAggregations(cwesAgg));
    }
    return aggregationBuilder;
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SumAggregationBuilder(org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude)

Example 40 with AggregationBuilder

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

the class IssueIndex method addAssignedToMeFacetIfNeeded.

private void addAssignedToMeFacetIfNeeded(SearchOptions options, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest) {
    String uuid = userSession.getUuid();
    if (options.getFacets().contains(ASSIGNED_TO_ME.getName()) && !StringUtils.isEmpty(uuid)) {
        AggregationBuilder aggregation = aggregationHelper.buildTermTopAggregation(ASSIGNED_TO_ME.getName(), ASSIGNED_TO_ME.getTopAggregationDef(), ASSIGNED_TO_ME.getNumberOfTerms(), NO_EXTRA_FILTER, t -> aggregationHelper.getSubAggregationHelper().buildSelectedItemsAggregation(ASSIGNED_TO_ME.getName(), ASSIGNED_TO_ME.getTopAggregationDef(), new String[] { uuid }).ifPresent(t::subAggregation));
        esRequest.aggregation(aggregation);
    }
}
Also used : SumAggregationBuilder(org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder)

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