From d9bc2693a3b10830d6b8bb26286b24b08aed6e88 Mon Sep 17 00:00:00 2001 From: Thiago Souza Date: Thu, 18 Aug 2016 00:26:37 -0300 Subject: [PATCH 1/3] Use internal from/to when creating InternalDateRange.Bucket InternalDateRange.Factory.createBucket should use prototype's internal from/to Fixes https://github.com/elastic/elasticsearch/issues/19364 --- .../bucket/range/date/InternalDateRange.java | 12 ++++- .../aggregations/pipeline/BucketScriptIT.java | 51 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) 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 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") From 2ba508a7614285e827ca5c4be80aa69ef7fe6e4b Mon Sep 17 00:00:00 2001 From: Thiago Souza Date: Fri, 19 Aug 2016 13:53:15 -0300 Subject: [PATCH 2/3] Use a better name for unit test method --- .../search/aggregations/pipeline/BucketScriptIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java index 922e1ae5ebf..09bb6742c59 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java @@ -243,7 +243,7 @@ public class BucketScriptIT extends ESIntegTestCase { } } - public void testInlineScript3() { + public void testInlineScriptWithDateRange() { SearchResponse response = client() .prepareSearch("idx") .addAggregation( From 9ea3f4ace303e4465aa31c50207531face914ae2 Mon Sep 17 00:00:00 2001 From: Thiago Souza Date: Fri, 19 Aug 2016 14:09:15 -0300 Subject: [PATCH 3/3] Use supported random methods instead of DateTime.now() --- .../search/aggregations/pipeline/BucketScriptIT.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java index 09bb6742c59..101e52fcadb 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptIT.java @@ -67,6 +67,7 @@ public class BucketScriptIT extends ESIntegTestCase { private static int numDocs; private static int minNumber; private static int maxNumber; + private static long date; @Override protected Collection> nodePlugins() { @@ -129,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++) { @@ -146,7 +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, DateTime.now()); + jsonBuilder.field(FIELD_5_NAME, date); jsonBuilder.endObject(); return jsonBuilder; } @@ -249,7 +251,7 @@ public class BucketScriptIT extends ESIntegTestCase { .addAggregation( dateRange("range") .field(FIELD_5_NAME) - .addRange("now-1d", "now") + .addUnboundedFrom(date) .subAggregation(sum("field2Sum").field(FIELD_2_NAME)) .subAggregation(sum("field3Sum").field(FIELD_3_NAME)) .subAggregation(sum("field4Sum").field(FIELD_4_NAME))