Search in sources :

Example 1 with AbstractAggregationBuilder

use of org.elasticsearch.search.aggregations.AbstractAggregationBuilder in project sonarqube by SonarSource.

the class RuleIndex method addActiveSeverityFacetIfNeeded.

private static void addActiveSeverityFacetIfNeeded(RuleQuery query, SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
    if (options.getFacets().contains(FACET_ACTIVE_SEVERITIES)) {
        // We are building a children aggregation on active rules
        // so the rule filter has to be used as parent filter for active rules
        // from which we remove filters that concern active rules ("activation")
        HasParentQueryBuilder ruleFilter = QueryBuilders.hasParentQuery(INDEX_TYPE_RULE.getType(), stickyFacetBuilder.getStickyFacetFilter("activation"));
        // Rebuilding the active rule filter without severities
        BoolQueryBuilder childrenFilter = boolQuery();
        addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
        RuleIndex.addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
        QueryBuilder activeRuleFilter;
        if (childrenFilter.hasClauses()) {
            activeRuleFilter = childrenFilter.must(ruleFilter);
        } else {
            activeRuleFilter = ruleFilter;
        }
        AbstractAggregationBuilder activeSeverities = AggregationBuilders.children(FACET_ACTIVE_SEVERITIES + "_children").childType(INDEX_TYPE_ACTIVE_RULE.getType()).subAggregation(AggregationBuilders.filter(FACET_ACTIVE_SEVERITIES + "_filter").filter(activeRuleFilter).subAggregation(AggregationBuilders.terms(FACET_ACTIVE_SEVERITIES).field(FIELD_ACTIVE_RULE_SEVERITY).include(Joiner.on('|').join(Severity.ALL)).size(Severity.ALL.size())));
        aggregations.put(FACET_ACTIVE_SEVERITIES, AggregationBuilders.global(FACET_ACTIVE_SEVERITIES).subAggregation(activeSeverities));
    }
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 2 with AbstractAggregationBuilder

use of org.elasticsearch.search.aggregations.AbstractAggregationBuilder in project zipkin by openzipkin.

the class HttpClient method collectBucketKeys.

@Override
protected ListenableFuture<List<String>> collectBucketKeys(String[] indices, QueryBuilder query, AbstractAggregationBuilder... aggregations) {
    SearchSourceBuilder elasticQuery = new SearchSourceBuilder().query(query).size(0);
    for (AbstractAggregationBuilder aggregation : aggregations) {
        elasticQuery.aggregation(aggregation);
    }
    HttpCall<List<String>> searchRequest = http.newCall(new Request.Builder().url(lenientSearch(indices, SPAN)).post(RequestBody.create(APPLICATION_JSON, elasticQuery.toString())).tag("search-spansAggregations").build(), BodyConverters.SORTED_KEYS);
    CallbackListenableFuture<List<String>> result = new CallbackListenableFuture<>();
    searchRequest.submit(result);
    return result;
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) Request(okhttp3.Request) List(java.util.List) Arrays.asList(java.util.Arrays.asList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 3 with AbstractAggregationBuilder

use of org.elasticsearch.search.aggregations.AbstractAggregationBuilder in project vertexium by visallo.

the class ElasticsearchSearchQueryBase method getElasticsearchStatisticsAggregations.

protected List<AbstractAggregationBuilder> getElasticsearchStatisticsAggregations(StatisticsAggregation agg) {
    List<AbstractAggregationBuilder> aggs = new ArrayList<>();
    for (String propertyName : getPropertyNames(agg.getFieldName())) {
        String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
        String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
        ExtendedStatsAggregationBuilder statsAgg = AggregationBuilders.extendedStats(aggName);
        statsAgg.field(propertyName);
        aggs.add(statsAgg);
    }
    return aggs;
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) ExtendedStatsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder)

Example 4 with AbstractAggregationBuilder

use of org.elasticsearch.search.aggregations.AbstractAggregationBuilder in project sonarqube by SonarSource.

the class RuleIndex method addStatusFacetIfNeeded.

private static void addStatusFacetIfNeeded(SearchOptions options, Map<String, AbstractAggregationBuilder> aggregations, StickyFacetBuilder stickyFacetBuilder) {
    if (options.getFacets().contains(FACET_STATUSES)) {
        BoolQueryBuilder facetFilter = stickyFacetBuilder.getStickyFacetFilter(FIELD_RULE_STATUS);
        AbstractAggregationBuilder statuses = AggregationBuilders.filter(FACET_STATUSES + "_filter").filter(facetFilter).subAggregation(AggregationBuilders.terms(FACET_STATUSES).field(FIELD_RULE_STATUS).include(Joiner.on('|').join(ALL_STATUSES_EXCEPT_REMOVED)).exclude(RuleStatus.REMOVED.toString()).size(ALL_STATUSES_EXCEPT_REMOVED.size()));
        aggregations.put(FACET_STATUSES, AggregationBuilders.global(FACET_STATUSES).subAggregation(statuses));
    }
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 5 with AbstractAggregationBuilder

use of org.elasticsearch.search.aggregations.AbstractAggregationBuilder in project molgenis by molgenis.

the class AggregationGenerator method createAggregations.

private List<AggregationBuilder> createAggregations(LinkedList<Attribute> attrs, Attribute parentAttr, Attribute distinctAttr) {
    Attribute attr = attrs.pop();
    List<AggregationBuilder> aggs = new ArrayList<>();
    // term aggregation
    String termsAggName = attr.getName() + FieldConstants.AGGREGATION_TERMS_POSTFIX;
    String termsAggFieldName = getAggregateFieldName(attr);
    AggregationBuilder termsAgg = AggregationBuilders.terms(termsAggName).size(MAX_VALUE).field(termsAggFieldName);
    aggs.add(termsAgg);
    // missing term aggregation
    if (attr.isNillable()) {
        String missingAggName = attr.getName() + FieldConstants.AGGREGATION_MISSING_POSTFIX;
        String missingAggFieldName = getAggregateFieldName(attr);
        AggregationBuilder missingTermsAgg = AggregationBuilders.missing(missingAggName).field(missingAggFieldName);
        aggs.add(missingTermsAgg);
    }
    // add distinct term aggregations
    if (attrs.isEmpty() && distinctAttr != null) {
        String cardinalityAggName = distinctAttr.getName() + FieldConstants.AGGREGATION_DISTINCT_POSTFIX;
        String cardinalityAggFieldName = getAggregateFieldName(distinctAttr);
        CardinalityAggregationBuilder distinctAgg = AggregationBuilders.cardinality(cardinalityAggName).field(cardinalityAggFieldName).precisionThreshold(PRECISION_THRESHOLD);
        // CardinalityBuilder does not implement AggregationBuilder interface, so we need some more code
        AbstractAggregationBuilder wrappedDistinctAgg;
        if (AggregateUtils.isNestedType(distinctAttr)) {
            String nestedAggName = distinctAttr.getName() + AGGREGATION_NESTED_POSTFIX;
            String nestedAggFieldName = getAggregatePathName(distinctAttr);
            NestedAggregationBuilder nestedBuilder = AggregationBuilders.nested(nestedAggName, nestedAggFieldName);
            nestedBuilder.subAggregation(distinctAgg);
            if (AggregateUtils.isNestedType(attr)) {
                String reverseAggName = attr.getName() + AggregationGenerator.AGGREGATION_REVERSE_POSTFIX;
                ReverseNestedAggregationBuilder reverseNestedBuilder = AggregationBuilders.reverseNested(reverseAggName);
                reverseNestedBuilder.subAggregation(nestedBuilder);
                wrappedDistinctAgg = reverseNestedBuilder;
            } else {
                wrappedDistinctAgg = nestedBuilder;
            }
        } else {
            if (AggregateUtils.isNestedType(attr)) {
                String reverseAggName = attr.getName() + AggregationGenerator.AGGREGATION_REVERSE_POSTFIX;
                ReverseNestedAggregationBuilder reverseNestedBuilder = AggregationBuilders.reverseNested(reverseAggName);
                reverseNestedBuilder.subAggregation(distinctAgg);
                wrappedDistinctAgg = reverseNestedBuilder;
            } else {
                wrappedDistinctAgg = distinctAgg;
            }
        }
        // add wrapped distinct term aggregation to aggregations
        for (AggregationBuilder agg : aggs) {
            agg.subAggregation(wrappedDistinctAgg);
        }
    }
    // add sub aggregations
    if (!attrs.isEmpty()) {
        List<AggregationBuilder> subAggs = createAggregations(attrs, attr, distinctAttr);
        for (AggregationBuilder agg : aggs) {
            for (AggregationBuilder subAgg : subAggs) {
                agg.subAggregation(subAgg);
            }
        }
    }
    // wrap in nested aggregation is this aggregation is nested
    if (AggregateUtils.isNestedType(attr)) {
        String nestedAggName = attr.getName() + AGGREGATION_NESTED_POSTFIX;
        String nestedAggFieldName = getAggregatePathName(attr);
        NestedAggregationBuilder nestedAgg = AggregationBuilders.nested(nestedAggName, nestedAggFieldName);
        for (AggregationBuilder agg : aggs) {
            nestedAgg.subAggregation(agg);
        }
        aggs = Collections.singletonList(nestedAgg);
    }
    // wrap in reverse nested aggregation if parent aggregation is nested
    if (parentAttr != null && AggregateUtils.isNestedType(parentAttr)) {
        String reverseAggName = parentAttr.getName() + AggregationGenerator.AGGREGATION_REVERSE_POSTFIX;
        ReverseNestedAggregationBuilder reverseNestedAgg = AggregationBuilders.reverseNested(reverseAggName);
        for (AggregationBuilder agg : aggs) {
            reverseNestedAgg.subAggregation(agg);
        }
        aggs = Collections.singletonList(reverseNestedAgg);
    }
    return aggs;
}
Also used : ReverseNestedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder) NestedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) CardinalityAggregationBuilder(org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder) ReverseNestedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) Attribute(org.molgenis.data.meta.model.Attribute) ArrayList(java.util.ArrayList) CardinalityAggregationBuilder(org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder) NestedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder) ReverseNestedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder)

Aggregations

AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)5 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Request (okhttp3.Request)1 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)1 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)1 NestedAggregationBuilder (org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder)1 ReverseNestedAggregationBuilder (org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder)1 CardinalityAggregationBuilder (org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder)1 ExtendedStatsAggregationBuilder (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 Attribute (org.molgenis.data.meta.model.Attribute)1