diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java index 3d4c02bc6fc..f0dfec2312f 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/date/InternalDateRange.java @@ -64,6 +64,14 @@ public class InternalDateRange extends InternalRange getFactory() { return FACTORY; @@ -109,8 +117,8 @@ public class InternalDateRange extends InternalRange> nodePlugins() { @@ -125,6 +130,7 @@ public class BucketScriptIT extends ESIntegTestCase { numDocs = randomIntBetween(10, 500); minNumber = -200; maxNumber = 200; + date = randomLong(); List builders = new ArrayList<>(); for (int docs = 0; docs < numDocs; docs++) { @@ -142,6 +148,7 @@ public class BucketScriptIT extends ESIntegTestCase { jsonBuilder.field(FIELD_2_NAME, randomIntBetween(minNumber, maxNumber)); jsonBuilder.field(FIELD_3_NAME, randomIntBetween(minNumber, maxNumber)); jsonBuilder.field(FIELD_4_NAME, randomIntBetween(minNumber, maxNumber)); + jsonBuilder.field(FIELD_5_NAME, date); jsonBuilder.endObject(); return jsonBuilder; } @@ -238,6 +245,52 @@ public class BucketScriptIT extends ESIntegTestCase { } } + public void testInlineScriptWithDateRange() { + SearchResponse response = client() + .prepareSearch("idx") + .addAggregation( + dateRange("range") + .field(FIELD_5_NAME) + .addUnboundedFrom(date) + .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) + .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) + .subAggregation(sum("field4Sum").field(FIELD_4_NAME)) + .subAggregation( + bucketScript("seriesArithmetic", + new Script("_value0 + _value1 + _value2", ScriptType.INLINE, CustomScriptPlugin.NAME, null) + , "field2Sum", "field3Sum", "field4Sum"))) + .execute().actionGet(); + + assertSearchResponse(response); + + Range range = response.getAggregations().get("range"); + assertThat(range, notNullValue()); + assertThat(range.getName(), equalTo("range")); + List buckets = range.getBuckets(); + + for (int i = 0; i < buckets.size(); ++i) { + Range.Bucket bucket = buckets.get(i); + if (bucket.getDocCount() == 0) { + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, nullValue()); + } else { + Sum field2Sum = bucket.getAggregations().get("field2Sum"); + assertThat(field2Sum, notNullValue()); + double field2SumValue = field2Sum.getValue(); + Sum field3Sum = bucket.getAggregations().get("field3Sum"); + assertThat(field3Sum, notNullValue()); + double field3SumValue = field3Sum.getValue(); + Sum field4Sum = bucket.getAggregations().get("field4Sum"); + assertThat(field4Sum, notNullValue()); + double field4SumValue = field4Sum.getValue(); + SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic"); + assertThat(seriesArithmetic, notNullValue()); + double seriesArithmeticValue = seriesArithmetic.value(); + assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue)); + } + } + } + public void testInlineScriptSingleVariable() { SearchResponse response = client() .prepareSearch("idx")