org.elasticsearch.search.aggregations.metrics.TopHits Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.TopHits. 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: TransportConnection.java    From heroic with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public Stream<Pair<String, SearchHits>> parseHits(@NotNull StringTerms terms) {
    return terms.getBuckets()
        .stream()
        .map(bucket -> {
            TopHits hits = bucket.getAggregations().get("hits");
            return new Pair<>(bucket.getKeyAsString(), hits.getHits());
        });
}
 
Example #2
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 #3
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);

        }
    }

}