Search in sources :

Example 31 with FilterAggregationBuilder

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

the class StickyFacetBuilder method buildStickyFacet.

/**
 * Creates an aggregation, that will return the top-terms for <code>fieldName</code>.
 *
 * It will filter according to the filters of every of the <em>other</em> fields, but will not apply filters to <em>this</em> field (so that the user can see all terms, even
 * after having chosen for one of the terms).
 *
 * If special filtering is required (like for nested types), additional functionality can be passed into the method in the <code>additionalAggregationFilter</code> parameter.
 *
 * @param fieldName the name of the field that contains the terms
 * @param facetName the name of the aggregation (use this for to find the corresponding results in the response)
 * @param size number of facet items
 * @param additionalAggregationFilter additional features (like filtering using childQuery)
 * @param selected the terms, that the user already has selected
 * @return the (global) aggregation, that can be added on top level of the elasticsearch request
 */
public AggregationBuilder buildStickyFacet(String fieldName, String facetName, int size, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, Object... selected) {
    BoolQueryBuilder facetFilter = getStickyFacetFilter(fieldName);
    FilterAggregationBuilder facetTopAggregation = buildTopFacetAggregation(fieldName, facetName, facetFilter, size, additionalAggregationFilter);
    facetTopAggregation = addSelectedItemsToFacet(fieldName, facetName, facetTopAggregation, additionalAggregationFilter, selected);
    return AggregationBuilders.global(facetName).subAggregation(facetTopAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 32 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder 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 33 with FilterAggregationBuilder

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

the class TopAggregationHelperTest method buildTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation_and_extra_one.

@Test
public void buildTopAggregation_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));
    MinAggregationBuilder subAggregation = AggregationBuilders.min("donut");
    FilterAggregationBuilder aggregationBuilder = underTest.buildTopAggregation(topAggregationName, topAggregation, t -> t.must(extraFilter), t -> t.subAggregation(subAggregation));
    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isEqualTo(computerFilter);
    assertThat(((BoolQueryBuilder) aggregationBuilder.getFilter()).must()).containsExactly(extraFilter);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.MinAggregationBuilder) Test(org.junit.Test)

Example 34 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder 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 35 with FilterAggregationBuilder

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

the class IndicesAdapterES6 method indexRangeStatsOfIndex.

@Override
public IndexRangeStats indexRangeStatsOfIndex(String index) {
    final FilterAggregationBuilder builder = AggregationBuilders.filter("agg", QueryBuilders.existsQuery(Message.FIELD_TIMESTAMP)).subAggregation(AggregationBuilders.min("ts_min").field(Message.FIELD_TIMESTAMP)).subAggregation(AggregationBuilders.max("ts_max").field(Message.FIELD_TIMESTAMP)).subAggregation(AggregationBuilders.terms("streams").size(Integer.MAX_VALUE).field(Message.FIELD_STREAMS));
    final String query = searchSource().aggregation(builder).size(0).toString();
    final Search request = new Search.Builder(query).addIndex(index).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).ignoreUnavailable(true).build();
    if (LOG.isDebugEnabled()) {
        String data = "{}";
        try {
            data = request.getData(objectMapper.copy().enable(SerializationFeature.INDENT_OUTPUT));
        } catch (IOException e) {
            LOG.debug("Couldn't pretty print request payload", e);
        }
        LOG.debug("Index range query: _search/{}: {}", index, data);
    }
    final SearchResult result = JestUtils.execute(jestClient, request, () -> "Couldn't build index range of index " + index);
    final FilterAggregation f = result.getAggregations().getFilterAggregation("agg");
    if (f == null) {
        throw new IndexNotFoundException("Couldn't build index range of index " + index + " because it doesn't exist.");
    } else if (f.getCount() == 0L) {
        LOG.debug("No documents with attribute \"timestamp\" found in index <{}>", index);
        return IndexRangeStats.EMPTY;
    }
    final MinAggregation minAgg = f.getMinAggregation("ts_min");
    final DateTime min = new DateTime(minAgg.getMin().longValue(), DateTimeZone.UTC);
    final MaxAggregation maxAgg = f.getMaxAggregation("ts_max");
    final DateTime max = new DateTime(maxAgg.getMax().longValue(), DateTimeZone.UTC);
    // make sure we return an empty list, so we can differentiate between old indices that don't have this information
    // and newer ones that simply have no streams.
    final TermsAggregation streams = f.getTermsAggregation("streams");
    final List<String> streamIds = streams.getBuckets().stream().map(TermsAggregation.Entry::getKeyAsString).collect(toList());
    return IndexRangeStats.create(min, max, streamIds);
}
Also used : TermsAggregation(io.searchbox.core.search.aggregation.TermsAggregation) FilterAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) FieldSortBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.sort.FieldSortBuilder) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) FilterAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchResult(io.searchbox.core.SearchResult) MinAggregation(io.searchbox.core.search.aggregation.MinAggregation) IOException(java.io.IOException) MaxAggregation(io.searchbox.core.search.aggregation.MaxAggregation) DateTime(org.joda.time.DateTime) Search(io.searchbox.core.Search) IndexNotFoundException(org.graylog2.indexer.IndexNotFoundException) FilterAggregation(io.searchbox.core.search.aggregation.FilterAggregation)

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