From e3f9824931c3546b78b5dfc33dfa22fa0f41fbca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 6 Nov 2015 17:04:43 +0100 Subject: [PATCH 1/3] Tests: Move DateHistogramTests back to core module DateHistogramTests had some dependency on groovy scripts and were moved to the lang-groovy module. This PR moves it back and replaces use of groovy scripts by a mock script engine. Removing three test cases that were testing doing some date manipulation using script, since these are more groovy script tests than testing the DateHistogram aggregation. --- .../elasticsearch/search/aggregations/bucket/DateHistogramIT.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DateHistogramTests.java => core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java (100%) diff --git a/plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DateHistogramTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java similarity index 100% rename from plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DateHistogramTests.java rename to core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java From 5108e6fd1ff5e9c6f43e5575a3eafc951a342cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 6 Nov 2015 17:10:47 +0100 Subject: [PATCH 2/3] Adapting test by using mock script engine --- .../aggregations/bucket/DateHistogramIT.java | 317 ++++++++---------- 1 file changed, 144 insertions(+), 173 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java index d65b469e6eb..ebdc4425785 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java @@ -16,8 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.messy.tests; +package org.elasticsearch.search.aggregations.bucket; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.search.Scorer; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; @@ -27,13 +29,21 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.script.CompiledScript; +import org.elasticsearch.script.ExecutableScript; +import org.elasticsearch.script.LeafSearchScript; import org.elasticsearch.script.Script; -import org.elasticsearch.script.groovy.GroovyPlugin; +import org.elasticsearch.script.ScriptEngineService; +import org.elasticsearch.script.ScriptModule; +import org.elasticsearch.script.SearchScript; +import org.elasticsearch.script.ScriptService.ScriptType; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket; import org.elasticsearch.search.aggregations.metrics.max.Max; import org.elasticsearch.search.aggregations.metrics.sum.Sum; +import org.elasticsearch.search.lookup.LeafSearchLookup; +import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.test.ESIntegTestCase; import org.hamcrest.Matchers; import org.joda.time.DateTime; @@ -45,8 +55,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -71,12 +81,7 @@ import static org.hamcrest.core.IsNull.notNullValue; * */ @ESIntegTestCase.SuiteScopeTestCase -public class DateHistogramTests extends ESIntegTestCase { - - @Override - protected Collection> nodePlugins() { - return Collections.singleton(GroovyPlugin.class); - } +public class DateHistogramIT extends ESIntegTestCase { private DateTime date(int month, int day) { return new DateTime(2012, month, day, 0, 0, DateTimeZone.UTC); @@ -132,6 +137,12 @@ public class DateHistogramTests extends ESIntegTestCase { ensureSearchable(); } + @Override + protected Collection> nodePlugins() { + return Arrays.asList( + ExtractFieldScriptPlugin.class); + } + @After public void afterEachTest() throws IOException { internalCluster().wipeIndices("idx2"); @@ -519,46 +530,6 @@ public class DateHistogramTests extends ESIntegTestCase { } } - public void testSingleValuedFieldWithValueScript() throws Exception { - SearchResponse response = client().prepareSearch("idx") - .addAggregation(dateHistogram("histo") - .field("date") - .script(new Script("new DateTime(_value).plusMonths(1).getMillis()")) - .interval(DateHistogramInterval.MONTH)).execute().actionGet(); - - assertSearchResponse(response); - - Histogram histo = response.getAggregations().get("histo"); - assertThat(histo, notNullValue()); - assertThat(histo.getName(), equalTo("histo")); - - List buckets = histo.getBuckets(); - assertThat(buckets.size(), equalTo(3)); - - DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC); - Histogram.Bucket bucket = buckets.get(0); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(1l)); - - key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(1); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(2l)); - - key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(2); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(3l)); - } - - - /* [ Jan 2, Feb 3] [ Feb 2, Mar 3] @@ -645,126 +616,6 @@ public class DateHistogramTests extends ESIntegTestCase { assertThat(bucket.getDocCount(), equalTo(1l)); } - /** - * The script will change to document date values to the following: - *

- * doc 1: [ Feb 2, Mar 3] - * doc 2: [ Mar 2, Apr 3] - * doc 3: [ Mar 15, Apr 16] - * doc 4: [ Apr 2, May 3] - * doc 5: [ Apr 15, May 16] - * doc 6: [ Apr 23, May 24] - */ - public void testMultiValuedFieldWithValueScript() throws Exception { - SearchResponse response = client().prepareSearch("idx") - .addAggregation(dateHistogram("histo") - .field("dates") - .script(new Script("new DateTime(_value, DateTimeZone.UTC).plusMonths(1).getMillis()")) - .interval(DateHistogramInterval.MONTH)).execute().actionGet(); - - assertSearchResponse(response); - - Histogram histo = response.getAggregations().get("histo"); - assertThat(histo, notNullValue()); - assertThat(histo.getName(), equalTo("histo")); - List buckets = histo.getBuckets(); - assertThat(buckets.size(), equalTo(4)); - - DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC); - Histogram.Bucket bucket = buckets.get(0); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(1l)); - - key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(1); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(3l)); - - key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(2); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(5l)); - - key = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(3); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(3l)); - } - - /** - * The script will change to document date values to the following: - *

- * doc 1: [ Feb 2, Mar 3] - * doc 2: [ Mar 2, Apr 3] - * doc 3: [ Mar 15, Apr 16] - * doc 4: [ Apr 2, May 3] - * doc 5: [ Apr 15, May 16] - * doc 6: [ Apr 23, May 24] - */ - public void testMultiValuedFieldWithValueScriptWithInheritedSubAggregator() throws Exception { - SearchResponse response = client().prepareSearch("idx") - .addAggregation(dateHistogram("histo") - .field("dates") - .script(new Script("new DateTime((long)_value, DateTimeZone.UTC).plusMonths(1).getMillis()")) - .interval(DateHistogramInterval.MONTH).subAggregation(max("max"))).execute().actionGet(); - - assertSearchResponse(response); - - Histogram histo = response.getAggregations().get("histo"); - assertThat(histo, notNullValue()); - assertThat(histo.getName(), equalTo("histo")); - List buckets = histo.getBuckets(); - assertThat(buckets.size(), equalTo(4)); - - DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC); - Histogram.Bucket bucket = buckets.get(0); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(1l)); - Max max = bucket.getAggregations().get("max"); - assertThat(max, notNullValue()); - assertThat((long) max.getValue(), equalTo(new DateTime(2012, 3, 3, 0, 0, DateTimeZone.UTC).getMillis())); - - key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(1); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(3l)); - max = bucket.getAggregations().get("max"); - assertThat(max, notNullValue()); - assertThat((long) max.getValue(), equalTo(new DateTime(2012, 4, 16, 0, 0, DateTimeZone.UTC).getMillis())); - - key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(2); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(5l)); - max = bucket.getAggregations().get("max"); - assertThat(max, notNullValue()); - assertThat((long) max.getValue(), equalTo(new DateTime(2012, 5, 24, 0, 0, DateTimeZone.UTC).getMillis())); - - key = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC); - bucket = buckets.get(3); - assertThat(bucket, notNullValue()); - assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); - assertThat(((DateTime) bucket.getKey()), equalTo(key)); - assertThat(bucket.getDocCount(), equalTo(3l)); - max = bucket.getAggregations().get("max"); - assertThat(max, notNullValue()); - assertThat((long) max.getValue(), equalTo(new DateTime(2012, 5, 24, 0, 0, DateTimeZone.UTC).getMillis())); - } - /** * Jan 2 * Feb 2 @@ -775,7 +626,7 @@ public class DateHistogramTests extends ESIntegTestCase { */ public void testScriptSingleValue() throws Exception { SearchResponse response = client().prepareSearch("idx") - .addAggregation(dateHistogram("histo").script(new Script("doc['date'].value")).interval(DateHistogramInterval.MONTH)) + .addAggregation(dateHistogram("histo").script(new Script("date", ScriptType.INLINE, ExtractFieldScriptEngine.NAME, null)).interval(DateHistogramInterval.MONTH)) .execute().actionGet(); assertSearchResponse(response); @@ -811,7 +662,7 @@ public class DateHistogramTests extends ESIntegTestCase { public void testScriptSingleValueWithSubAggregatorInherited() throws Exception { SearchResponse response = client().prepareSearch("idx") .addAggregation(dateHistogram("histo") - .script(new Script("doc['date'].value")).interval(DateHistogramInterval.MONTH) + .script(new Script("date", ScriptType.INLINE, ExtractFieldScriptEngine.NAME, null)).interval(DateHistogramInterval.MONTH) .subAggregation(max("max"))).execute().actionGet(); assertSearchResponse(response); @@ -855,7 +706,7 @@ public class DateHistogramTests extends ESIntegTestCase { public void testScriptMultiValued() throws Exception { SearchResponse response = client().prepareSearch("idx") - .addAggregation(dateHistogram("histo").script(new Script("doc['dates'].values")).interval(DateHistogramInterval.MONTH)) + .addAggregation(dateHistogram("histo").script(new Script("dates", ScriptType.INLINE, ExtractFieldScriptEngine.NAME, null)).interval(DateHistogramInterval.MONTH)) .execute().actionGet(); assertSearchResponse(response); @@ -909,7 +760,7 @@ public class DateHistogramTests extends ESIntegTestCase { public void testScriptMultiValuedWithAggregatorInherited() throws Exception { SearchResponse response = client().prepareSearch("idx") .addAggregation(dateHistogram("histo") - .script(new Script("doc['dates'].values")).interval(DateHistogramInterval.MONTH) + .script(new Script("dates", ScriptType.INLINE, ExtractFieldScriptEngine.NAME, null)).interval(DateHistogramInterval.MONTH) .subAggregation(max("max"))).execute().actionGet(); assertSearchResponse(response); @@ -1369,4 +1220,124 @@ public class DateHistogramTests extends ESIntegTestCase { Histogram histo = response.getAggregations().get("histo"); assertThat(histo.getBuckets().size(), greaterThan(0)); } + + public static class ExtractFieldScriptPlugin extends Plugin { + + @Override + public String name() { + return ExtractFieldScriptEngine.NAME; + } + + @Override + public String description() { + return "Mock script engine for " + DateHistogramIT.class; + } + + public void onModule(ScriptModule module) { + module.addScriptEngine(ExtractFieldScriptEngine.class); + } + + } + + public static class ExtractFieldScriptEngine implements ScriptEngineService { + + public static final String NAME = "extract_field"; + + @Override + public void close() throws IOException { + } + + @Override + public String[] types() { + return new String[] { NAME }; + } + + @Override + public String[] extensions() { + return types(); + } + + @Override + public boolean sandboxed() { + return true; + } + + @Override + public Object compile(String script) { + return script; + } + + @Override + public ExecutableScript executable(CompiledScript compiledScript, Map params) { + throw new UnsupportedOperationException(); + } + @Override + public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, Map vars) { + return new SearchScript() { + + @Override + public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException { + + final LeafSearchLookup leafLookup = lookup.getLeafSearchLookup(context); + + return new LeafSearchScript() { + + @Override + public Object unwrap(Object value) { + return null; + } + + @Override + public void setNextVar(String name, Object value) { + } + + @Override + public Object run() { + String fieldName = (String) compiledScript.compiled(); + return leafLookup.doc().get(fieldName); + } + + @Override + public void setScorer(Scorer scorer) { + } + + @Override + public void setSource(Map source) { + } + + @Override + public void setDocument(int doc) { + if (leafLookup != null) { + leafLookup.setDocument(doc); + } + } + + @Override + public long runAsLong() { + return 0; + } + + @Override + public float runAsFloat() { + return 0; + } + + @Override + public double runAsDouble() { + return 0; + } + }; + } + + @Override + public boolean needsScores() { + return false; + } + }; + } + + @Override + public void scriptRemoved(CompiledScript script) { + } + } } From b377d81e1cab84db5592bc603204eda770ce8547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Mon, 16 Nov 2015 13:00:43 +0100 Subject: [PATCH 3/3] Adding mock script engine for value script tests --- .../aggregations/bucket/DateHistogramIT.java | 302 +++++++++++++++++- 1 file changed, 298 insertions(+), 4 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java index ebdc4425785..9a1d498ad6b 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/DateHistogramIT.java @@ -55,6 +55,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; @@ -140,7 +141,8 @@ public class DateHistogramIT extends ESIntegTestCase { @Override protected Collection> nodePlugins() { return Arrays.asList( - ExtractFieldScriptPlugin.class); + ExtractFieldScriptPlugin.class, + FieldValueScriptPlugin.class); } @After @@ -530,6 +532,44 @@ public class DateHistogramIT extends ESIntegTestCase { } } + public void testSingleValuedFieldWithValueScript() throws Exception { + SearchResponse response = client().prepareSearch("idx") + .addAggregation(dateHistogram("histo") + .field("date") + .script(new Script("", ScriptType.INLINE, FieldValueScriptEngine.NAME, null)) + .interval(DateHistogramInterval.MONTH)).execute().actionGet(); + + assertSearchResponse(response); + + Histogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + + List buckets = histo.getBuckets(); + assertThat(buckets.size(), equalTo(3)); + + DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC); + Histogram.Bucket bucket = buckets.get(0); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(1l)); + + key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(1); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(2l)); + + key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(2); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(3l)); + } + /* [ Jan 2, Feb 3] [ Feb 2, Mar 3] @@ -616,6 +656,126 @@ public class DateHistogramIT extends ESIntegTestCase { assertThat(bucket.getDocCount(), equalTo(1l)); } + /** + * The script will change to document date values to the following: + *

+ * doc 1: [ Feb 2, Mar 3] + * doc 2: [ Mar 2, Apr 3] + * doc 3: [ Mar 15, Apr 16] + * doc 4: [ Apr 2, May 3] + * doc 5: [ Apr 15, May 16] + * doc 6: [ Apr 23, May 24] + */ + public void testMultiValuedFieldWithValueScript() throws Exception { + SearchResponse response = client().prepareSearch("idx") + .addAggregation(dateHistogram("histo") + .field("dates") + .script(new Script("", ScriptType.INLINE, FieldValueScriptEngine.NAME, null)) + .interval(DateHistogramInterval.MONTH)).execute().actionGet(); + + assertSearchResponse(response); + + Histogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + assertThat(buckets.size(), equalTo(4)); + + DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC); + Histogram.Bucket bucket = buckets.get(0); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(1l)); + + key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(1); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(3l)); + + key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(2); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(5l)); + + key = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(3); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(3l)); + } + + /** + * The script will change to document date values to the following: + *

+ * doc 1: [ Feb 2, Mar 3] + * doc 2: [ Mar 2, Apr 3] + * doc 3: [ Mar 15, Apr 16] + * doc 4: [ Apr 2, May 3] + * doc 5: [ Apr 15, May 16] + * doc 6: [ Apr 23, May 24] + */ + public void testMultiValuedFieldWithValueScriptWithInheritedSubAggregator() throws Exception { + SearchResponse response = client().prepareSearch("idx") + .addAggregation(dateHistogram("histo") + .field("dates") + .script(new Script("", ScriptType.INLINE, FieldValueScriptEngine.NAME, null)) + .interval(DateHistogramInterval.MONTH).subAggregation(max("max"))).execute().actionGet(); + + assertSearchResponse(response); + + Histogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + assertThat(buckets.size(), equalTo(4)); + + DateTime key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC); + Histogram.Bucket bucket = buckets.get(0); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(1l)); + Max max = bucket.getAggregations().get("max"); + assertThat(max, notNullValue()); + assertThat((long) max.getValue(), equalTo(new DateTime(2012, 3, 3, 0, 0, DateTimeZone.UTC).getMillis())); + + key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(1); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(3l)); + max = bucket.getAggregations().get("max"); + assertThat(max, notNullValue()); + assertThat((long) max.getValue(), equalTo(new DateTime(2012, 4, 16, 0, 0, DateTimeZone.UTC).getMillis())); + + key = new DateTime(2012, 4, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(2); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(5l)); + max = bucket.getAggregations().get("max"); + assertThat(max, notNullValue()); + assertThat((long) max.getValue(), equalTo(new DateTime(2012, 5, 24, 0, 0, DateTimeZone.UTC).getMillis())); + + key = new DateTime(2012, 5, 1, 0, 0, DateTimeZone.UTC); + bucket = buckets.get(3); + assertThat(bucket, notNullValue()); + assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key))); + assertThat(((DateTime) bucket.getKey()), equalTo(key)); + assertThat(bucket.getDocCount(), equalTo(3l)); + max = bucket.getAggregations().get("max"); + assertThat(max, notNullValue()); + assertThat((long) max.getValue(), equalTo(new DateTime(2012, 5, 24, 0, 0, DateTimeZone.UTC).getMillis())); + } + /** * Jan 2 * Feb 2 @@ -1221,6 +1381,9 @@ public class DateHistogramIT extends ESIntegTestCase { assertThat(histo.getBuckets().size(), greaterThan(0)); } + /** + * Mock plugin for the {@link ExtractFieldScriptEngine} + */ public static class ExtractFieldScriptPlugin extends Plugin { @Override @@ -1239,6 +1402,9 @@ public class DateHistogramIT extends ESIntegTestCase { } + /** + * This mock script returns the field that is specified by name in the script body + */ public static class ExtractFieldScriptEngine implements ScriptEngineService { public static final String NAME = "extract_field"; @@ -1314,17 +1480,145 @@ public class DateHistogramIT extends ESIntegTestCase { @Override public long runAsLong() { - return 0; + throw new UnsupportedOperationException(); } @Override public float runAsFloat() { - return 0; + throw new UnsupportedOperationException(); } @Override public double runAsDouble() { - return 0; + throw new UnsupportedOperationException(); + } + }; + } + + @Override + public boolean needsScores() { + return false; + } + }; + } + + @Override + public void scriptRemoved(CompiledScript script) { + } + } + + /** + * Mock plugin for the {@link FieldValueScriptEngine} + */ + public static class FieldValueScriptPlugin extends Plugin { + + @Override + public String name() { + return FieldValueScriptEngine.NAME; + } + + @Override + public String description() { + return "Mock script engine for " + DateHistogramIT.class; + } + + public void onModule(ScriptModule module) { + module.addScriptEngine(FieldValueScriptEngine.class); + } + + } + + /** + * This mock script returns the field value and adds one month to the returned date + */ + public static class FieldValueScriptEngine implements ScriptEngineService { + + public static final String NAME = "field_value"; + + @Override + public void close() throws IOException { + } + + @Override + public String[] types() { + return new String[] { NAME }; + } + + @Override + public String[] extensions() { + return types(); + } + + @Override + public boolean sandboxed() { + return true; + } + + @Override + public Object compile(String script) { + return script; + } + + @Override + public ExecutableScript executable(CompiledScript compiledScript, Map params) { + throw new UnsupportedOperationException(); + } + @Override + public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, Map vars) { + return new SearchScript() { + + private Map vars = new HashMap<>(2); + + @Override + public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException { + + final LeafSearchLookup leafLookup = lookup.getLeafSearchLookup(context); + + return new LeafSearchScript() { + + @Override + public Object unwrap(Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public void setNextVar(String name, Object value) { + vars.put(name, value); + } + + @Override + public Object run() { + throw new UnsupportedOperationException(); + } + + @Override + public void setScorer(Scorer scorer) { + } + + @Override + public void setSource(Map source) { + } + + @Override + public void setDocument(int doc) { + if (leafLookup != null) { + leafLookup.setDocument(doc); + } + } + + @Override + public long runAsLong() { + return new DateTime((long) vars.get("_value"), DateTimeZone.UTC).plusMonths(1).getMillis(); + } + + @Override + public float runAsFloat() { + throw new UnsupportedOperationException(); + } + + @Override + public double runAsDouble() { + return new DateTime(new Double((double) vars.get("_value")).longValue(), DateTimeZone.UTC).plusMonths(1).getMillis(); } }; }