use of 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.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.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;
}
use of org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.
the class TopAggregationHelperTest method buildTermTopAggregation_adds_subAggregation_from_lambda_parameter.
@Test
public void buildTermTopAggregation_adds_subAggregation_from_lambda_parameter() {
SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
AggregationBuilder[] subAggs = IntStream.range(0, 1 + new Random().nextInt(12)).mapToObj(i -> AggregationBuilders.min("subAgg_" + i)).toArray(AggregationBuilder[]::new);
String topAggregationName = randomAlphabetic(10);
TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);
AggregationBuilder[] allSubAggs = Stream.concat(Arrays.stream(subAggs), Stream.of(termSubAgg)).toArray(AggregationBuilder[]::new);
AggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(topAggregationName, topAggregation, null, NO_EXTRA_FILTER, t -> Arrays.stream(subAggs).forEach(t::subAggregation));
assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
assertThat(aggregationBuilder.getSubAggregations()).hasSize(allSubAggs.length);
assertThat(aggregationBuilder.getSubAggregations()).containsExactlyInAnyOrder(allSubAggs);
}
use of org.elasticsearch.search.aggregations.AggregationBuilder in project sonarqube by SonarSource.
the class IssueIndex method addSecurityCategoryFacetIfNeeded.
private static void addSecurityCategoryFacetIfNeeded(String param, Facet facet, SearchOptions options, TopAggregationHelper aggregationHelper, SearchSourceBuilder esRequest, Object[] selectedValues) {
if (!options.getFacets().contains(param)) {
return;
}
AggregationBuilder aggregation = aggregationHelper.buildTermTopAggregation(facet.getName(), facet.getTopAggregationDef(), facet.getNumberOfTerms(), filter -> filter.must(termQuery(FIELD_ISSUE_TYPE, VULNERABILITY.name())), t -> aggregationHelper.getSubAggregationHelper().buildSelectedItemsAggregation(facet.getName(), facet.getTopAggregationDef(), selectedValues).ifPresent(t::subAggregation));
esRequest.aggregation(aggregation);
}
Aggregations