Search in sources :

Example 21 with AggregationBuilder

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

the class StickyFacetBuilder method buildTopFacetAggregation.

private FilterAggregationBuilder buildTopFacetAggregation(String fieldName, String facetName, BoolQueryBuilder facetFilter, int size, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter) {
    TermsAggregationBuilder termsAggregation = buildTermsFacetAggregation(fieldName, facetName, size);
    AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(termsAggregation);
    return AggregationBuilders.filter(facetName + "_filter", facetFilter).subAggregation(improvedAggregation);
}
Also used : 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)

Example 22 with AggregationBuilder

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

the class IssueIndex method addResolutionFacetIfNeeded.

private static void addResolutionFacetIfNeeded(SearchOptions options, IssueQuery query, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest) {
    if (!options.getFacets().contains(PARAM_RESOLUTIONS)) {
        return;
    }
    AggregationBuilder aggregation = aggregationHelper.buildTermTopAggregation(RESOLUTIONS.getName(), RESOLUTIONS.getTopAggregationDef(), RESOLUTIONS.getNumberOfTerms(), NO_EXTRA_FILTER, t -> t.subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.missing(RESOLUTIONS.getName() + FACET_SUFFIX_MISSING).field(RESOLUTIONS.getFieldName()))));
    esRequest.aggregation(aggregation);
}
Also used : 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)

Example 23 with AggregationBuilder

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

the class IssueIndex method addAssigneesFacetIfNeeded.

private static void addAssigneesFacetIfNeeded(SearchOptions options, IssueQuery query, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest) {
    if (!options.getFacets().contains(PARAM_ASSIGNEES)) {
        return;
    }
    Consumer<FilterAggregationBuilder> assigneeAggregations = t -> {
        // optional second aggregation to return the issue count for selected assignees (if any)
        Object[] assignees = query.assignees().toArray();
        aggregationHelper.getSubAggregationHelper().buildSelectedItemsAggregation(ASSIGNEES.getName(), ASSIGNEES.getTopAggregationDef(), assignees).ifPresent(t::subAggregation);
        // third aggregation to always return the count of unassigned in the assignee facet
        t.subAggregation(addEffortAggregationIfNeeded(query, AggregationBuilders.missing(ASSIGNEES.getName() + FACET_SUFFIX_MISSING).field(ASSIGNEES.getFieldName())));
    };
    AggregationBuilder aggregation = aggregationHelper.buildTermTopAggregation(ASSIGNEES.getName(), ASSIGNEES.getTopAggregationDef(), ASSIGNEES.getNumberOfTerms(), NO_EXTRA_FILTER, assigneeAggregations);
    esRequest.aggregation(aggregation);
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) CWE(org.sonar.server.issue.index.IssueIndex.Facet.CWE) SumAggregationBuilder(org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder) PARAM_ASSIGNEES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGNEES) AUTHOR(org.sonar.server.issue.index.IssueIndex.Facet.AUTHOR) EsClient(org.sonar.server.es.EsClient) Collections.singletonList(java.util.Collections.singletonList) FIELD_ISSUE_KEY(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_KEY) SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) SimpleFieldTopAggregationDefinition(org.sonar.server.es.searchrequest.SimpleFieldTopAggregationDefinition) LANGUAGES(org.sonar.server.issue.index.IssueIndex.Facet.LANGUAGES) PARAM_FILES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_FILES) Map(java.util.Map) FIELD_ISSUE_FUNC_CREATED_AT(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT) FIELD_ISSUE_TAGS(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_TAGS) FIELD_ISSUE_RULE_UUID(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_RULE_UUID) ParsedFilter(org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) SEVERITIES(org.sonar.server.issue.index.IssueIndex.Facet.SEVERITIES) FIELD_ISSUE_SCOPE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_SCOPE) PARAM_SCOPES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SCOPES) PARAM_STATUSES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_STATUSES) SANS_TOP_25(org.sonar.server.issue.index.IssueIndex.Facet.SANS_TOP_25) FIELD_ISSUE_CWE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_CWE) TYPE_ISSUE(org.sonar.server.issue.index.IssueIndexDefinition.TYPE_ISSUE) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) PARAM_TYPES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TYPES) Set(java.util.Set) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) FIELD_ISSUE_IS_MAIN_BRANCH(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_IS_MAIN_BRANCH) FIELD_ISSUE_STATUS(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_STATUS) ParsedMax(org.elasticsearch.search.aggregations.metrics.ParsedMax) Stream(java.util.stream.Stream) CREATED_AT(org.sonar.server.issue.index.IssueIndex.Facet.CREATED_AT) Min(org.elasticsearch.search.aggregations.metrics.Min) FILES(org.sonar.server.issue.index.IssueIndex.Facet.FILES) SecurityReviewRating.computePercent(org.sonar.server.security.SecurityReviewRating.computePercent) ViewIndexDefinition(org.sonar.server.view.index.ViewIndexDefinition) FIELD_ISSUE_ASSIGNEE_UUID(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE_UUID) FIELD_ISSUE_LINE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_LINE) PROJECT_UUIDS(org.sonar.server.issue.index.IssueIndex.Facet.PROJECT_UUIDS) TYPE_VIEW(org.sonar.server.view.index.ViewIndexDefinition.TYPE_VIEW) AuthorizationDoc(org.sonar.server.permission.index.AuthorizationDoc) Duration(org.joda.time.Duration) FIELD_ISSUE_BRANCH_UUID(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_BRANCH_UUID) FIELD_ISSUE_TYPE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_TYPE) QueryBuilders.rangeQuery(org.elasticsearch.index.query.QueryBuilders.rangeQuery) RULES(org.sonar.server.issue.index.IssueIndex.Facet.RULES) SearchRequest(org.elasticsearch.action.search.SearchRequest) STATUSES(org.sonar.server.issue.index.IssueIndex.Facet.STATUSES) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) FIELD_ISSUE_VULNERABILITY_PROBABILITY(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_VULNERABILITY_PROBABILITY) PARAM_CREATED_AT(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CREATED_AT) OptionalLong(java.util.OptionalLong) FIELD_ISSUE_RESOLUTION(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_RESOLUTION) FIELD_ISSUE_SEVERITY_VALUE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_SEVERITY_VALUE) Severity(org.sonar.api.rule.Severity) DateUtils(org.sonar.api.utils.DateUtils) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) STICKY(org.sonar.server.es.searchrequest.TopAggregationDefinition.STICKY) FIELD_INDEX_TYPE(org.sonar.server.es.IndexType.FIELD_INDEX_TYPE) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Nullable(javax.annotation.Nullable) FIELD_ISSUE_NEW_CODE_REFERENCE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_NEW_CODE_REFERENCE) PARAM_DIRECTORIES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_DIRECTORIES) PARAM_RESOLUTIONS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_RESOLUTIONS) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) PARAM_SEVERITIES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SEVERITIES) QueryBuilders.matchAllQuery(org.elasticsearch.index.query.QueryBuilders.matchAllQuery) RequestFiltersComputer(org.sonar.server.es.searchrequest.RequestFiltersComputer) NON_STICKY(org.sonar.server.es.searchrequest.TopAggregationDefinition.NON_STICKY) NO_OTHER_SUBAGGREGATION(org.sonar.server.es.searchrequest.TopAggregationHelper.NO_OTHER_SUBAGGREGATION) PARAM_LANGUAGES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_LANGUAGES) ASSIGNED_TO_ME(org.sonar.server.issue.index.IssueIndex.Facet.ASSIGNED_TO_ME) FIELD_ISSUE_OWASP_TOP_10(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_OWASP_TOP_10) HasAggregations(org.elasticsearch.search.aggregations.HasAggregations) SANS_TOP_25_RISKY_RESOURCE(org.sonar.server.security.SecurityStandards.SANS_TOP_25_RISKY_RESOURCE) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) PARAM_OWASP_TOP_10(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_OWASP_TOP_10) FIELD_ISSUE_FILE_PATH(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_FILE_PATH) Preconditions(com.google.common.base.Preconditions) FIELD_ISSUE_FUNC_CLOSED_AT(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_FUNC_CLOSED_AT) SecurityStandards(org.sonar.server.security.SecurityStandards) Date(java.util.Date) MODULE_UUIDS(org.sonar.server.issue.index.IssueIndex.Facet.MODULE_UUIDS) LongBounds(org.elasticsearch.search.aggregations.bucket.histogram.LongBounds) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) OWASP_TOP_10(org.sonar.server.issue.index.IssueIndex.Facet.OWASP_TOP_10) ParsedStringTerms(org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms) BaseDoc(org.sonar.server.es.BaseDoc) FIELD_ISSUE_PROJECT_UUID(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID) PARAM_SONARSOURCE_SECURITY(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SONARSOURCE_SECURITY) SONARSOURCE_SECURITY(org.sonar.server.issue.index.IssueIndex.Facet.SONARSOURCE_SECURITY) FIELD_ISSUE_DIRECTORY_PATH(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_DIRECTORY_PATH) PARAM_AUTHOR(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_AUTHOR) SearchResponse(org.elasticsearch.action.search.SearchResponse) FIELD_ISSUE_AUTHOR_LOGIN(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_AUTHOR_LOGIN) TAGS(org.sonar.server.issue.index.IssueIndex.Facet.TAGS) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) SubAggregationHelper(org.sonar.server.es.searchrequest.SubAggregationHelper) FIELD_ISSUE_EFFORT(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_EFFORT) System2(org.sonar.api.utils.System2) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) VULNERABILITY(org.sonar.api.rules.RuleType.VULNERABILITY) Collection(java.util.Collection) DIRECTORIES(org.sonar.server.issue.index.IssueIndex.Facet.DIRECTORIES) RESOLUTIONS(org.sonar.server.issue.index.IssueIndex.Facet.RESOLUTIONS) PARAM_SANS_TOP_25(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SANS_TOP_25) TopAggregationDefinition(org.sonar.server.es.searchrequest.TopAggregationDefinition) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) TopAggregationHelper(org.sonar.server.es.searchrequest.TopAggregationHelper) BaseDoc.epochMillisToEpochSeconds(org.sonar.server.es.BaseDoc.epochMillisToEpochSeconds) ASSIGNEES(org.sonar.server.issue.index.IssueIndex.Facet.ASSIGNEES) List(java.util.List) SearchOptions(org.sonar.server.es.SearchOptions) SecurityReviewRating.computeRating(org.sonar.server.security.SecurityReviewRating.computeRating) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) AllFilters(org.sonar.server.es.searchrequest.RequestFiltersComputer.AllFilters) SCOPES(org.sonar.server.issue.index.IssueIndex.Facet.SCOPES) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SQCategory(org.sonar.server.security.SecurityStandards.SQCategory) IntStream(java.util.stream.IntStream) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) FACET_MODE_EFFORT(org.sonarqube.ws.client.issue.IssuesWsParameters.FACET_MODE_EFFORT) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) CWES_BY_CWE_TOP_25(org.sonar.server.security.SecurityStandards.CWES_BY_CWE_TOP_25) PARAM_RULES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_RULES) EsUtils.escapeSpecialRegexChars(org.sonar.server.es.EsUtils.escapeSpecialRegexChars) Sorting(org.sonar.server.es.Sorting) FIELD_ISSUE_MODULE_UUID(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_MODULE_UUID) SANS_TOP_25_POROUS_DEFENSES(org.sonar.server.security.SecurityStandards.SANS_TOP_25_POROUS_DEFENSES) FIELD_ISSUE_MODULE_PATH(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH) OptionalInt(java.util.OptionalInt) EsUtils(org.sonar.server.es.EsUtils) FIELD_ISSUE_SEVERITY(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_SEVERITY) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) PARAM_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TAGS) FIELD_ISSUE_LANGUAGE(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_LANGUAGE) SANS_TOP_25_INSECURE_INTERACTION(org.sonar.server.security.SecurityStandards.SANS_TOP_25_INSECURE_INTERACTION) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) FIELD_ISSUE_SANS_TOP_25(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_SANS_TOP_25) QueryBuilders.termsQuery(org.elasticsearch.index.query.QueryBuilders.termsQuery) TYPES(org.sonar.server.issue.index.IssueIndex.Facet.TYPES) PARAM_CWE(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CWE) FIELD_ISSUE_FUNC_UPDATED_AT(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_FUNC_UPDATED_AT) FIELD_ISSUE_SQ_SECURITY_CATEGORY(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_SQ_SECURITY_CATEGORY) FIELD_ISSUE_COMPONENT_UUID(org.sonar.server.issue.index.IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID) TermsLookup(org.elasticsearch.indices.TermsLookup) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) PeriodStart(org.sonar.server.issue.index.IssueQuery.PeriodStart) ParsedValueCount(org.elasticsearch.search.aggregations.metrics.ParsedValueCount) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Issue(org.sonar.api.issue.Issue) NO_EXTRA_FILTER(org.sonar.server.es.searchrequest.TopAggregationHelper.NO_EXTRA_FILTER) UserSession(org.sonar.server.user.UserSession) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) IndexType(org.sonar.server.es.IndexType) QueryBuilders.existsQuery(org.elasticsearch.index.query.QueryBuilders.existsQuery) 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)

Example 24 with AggregationBuilder

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

the class IssueIndex method getCreatedAtFacet.

private Optional<AggregationBuilder> getCreatedAtFacet(IssueQuery query, Map<String, QueryBuilder> filters, QueryBuilder esQuery) {
    long startTime;
    Date createdAfter = query.createdAfter();
    if (createdAfter == null) {
        Optional<Long> minDate = getMinCreatedAt(filters, esQuery);
        if (!minDate.isPresent()) {
            return Optional.empty();
        }
        startTime = minDate.get();
    } else {
        startTime = createdAfter.getTime();
    }
    Date createdBefore = query.createdBefore();
    long endTime = createdBefore == null ? system.now() : createdBefore.getTime();
    Duration timeSpan = new Duration(startTime, endTime);
    DateHistogramInterval bucketSize = DateHistogramInterval.YEAR;
    if (timeSpan.isShorterThan(TWENTY_DAYS)) {
        bucketSize = DateHistogramInterval.DAY;
    } else if (timeSpan.isShorterThan(TWENTY_WEEKS)) {
        bucketSize = DateHistogramInterval.WEEK;
    } else if (timeSpan.isShorterThan(TWENTY_MONTHS)) {
        bucketSize = DateHistogramInterval.MONTH;
    }
    // from GMT to server TZ
    int offsetInSeconds = -system.getDefaultTimeZone().getRawOffset() / 1_000;
    AggregationBuilder dateHistogram = AggregationBuilders.dateHistogram(PARAM_CREATED_AT).field(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT).interval(bucketSize).minDocCount(0L).format(DateUtils.DATETIME_FORMAT).timeZone(TimeZone.getTimeZone("GMT").getID()).offset(offsetInSeconds + "s").extendedBounds(startTime, endTime - 1_000L);
    dateHistogram = addEffortAggregationIfNeeded(query, dateHistogram);
    return Optional.of(dateHistogram);
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) DateHistogramInterval(org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval) Duration(org.joda.time.Duration) Date(java.util.Date)

Example 25 with AggregationBuilder

use of org.elasticsearch.search.aggregations.AggregationBuilder 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

AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)39 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)28 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)10 AbstractAggregationBuilder (org.elasticsearch.search.aggregations.AbstractAggregationBuilder)10 SumAggregationBuilder (org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder)8 CardinalityAggregationBuilder (org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder)8 ArrayList (java.util.ArrayList)7 RangeAggregationBuilder (org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder)7 DateRangeAggregationBuilder (org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder)7 Map (java.util.Map)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 GeoGridAggregationBuilder (org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder)6 DateHistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)6 HistogramAggregationBuilder (org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder)6 PercentilesAggregationBuilder (org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder)6 ExtendedStatsAggregationBuilder (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder)6 BucketSelectorPipelineAggregationBuilder (org.elasticsearch.search.aggregations.pipeline.bucketselector.BucketSelectorPipelineAggregationBuilder)6 HashMap (java.util.HashMap)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5