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