org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchFeatureDaoTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private Object[] getFeaturesForPeriodThrowIllegalStateData() {
    String aggName = "aggName";

    InternalTDigestPercentiles empty = mock(InternalTDigestPercentiles.class);
    Iterator<Percentile> emptyIterator = mock(Iterator.class);
    when(empty.iterator()).thenReturn(emptyIterator);
    when(emptyIterator.hasNext()).thenReturn(false);
    when(empty.getName()).thenReturn(aggName);

    MultiBucketsAggregation multiBucket = mock(MultiBucketsAggregation.class);
    when(multiBucket.getName()).thenReturn(aggName);

    return new Object[] {
        new Object[] { asList(empty), asList(aggName), null },
        new Object[] { asList(multiBucket), asList(aggName), null }, };
}
 
Example #2
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static void writeOrder(Terms.Order order, StreamOutput out) throws IOException {
    if (order instanceof Aggregation) {
        out.writeByte(order.id());
        Aggregation aggregationOrder = (Aggregation) order;
        out.writeBoolean(((MultiBucketsAggregation.Bucket.SubAggregationComparator) aggregationOrder.comparator).asc());
        AggregationPath path = ((Aggregation) order).path();
        out.writeString(path.toString());
    } else if (order instanceof CompoundOrder) {
        CompoundOrder compoundOrder = (CompoundOrder) order;
            out.writeByte(order.id());
            out.writeVInt(compoundOrder.orderElements.size());
            for (Terms.Order innerOrder : compoundOrder.orderElements) {
                Streams.writeOrder(innerOrder, out);
            }
    } else {
        out.writeByte(order.id());
    }
}
 
Example #3
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception {
    final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
        .field("tags")
        .order(BucketOrder.count(false));

    final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
    final SearchRequest searchRequest = new SearchRequest().indices("blog")
        .source(builder);

    final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

    final Map<String, Aggregation> results = response.getAggregations()
        .asMap();
    final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");

    final List<String> keys = topTags.getBuckets()
        .stream()
        .map(MultiBucketsAggregation.Bucket::getKeyAsString)
        .collect(toList());
    assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
}
 
Example #4
Source File: ElasticsearchTransactionRepository.java    From servicecomb-pack with Apache License 2.0 6 votes vote down vote up
public Map<String, Long> getTransactionStatistics() {
  TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders
      .terms("count_group_by_state").field("state.keyword");
  SearchQuery searchQuery = new NativeSearchQueryBuilder()
      .withIndices(INDEX_NAME)
      .addAggregation(termsAggregationBuilder)
      .build();
  return this.template.query(searchQuery, response -> {
    Map<String, Long> statistics = new HashMap<>();
    if (response.getHits().totalHits > 0) {
      final StringTerms groupState = response.getAggregations().get("count_group_by_state");
      statistics = groupState.getBuckets()
          .stream()
          .collect(Collectors.toMap(MultiBucketsAggregation.Bucket::getKeyAsString,
              MultiBucketsAggregation.Bucket::getDocCount));
    }
    return statistics;
  });
}
 
Example #5
Source File: ElasticSearchQueryManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception {
    final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags")
        .field("title");

    final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
    final SearchRequest searchRequest = new SearchRequest("blog").source(builder);

    final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

    final Map<String, Aggregation> results = response.getAggregations()
        .asMap();
    final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");

    final List<String> keys = topTags.getBuckets()
        .stream()
        .map(MultiBucketsAggregation.Bucket::getKeyAsString)
        .sorted()
        .collect(toList());
    assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
}
 
Example #6
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 6 votes vote down vote up
public TResult reduce(ElasticsearchSearchQueryBase query, Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey) {
    List<TBucket> buckets = new ArrayList<>();
    for (Map.Entry<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKeyEntry : bucketsByKey.entrySet()) {
        String key = bucketKeyToString(bucketsByKeyEntry.getKey(), null);
        long count = 0;
        List<Aggregation> subAggs = new ArrayList<>();
        for (MultiBucketsAggregation.Bucket b : bucketsByKeyEntry.getValue()) {
            count += b.getDocCount();
            for (Aggregation subAgg : b.getAggregations()) {
                subAggs.add(subAgg);
            }
        }
        Map<String, AggregationResult> nestedResults = reduceAggregationResults(query, getAggregationResultsByName(query, subAggs));
        buckets.add(createBucket(key, count, nestedResults, bucketsByKeyEntry.getValue()));
    }
    return bucketsToResults(buckets);
}
 
Example #7
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 6 votes vote down vote up
public TResult reduce(ElasticsearchSearchQueryBase query, Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey) {
    List<TBucket> buckets = new ArrayList<>();
    for (Map.Entry<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKeyEntry : bucketsByKey.entrySet()) {
        String key = bucketKeyToString(bucketsByKeyEntry.getKey(), null);
        long count = 0;
        List<Aggregation> subAggs = new ArrayList<>();
        for (MultiBucketsAggregation.Bucket b : bucketsByKeyEntry.getValue()) {
            count += b.getDocCount();
            for (Aggregation subAgg : b.getAggregations()) {
                subAggs.add(subAgg);
            }
        }
        Map<String, AggregationResult> nestedResults = reduceAggregationResults(query, getAggregationResultsByName(query, subAggs));
        buckets.add(createBucket(key, count, nestedResults, bucketsByKeyEntry.getValue()));
    }
    return bucketsToResults(buckets);
}
 
Example #8
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public <T extends Aggregation> T getAggregationByPath(List<String> paths) {
  	this.checkAggs();
  	T agg = aggregations.get(paths.get(0));
  	for (int i = 1; i < paths.size(); i++) {
  		String attr = paths.get(i);
  		if(agg instanceof HasAggregations){
  			HasAggregations hasagg = (HasAggregations) agg;
  			agg = hasagg.getAggregations().get(attr);
  		}else if(agg instanceof MultiBucketsAggregation){
  			MultiBucketsAggregation magg = (MultiBucketsAggregation) agg;
  			if(magg.getBuckets().isEmpty()){
  				return agg;
  			}
  			Bucket bucket = magg.getBuckets().get(0);
  			agg = bucket.getAggregations().get(attr);
  		}else{
  			break;
  		}
}
  	return agg;
  }
 
Example #9
Source File: Search.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
public static void LeafTraverse(final ArrayList<Function> statsFields, final ArrayList<com.everdata.parser.AST_Stats.Bucket> bucketFields, Queue<List<String>> rows, 
		org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket node, Stack<String> path, int[] totalRows,
		final int from, final int size) throws CommandException{
	
	
	path.push(node.getKey());
	
	MultiBucketsAggregation bucketAgg = node.getAggregations().get("statsWithBy");
	if( bucketAgg != null ){
		Iterator<? extends org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket> iterator = bucketAgg.getBuckets().iterator();
		while (iterator.hasNext()) {
			LeafTraverse(statsFields, bucketFields, rows, iterator.next(), path, totalRows, from, size);
		}
	}else{
		if(size < 0 ||  totalRows[0] < (from + size) ){
			List<String> row = new ArrayList<String>();
			row.addAll(path);
			
			Map<String, Aggregation> map = node.getAggregations().getAsMap();

			for (Function f : statsFields) {
				row.add(getValueFromAggregation(map.get(f.statsField), f));	
			}
			
			row.add(String.valueOf(node.getDocCount()));

			rows.add(row);
		}
		totalRows[0] += 1;
	}
	
	path.pop();
}
 
Example #10
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static GeoPoint getAverageGeoPointFromBuckets(List<MultiBucketsAggregation.Bucket> buckets) {
    List<GeoPoint> geoPoints = new ArrayList<>();
    for (MultiBucketsAggregation.Bucket b : buckets) {
        GeoGrid.Bucket gb = (GeoGrid.Bucket) b;
        org.elasticsearch.common.geo.GeoPoint gp = (org.elasticsearch.common.geo.GeoPoint) gb.getKey();
        geoPoints.add(new GeoPoint(gp.getLat(), gp.getLon()));
    }
    return GeoPoint.calculateCenter(geoPoints);
}
 
Example #11
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static GeoPoint getAverageGeoPointFromBuckets(List<MultiBucketsAggregation.Bucket> buckets) {
    List<GeoPoint> geoPoints = new ArrayList<>();
    for (MultiBucketsAggregation.Bucket b : buckets) {
        GeoHashGrid.Bucket gb = (GeoHashGrid.Bucket) b;
        org.elasticsearch.common.geo.GeoPoint gp = (org.elasticsearch.common.geo.GeoPoint) gb.getKey();
        geoPoints.add(new GeoPoint(gp.getLat(), gp.getLon()));
    }
    return GeoPoint.calculateCenter(geoPoints);
}
 
Example #12
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
static public List<? extends Bucket> getBuckets(Aggregation agg) {
	if(agg instanceof MultiBucketsAggregation){
		return ((MultiBucketsAggregation)agg).getBuckets();
	}else{
		return ImmutableList.of();
	}
}
 
Example #13
Source File: TransportBasedClient.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void setAggregations(Aggregations aggregations, ActionResponse actionResp) {
  // Only the result of the first aggregation is returned
  //
  final Aggregation agg = aggregations.asList().get(0);

  if (agg instanceof InternalMetricsAggregation) {
    actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
        XContentHelper.toString((InternalMetricsAggregation) agg).toString()));
  } else if (agg instanceof InternalSingleBucketAggregation) {
    actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
        XContentHelper.toString((InternalSingleBucketAggregation) agg).toString()));
  } else if (agg instanceof InternalMultiBucketAggregation) {
    final Set<String> headerKeys = new HashSet<>();
    final List<Map<String, Object>> buckets = new LinkedList<>();
    final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;

    for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
      try {
        final XContentBuilder builder = XContentFactory.jsonBuilder();
        bucket.toXContent(builder, null);
        actionResp.addAggregation(
            new AggWrapper(AggWrapper.AggregationType.MULTI_BUCKETS, builder.string()));
      } catch (final IOException e) {
        // Ignored
      }
    }
  }
}
 
Example #14
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
Aggregation(String key, boolean asc) {
    super(ID, key, asc, new MultiBucketsAggregation.Bucket.SubAggregationComparator<Terms.Bucket>(key, asc));
}
 
Example #15
Source File: EsHelper.java    From occurrence with Apache License 2.0 4 votes vote down vote up
private static Set<String> parseFindExistingIndexesInAliasResponse(SearchResponse response) {
  return ((Terms) response.getAggregations().get(AGG_BY_INDEX))
      .getBuckets().stream()
          .map(MultiBucketsAggregation.Bucket::getKeyAsString)
          .collect(Collectors.toSet());
}
 
Example #16
Source File: ObjectResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private void handleAggregations(Aggregations aggregations, List<String> headers, List<List<Object>> lines) throws ObjectResultsExtractException {
    if (allNumericAggregations(aggregations)) {
        lines.get(this.currentLineIndex).addAll(fillHeaderAndCreateLineForNumericAggregations(aggregations, headers));
        return;
    }
    //aggregations with size one only supported when not metrics.
    List<Aggregation> aggregationList = aggregations.asList();
    if (aggregationList.size() > 1) {
        throw new ObjectResultsExtractException("currently support only one aggregation at same level (Except for numeric metrics)");
    }
    Aggregation aggregation = aggregationList.get(0);
    //we want to skip singleBucketAggregations (nested,reverse_nested,filters)
    if (aggregation instanceof SingleBucketAggregation) {
        Aggregations singleBucketAggs = ((SingleBucketAggregation) aggregation).getAggregations();
        handleAggregations(singleBucketAggs, headers, lines);
        return;
    }
    if (aggregation instanceof NumericMetricsAggregation) {
        handleNumericMetricAggregation(headers, lines.get(currentLineIndex), aggregation);
        return;
    }
    if (aggregation instanceof GeoBounds) {
        handleGeoBoundsAggregation(headers, lines, (GeoBounds) aggregation);
        return;
    }
    if (aggregation instanceof TopHits) {
        //todo: handle this . it returns hits... maby back to normal?
        //todo: read about this usages
        // TopHits topHitsAggregation = (TopHits) aggregation;
    }
    if (aggregation instanceof MultiBucketsAggregation) {
        MultiBucketsAggregation bucketsAggregation = (MultiBucketsAggregation) aggregation;
        String name = bucketsAggregation.getName();
        //checking because it can comes from sub aggregation again
        if (!headers.contains(name)) {
            headers.add(name);
        }
        Collection<? extends MultiBucketsAggregation.Bucket> buckets = bucketsAggregation.getBuckets();

        //clone current line.
        List<Object> currentLine = lines.get(this.currentLineIndex);
        List<Object> clonedLine = new ArrayList<>(currentLine);

        //call handle_Agg with current_line++
        boolean firstLine = true;
        for (MultiBucketsAggregation.Bucket bucket : buckets) {
            //each bucket need to add new line with current line copied => except for first line
            String key = bucket.getKeyAsString();
            if (firstLine) {
                firstLine = false;
            } else {
                currentLineIndex++;
                currentLine = new ArrayList<Object>(clonedLine);
                lines.add(currentLine);
            }
            currentLine.add(key);
            handleAggregations(bucket.getAggregations(), headers, lines);

        }
    }

}
 
Example #17
Source File: CSVResultsExtractor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private  void handleAggregations(Aggregations aggregations, List<String> headers, List<List<String>> lines) throws CsvExtractorException {
    if(allNumericAggregations(aggregations)){
        lines.get(this.currentLineIndex).addAll(fillHeaderAndCreateLineForNumericAggregations(aggregations, headers));
        return;
    }
    //aggregations with size one only supported when not metrics.
    List<Aggregation> aggregationList = aggregations.asList();
    if(aggregationList.size() > 1){
        throw new CsvExtractorException("currently support only one aggregation at same level (Except for numeric metrics)");
    }
    Aggregation aggregation = aggregationList.get(0);
    //we want to skip singleBucketAggregations (nested,reverse_nested,filters)
    if(aggregation instanceof SingleBucketAggregation){
        Aggregations singleBucketAggs = ((SingleBucketAggregation) aggregation).getAggregations();
        handleAggregations(singleBucketAggs, headers, lines);
        return;
    }
    if(aggregation instanceof NumericMetricsAggregation){
        handleNumericMetricAggregation(headers, lines.get(currentLineIndex), aggregation);
        return;
    }
    if(aggregation instanceof GeoBounds){
        handleGeoBoundsAggregation(headers, lines, (GeoBounds) aggregation);
        return;
    }
    if(aggregation instanceof TopHits){
        //todo: handle this . it returns hits... maby back to normal?
        //todo: read about this usages
        // TopHits topHitsAggregation = (TopHits) aggregation;
    }
    if(aggregation instanceof MultiBucketsAggregation){
        MultiBucketsAggregation bucketsAggregation = (MultiBucketsAggregation) aggregation;
        String name = bucketsAggregation.getName();
        //checking because it can comes from sub aggregation again
        if(!headers.contains(name)){
            headers.add(name);
        }
        Collection<? extends MultiBucketsAggregation.Bucket> buckets = bucketsAggregation.getBuckets();

        //clone current line.
        List<String> currentLine = lines.get(this.currentLineIndex);
        List<String> clonedLine = new ArrayList<>(currentLine);

        //call handle_Agg with current_line++
        boolean firstLine = true;
        for (MultiBucketsAggregation.Bucket bucket : buckets) {
            //each bucket need to add new line with current line copied => except for first line
            String key = bucket.getKeyAsString();
            if(firstLine){
                firstLine = false;
            }
            else {
                currentLineIndex++;
                currentLine = new ArrayList<String>(clonedLine);
                lines.add(currentLine);
            }
            currentLine.add(key);
            handleAggregations(bucket.getAggregations(),headers,lines);

        }
    }

}
 
Example #18
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
Aggregation(String key, boolean asc) {
    super(ID, key, asc, new MultiBucketsAggregation.Bucket.SubAggregationComparator<InternalHistogram.Bucket>(key, asc));
}
 
Example #19
Source File: InternalOrder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
AggregationPath path() {
    return ((MultiBucketsAggregation.Bucket.SubAggregationComparator) comparator).path();
}
 
Example #20
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 votes vote down vote up
protected abstract TBucket createBucket(Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets); 
Example #21
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 votes vote down vote up
protected abstract TBucket createBucket(Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets); 
Example #22
Source File: KPIField.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 votes vote down vote up
Object extractValue(final String containingAggName, final MultiBucketsAggregation.Bucket bucket);