use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.
the class InternalSingleBucketAggregationTestCase method createTestInstance.
@Override
protected final T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
List<InternalAggregation> internal = new ArrayList<>();
if (hasInternalMax) {
internal.add(new InternalMax("max", randomDouble(), randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(), emptyMap()));
}
if (hasInternalMin) {
internal.add(new InternalMin("min", randomDouble(), randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(), emptyMap()));
}
// we shouldn't use the full long range here since we sum doc count on reduce, and don't want to overflow the long range there
long docCount = between(0, Integer.MAX_VALUE);
return createTestInstance(name, docCount, new InternalAggregations(internal), pipelineAggregators, metaData);
}
use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.
the class SignificanceHeuristicTests method createInternalAggregations.
// Create aggregations as they might come from three different shards and return as list.
private List<InternalAggregation> createInternalAggregations() {
SignificanceHeuristic significanceHeuristic = getRandomSignificanceheuristic();
TestAggFactory<?, ?> factory = randomBoolean() ? new StringTestAggFactory() : new LongTestAggFactory();
List<InternalAggregation> aggs = new ArrayList<>();
aggs.add(factory.createAggregation(significanceHeuristic, 4, 10, 1, (f, i) -> f.createBucket(4, 4, 5, 10, 0)));
aggs.add(factory.createAggregation(significanceHeuristic, 4, 10, 1, (f, i) -> f.createBucket(4, 4, 5, 10, 1)));
aggs.add(factory.createAggregation(significanceHeuristic, 8, 10, 2, (f, i) -> f.createBucket(4, 4, 5, 10, i)));
return aggs;
}
use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.
the class TermsAggregatorTests method testMixLongAndDouble.
public void testMixLongAndDouble() throws Exception {
for (TermsAggregatorFactory.ExecutionMode executionMode : TermsAggregatorFactory.ExecutionMode.values()) {
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name", ValueType.LONG).executionHint(executionMode.toString()).field("number").order(Terms.Order.term(true));
List<InternalAggregation> aggs = new ArrayList<>();
int numLongs = randomIntBetween(1, 3);
for (int i = 0; i < numLongs; i++) {
final Directory dir;
try (IndexReader reader = createIndexWithLongs()) {
dir = ((DirectoryReader) reader).directory();
IndexSearcher searcher = new IndexSearcher(reader);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName("number");
fieldType.setHasDocValues(true);
aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher));
}
dir.close();
}
int numDoubles = randomIntBetween(1, 3);
for (int i = 0; i < numDoubles; i++) {
final Directory dir;
try (IndexReader reader = createIndexWithDoubles()) {
dir = ((DirectoryReader) reader).directory();
IndexSearcher searcher = new IndexSearcher(reader);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
fieldType.setName("number");
fieldType.setHasDocValues(true);
aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher));
}
dir.close();
}
InternalAggregation.ReduceContext ctx = new InternalAggregation.ReduceContext(new MockBigArrays(Settings.EMPTY, new NoneCircuitBreakerService()), null, true);
for (InternalAggregation internalAgg : aggs) {
InternalAggregation mergedAggs = internalAgg.doReduce(aggs, ctx);
assertTrue(mergedAggs instanceof DoubleTerms);
long expected = numLongs + numDoubles;
List<Terms.Bucket> buckets = ((DoubleTerms) mergedAggs).getBuckets();
assertEquals(4, buckets.size());
assertEquals("1.0", buckets.get(0).getKeyAsString());
assertEquals(expected, buckets.get(0).getDocCount());
assertEquals("10.0", buckets.get(1).getKeyAsString());
assertEquals(expected * 2, buckets.get(1).getDocCount());
assertEquals("100.0", buckets.get(2).getKeyAsString());
assertEquals(expected * 2, buckets.get(2).getDocCount());
assertEquals("1000.0", buckets.get(3).getKeyAsString());
assertEquals(expected, buckets.get(3).getDocCount());
}
}
}
use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.
the class BucketScriptPipelineAggregator method reduce.
@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);
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");
}
final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
return (InternalAggregation) p;
}).collect(Collectors.toList());
aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter, new ArrayList<>(), metaData()));
InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), (InternalMultiBucketAggregation.InternalBucket) bucket);
newBuckets.add(newBucket);
}
}
}
return originalAgg.create(newBuckets);
}
use of org.elasticsearch.search.aggregations.InternalAggregation in project elasticsearch by elastic.
the class DoubleTerms method doReduce.
@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
boolean promoteToDouble = false;
for (InternalAggregation agg : aggregations) {
if (agg instanceof LongTerms && ((LongTerms) agg).format == DocValueFormat.RAW) {
/**
* this terms agg mixes longs and doubles, we must promote longs to doubles to make the internal aggs
* compatible
*/
promoteToDouble = true;
break;
}
}
if (promoteToDouble == false) {
return super.doReduce(aggregations, reduceContext);
}
List<InternalAggregation> newAggs = new ArrayList<>();
for (InternalAggregation agg : aggregations) {
if (agg instanceof LongTerms) {
DoubleTerms dTerms = LongTerms.convertLongTermsToDouble((LongTerms) agg, format);
newAggs.add(dTerms);
} else {
newAggs.add(agg);
}
}
return newAggs.get(0).doReduce(newAggs, reduceContext);
}
Aggregations