use of org.graylog.shaded.elasticsearch7.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.graylog.shaded.elasticsearch7.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;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilder in project molgenis by molgenis.
the class AggregationGenerator method generate.
public void generate(SearchRequestBuilder searchRequestBuilder, Attribute aggAttr1, Attribute aggAttr2, Attribute aggAttrDistinct) {
List<AggregationBuilder> aggregationBuilders = createAggregations(aggAttr1, aggAttr2, aggAttrDistinct);
// collect aggregates
searchRequestBuilder.setSize(0);
// add all aggregations to builder
for (AggregationBuilder aggregationBuilder : aggregationBuilders) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilder in project incubator-sdap-mudrod by apache.
the class LogAbstract method getUserDailyDocs.
public Map<String, Long> getUserDailyDocs() {
int docCount = es.getDocCount(logIndex, httpType);
AggregationBuilder dailyAgg = AggregationBuilders.dateHistogram("by_day").field("Time").dateHistogramInterval(DateHistogramInterval.DAY).order(Order.COUNT_DESC);
SearchResponse sr = es.getClient().prepareSearch(logIndex).setTypes(httpType).setQuery(QueryBuilders.matchAllQuery()).setSize(0).addAggregation(AggregationBuilders.terms("Users").field("IP").size(docCount).subAggregation(dailyAgg)).execute().actionGet();
Terms users = sr.getAggregations().get("Users");
Map<String, Long> userList = new HashMap<>();
for (Terms.Bucket user : users.getBuckets()) {
String ip = (String) user.getKey();
System.out.println(ip);
Histogram agg = user.getAggregations().get("by_day");
List<? extends Histogram.Bucket> dateList = agg.getBuckets();
for (Histogram.Bucket aDateList : dateList) {
Long count = aDateList.getDocCount();
String date = aDateList.getKey().toString();
System.out.println(date);
System.out.println(count);
}
}
return userList;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.
the class StickyFacetBuilder method addSelectedItemsToFacet.
public FilterAggregationBuilder addSelectedItemsToFacet(String fieldName, String facetName, FilterAggregationBuilder facetTopAggregation, Function<TermsAggregationBuilder, AggregationBuilder> additionalAggregationFilter, 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);
TermsAggregationBuilder selectedTerms = AggregationBuilders.terms(facetName + "_selected").size(max(MAXIMUM_NUMBER_OF_SELECTED_ITEMS_WHOSE_DOC_COUNT_WILL_BE_CALCULATED, includes.length())).field(fieldName).includeExclude(new IncludeExclude(includes, null));
if (subAggregation != null) {
selectedTerms = selectedTerms.subAggregation(subAggregation);
}
AggregationBuilder improvedAggregation = additionalAggregationFilter.apply(selectedTerms);
facetTopAggregation.subAggregation(improvedAggregation);
return facetTopAggregation;
}
Aggregations