Search in sources :

Example 21 with FilterAggregationBuilder

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

the class Searches method terms.

public TermsResult terms(String field, int size, String query, String filter, TimeRange range, Sorting.Direction sorting) {
    Terms.Order termsOrder;
    if (size == 0) {
        size = 50;
    }
    if (sorting == Sorting.Direction.DESC) {
        termsOrder = Terms.Order.count(false);
    } else {
        termsOrder = Terms.Order.count(true);
    }
    SearchRequestBuilder srb;
    if (filter == null) {
        srb = standardSearchRequest(query, determineAffectedIndices(range, null), range);
    } else {
        srb = filteredSearchRequest(query, filter, determineAffectedIndices(range, filter), range);
    }
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.terms(AGG_TERMS).field(field).size(size).order(termsOrder)).subAggregation(AggregationBuilders.missing("missing").field(field)).filter(standardAggregationFilters(range, filter));
    srb.addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new TermsResult(f.getAggregations().get(AGG_TERMS), f.getAggregations().get("missing"), f.getDocCount(), query, request.source(), r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TermsResult(org.graylog2.indexer.results.TermsResult) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with FilterAggregationBuilder

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

the class Searches method histogram.

public HistogramResult histogram(String query, DateHistogramInterval interval, String filter, TimeRange range) {
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.dateHistogram(AGG_HISTOGRAM).field("timestamp").interval(interval.toESInterval())).filter(standardAggregationFilters(range, filter));
    QueryStringQueryBuilder qs = queryStringQuery(query);
    qs.allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
    final Set<String> affectedIndices = determineAffectedIndices(range, filter);
    final SearchRequestBuilder srb = c.prepareSearch(affectedIndices.toArray(new String[affectedIndices.size()])).setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(qs).addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new DateHistogramResult(f.getAggregations().get(AGG_HISTOGRAM), query, request.source(), interval, r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) DateHistogramResult(org.graylog2.indexer.results.DateHistogramResult) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 23 with FilterAggregationBuilder

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

the class StickyFacetBuilder method buildStickyFacet.

public AggregationBuilder buildStickyFacet(String fieldName, String facetName, int size, Object... selected) {
    BoolQueryBuilder facetFilter = getStickyFacetFilter(fieldName);
    FilterAggregationBuilder facetTopAggregation = buildTopFacetAggregation(fieldName, facetName, facetFilter, size);
    facetTopAggregation = addSelectedItemsToFacet(fieldName, facetName, facetTopAggregation, selected);
    return AggregationBuilders.global(facetName).subAggregation(facetTopAggregation);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 24 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch7.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, 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);
    TermsBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected").field(fieldName).include(includes);
    if (subAggregation != null) {
        selectedTerms = selectedTerms.subAggregation(subAggregation);
    }
    facetTopAggregation.subAggregation(selectedTerms);
    return facetTopAggregation;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Arrays(java.util.Arrays) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Collectors(java.util.stream.Collectors) Order(org.elasticsearch.search.aggregations.bucket.terms.Terms.Order) Objects(java.util.Objects) Map(java.util.Map) Collector(java.util.stream.Collector) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Nullable(javax.annotation.Nullable) ArrayUtils(org.apache.commons.lang.ArrayUtils) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) Objects(java.util.Objects)

Example 25 with FilterAggregationBuilder

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

the class IssueIndex method addAssignedToMeFacetIfNeeded.

private void addAssignedToMeFacetIfNeeded(SearchRequestBuilder builder, SearchOptions options, IssueQuery query, Map<String, QueryBuilder> filters, QueryBuilder queryBuilder) {
    String login = userSession.getLogin();
    if (!options.getFacets().contains(FACET_ASSIGNED_TO_ME) || StringUtils.isEmpty(login)) {
        return;
    }
    String fieldName = IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE;
    String facetName = FACET_ASSIGNED_TO_ME;
    // Same as in super.stickyFacetBuilder
    StickyFacetBuilder assignedToMeFacetBuilder = newStickyFacetBuilder(query, filters, queryBuilder);
    BoolQueryBuilder facetFilter = assignedToMeFacetBuilder.getStickyFacetFilter(IS_ASSIGNED_FILTER, fieldName);
    FilterAggregationBuilder facetTopAggregation = AggregationBuilders.filter(facetName + "__filter").filter(facetFilter).subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.terms(facetName + "__terms").field(fieldName).include(escapeSpecialRegexChars(login))));
    builder.addAggregation(AggregationBuilders.global(facetName).subAggregation(facetTopAggregation));
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) StickyFacetBuilder(org.sonar.server.es.StickyFacetBuilder)

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