Search in sources :

Example 11 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTermTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation_and_extra_one.

@Test
public void buildTermTopAggregation_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));
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, t -> t.must(extraFilter), NO_OTHER_SUBAGGREGATION);
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isEqualTo(computerFilter);
    assertThat(((BoolQueryBuilder) aggregationBuilder.getFilter()).must()).containsExactly(extraFilter);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Example 12 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTermTopAggregation_adds_term_subaggregation_from_subAggregationHelper.

@Test
public void buildTermTopAggregation_adds_term_subaggregation_from_subAggregationHelper() {
    String topAggregationName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, NO_EXTRA_FILTER, NO_OTHER_SUBAGGREGATION);
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getSubAggregations()).hasSize(1);
    assertThat(aggregationBuilder.getSubAggregations().iterator().next()).isSameAs(termSubAgg);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Test(org.junit.Test)

Example 13 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelperTest method buildTermTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation.

@Test
public void buildTermTopAggregation_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));
    String topAggregationName = randomAlphabetic(10);
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, NO_EXTRA_FILTER, NO_OTHER_SUBAGGREGATION);
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isSameAs(computerFilter);
}
Also used : TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Example 14 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project sonarqube by SonarSource.

the class IssueIndex method addFacetIfNeeded.

private static void addFacetIfNeeded(SearchOptions options, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest, Facet facet, Object[] selectedValues) {
    if (!options.getFacets().contains(facet.getName())) {
        return;
    }
    FilterAggregationBuilder topAggregation = aggregationHelper.buildTermTopAggregation(facet.getName(), facet.getTopAggregationDef(), facet.getNumberOfTerms(), NO_EXTRA_FILTER, t -> aggregationHelper.getSubAggregationHelper().buildSelectedItemsAggregation(facet.getName(), facet.getTopAggregationDef(), selectedValues).ifPresent(t::subAggregation));
    esRequest.aggregation(topAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)

Example 15 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project sonarqube by SonarSource.

the class TopAggregationHelper method buildTopAggregation.

/**
 * Creates a top-level aggregation that will be correctly scoped (ie. filtered) to aggregate on
 * {@code TopAggregationDefinition#getFieldName} given the Request filters and the other top-aggregations
 * (see {@link RequestFiltersComputer#getTopAggregationFilter(TopAggregationDefinition)}).
 * <p>
 * Optionally, the scope (ie. filter) of the aggregation can be further reduced by providing {@code extraFilters}.
 * <p>
 * Aggregations <strong>must</strong> be added to the top-level one by providing {@code subAggregations} otherwise
 * the aggregation will be empty and will yield no result.
 *
 * @param topAggregationName the name of the top-aggregation in the request
 * @param topAggregation properties of the top-aggregation
 * @param extraFilters optional extra filters which could further restrict the scope of computation of the
 *                     top-terms aggregation
 * @param subAggregations sub aggregation(s) to actually compute something
 *
 * @throws IllegalStateException if no sub-aggregation has been added
 * @return the aggregation, that can be added on top level of the elasticsearch request
 */
public FilterAggregationBuilder buildTopAggregation(String topAggregationName, TopAggregationDefinition<?> topAggregation, Consumer<BoolQueryBuilder> extraFilters, Consumer<FilterAggregationBuilder> subAggregations) {
    BoolQueryBuilder filter = filterComputer.getTopAggregationFilter(topAggregation).orElseGet(QueryBuilders::boolQuery);
    // optionally add extra filter(s)
    extraFilters.accept(filter);
    FilterAggregationBuilder res = AggregationBuilders.filter(topAggregationName, filter);
    subAggregations.accept(res);
    checkState(!res.getSubAggregations().isEmpty(), "no sub-aggregation has been added to top-aggregation %s", topAggregationName);
    return res;
}
Also used : QueryBuilders(org.elasticsearch.index.query.QueryBuilders) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Aggregations

FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)33 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)15 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)8 Map (java.util.Map)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 Test (org.junit.Test)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)5 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)5 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)5 DateTime (org.joda.time.DateTime)5 Search (io.searchbox.core.Search)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Objects (java.util.Objects)3 Collectors (java.util.stream.Collectors)3 Nullable (javax.annotation.Nullable)3 ImmutableMap (com.google.common.collect.ImmutableMap)2