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

The following examples show how to use org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket. 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: BucketMetricsPipelineAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public final InternalAggregation doReduce(Aggregations aggregations, ReduceContext context) {
    preCollection();
    List<String> bucketsPath = AggregationPath.parse(bucketsPaths()[0]).getPathElementsAsStringList();
    for (Aggregation aggregation : aggregations) {
        if (aggregation.getName().equals(bucketsPath.get(0))) {
            bucketsPath = bucketsPath.subList(1, bucketsPath.size());
            InternalMultiBucketAggregation multiBucketsAgg = (InternalMultiBucketAggregation) aggregation;
            List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
            for (int i = 0; i < buckets.size(); i++) {
                Bucket bucket = buckets.get(i);
                Double bucketValue = BucketHelpers.resolveBucketValue(multiBucketsAgg, bucket, bucketsPath, gapPolicy);
                if (bucketValue != null && !Double.isNaN(bucketValue)) {
                    collectBucketValue(bucket.getKeyAsString(), bucketValue);
                }
            }
        }
    }
    return buildAggregation(Collections.EMPTY_LIST, metaData());
}
 
Example #2
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 #3
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public T buildTarget(Bucket bucket){
	T obj = ReflectUtils.newInstance(targetClass);
	BeanWrapper bw = newBeanWrapper(obj);
	bw.setPropertyValue(keyProperty, bucket.getKey());
	AggregationsResult ar = new AggregationsResult(bucket.getAggregations());
	this.fieldMappings.forEach((path, prop)->{
		if(prop instanceof BucketMappingObjectBuilder){
			BucketMappingObjectBuilder<?, ?> b = (BucketMappingObjectBuilder<?, ?>)prop;
			List<?> values = b.buildTargetList(ar);
			bw.setPropertyValue(path, values);
		}else{
			/*Object[] value = ar.getKeysByPath(path);
			SimpleSearchQueryBuilder.logger.info("values:"+ArrayUtils.toString(value));*/
			Object val = ar.getKeyByPath(path);
			bw.setPropertyValue(prop.toString(), val);
		}
	});
	return obj;
}
 
Example #4
Source File: BucketSelectorPipelineAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends Bucket> buckets = originalAgg.getBuckets();

    CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext, Collections.<String, String>emptyMap());
    List newBuckets = new ArrayList<>();
    for (Bucket bucket : buckets) {
        Map<String, Object> vars = new HashMap<>();
        if (script.getParams() != null) {
            vars.putAll(script.getParams());
        }
        for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) {
            String varName = entry.getKey();
            String bucketsPath = entry.getValue();
            Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy);
            vars.put(varName, value);
        }
        ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
        Object scriptReturnValue = executableScript.run();
        final boolean keepBucket;
        // TODO: WTF!!!!!
        if ("expression".equals(script.getLang())) {
            double scriptDoubleValue = (double) scriptReturnValue;
            keepBucket = scriptDoubleValue == 1.0;
        } else {
            keepBucket = (boolean) scriptReturnValue;
        }
        if (keepBucket) {
            newBuckets.add(bucket);
        }
    }
    return originalAgg.create(newBuckets);
}
 
Example #5
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 #6
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public <T> List<T> mapBuckets(String name, Class<T> clazz) {
	    	Aggregation agg = getAggregation(name);
	    	List<? extends Bucket> buckets = getBuckets(agg);
	    	List<T> datas = buckets.stream().map(bucket->{
	    		T target = ReflectUtils.newInstance(clazz);
//	    		ReflectUtils.copy(target, (Bucket)bucket);
	    		CopyUtils.copy(target, agg);
	    		return target;
	    	})
	    	.collect(Collectors.toList());
	    	return datas;
	    }
 
Example #7
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public List<T> buildTargetList(AggregationsResult aggResult){
	Assert.notNull(aggResult);
	List<? extends Bucket> buckets = aggResult.getBucketsByPath(key);
	if(buckets==null)
		return Collections.emptyList();
	return buckets.stream().map(b->mapping.buildTarget(b)).collect(Collectors.toList());
}
 
Example #8
Source File: BucketScriptPipelineAggregator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends Bucket> buckets = originalAgg.getBuckets();

    CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext, Collections.<String, String>emptyMap());
    List newBuckets = new ArrayList<>();
    for (Bucket bucket : buckets) {
        Map<String, Object> vars = new HashMap<>();
        if (script.getParams() != null) {
            vars.putAll(script.getParams());
        }
        boolean skipBucket = false;
        for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) {
            String varName = entry.getKey();
            String bucketsPath = entry.getValue();
            Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy);
            if (GapPolicy.SKIP == gapPolicy && (value == null || Double.isNaN(value))) {
                skipBucket = true;
                break;
            }
            vars.put(varName, value);
        }
        if (skipBucket) {
            newBuckets.add(bucket);
        } else {
            ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
            Object returned = executableScript.run();
            if (returned == null) {
                newBuckets.add(bucket);
            } else {
                if (!(returned instanceof Number)) {
                    throw new AggregationExecutionException("series_arithmetic script for reducer [" + name()
                            + "] must return a Number");
                }
                List<InternalAggregation> aggs = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(), FUNCTION));
                aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter,
                        new ArrayList<PipelineAggregator>(), metaData()));
                InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs),
                        (InternalMultiBucketAggregation.InternalBucket) bucket);
                newBuckets.add(newBucket);
            }
        }
    }
    return originalAgg.create(newBuckets);
}
 
Example #9
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public List<? extends Bucket> getBucketsByPath(String path) {
	this.checkAggs();
	Aggregation val = getAggregationByPath(path);
	return getBuckets(val);
}