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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations