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