Search in sources :

Example 51 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class StickyFacetBuilder method addSelectedItemsToFacet.

public FilterAggregationBuilder addSelectedItemsToFacet(String fieldName, String facetName, FilterAggregationBuilder facetTopAggregation, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, Object... selected) {
    if (selected.length <= 0) {
        return facetTopAggregation;
    }
    String includes = Arrays.stream(selected).filter(Objects::nonNull).map(s -> EsUtils.escapeSpecialRegexChars(s.toString())).collect(PIPE_JOINER);
    TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected").size(max(MAXIMUM_NUMBER_OF_SELECTED_ITEMS_WHOSE_DOC_COUNT_WILL_BE_CALCULATED, includes.length())).field(fieldName).includeExclude(new IncludeExclude(includes, null));
    if (subAggregation != null) {
        selectedTerms = selectedTerms.subAggregation(subAggregation);
    }
    AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(selectedTerms);
    facetTopAggregation.subAggregation(improvedAggregation);
    return facetTopAggregation;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Arrays(java.util.Arrays) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Map(java.util.Map) Math.max(java.lang.Math.max) Collector(java.util.stream.Collector) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Nullable(javax.annotation.Nullable) ArrayUtils(org.apache.commons.lang.ArrayUtils) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) Objects(java.util.Objects)

Example 52 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder 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 53 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class SubAggregationHelperTest method buildTermsAggregation_adds_term_subaggregation_with_minDoc_1_and_default_sort.

@Test
public void buildTermsAggregation_adds_term_subaggregation_with_minDoc_1_and_default_sort() {
    String aggName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    Stream.of(underTest, underTestWithCustomSubAgg).forEach(t -> {
        TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, null);
        assertThat(agg.getName()).isEqualTo(aggName);
        assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
        assertThat(agg.size()).isEqualTo(DEFAULT_BUCKET_SIZE);
        assertThat(agg.minDocCount()).isOne();
        assertThat(agg.order()).isEqualTo(BucketOrder.compound(SQ_DEFAULT_BUCKET_ORDER, ES_BUILTIN_TIE_BREAKER));
    });
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Test(org.junit.Test)

Example 54 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class SubAggregationHelperTest method buildSelectedItemsAggregation_adds_custom_sub_agg_from_constructor.

@Test
public void buildSelectedItemsAggregation_adds_custom_sub_agg_from_constructor() {
    String aggName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    String[] selected = randomNonEmptySelected();
    Stream.of(underTestWithCustomSubAgg, underTestWithCustomsSubAggAndOrder).forEach(t -> {
        TermsAggregationBuilder agg = t.buildSelectedItemsAggregation(aggName, topAggregation, selected).get();
        assertThat(agg.getName()).isEqualTo(aggName + "_selected");
        assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
        assertThat(agg.getSubAggregations()).hasSize(1);
        assertThat(agg.getSubAggregations().iterator().next()).isSameAs(customSubAgg);
    });
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Test(org.junit.Test)

Example 55 with TermsAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder in project sonarqube by SonarSource.

the class IssueIndex method listTermsMatching.

private Terms listTermsMatching(String fieldName, IssueQuery query, @Nullable String textQuery, BucketOrder termsOrder, int size) {
    SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType());
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(0);
    requestBuilder.source(sourceBuilder);
    sourceBuilder.query(boolQuery().must(QueryBuilders.matchAllQuery()).filter(createBoolFilter(query)));
    TermsAggregationBuilder aggreg = AggregationBuilders.terms("_ref").field(fieldName).size(size).order(termsOrder).minDocCount(1L);
    if (textQuery != null) {
        aggreg.includeExclude(new IncludeExclude(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)), null));
    }
    sourceBuilder.aggregation(aggreg);
    SearchResponse searchResponse = client.search(requestBuilder);
    return searchResponse.getAggregations().get("_ref");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)64 SignificantTermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregationBuilder)20 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)14 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)13 Test (org.junit.Test)11 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)8 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)8 LinkedList (java.util.LinkedList)7 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)6 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude)6 Arrays (java.util.Arrays)4 Map (java.util.Map)4 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)4 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)4 AggregationBuilders (org.elasticsearch.search.aggregations.AggregationBuilders)4 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)4 Nullable (javax.annotation.Nullable)3 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)3