Adding tests for `time_zone` parameter for date range aggregation
This commit is contained in:
parent
82d01e4315
commit
5ff413074a
|
@ -1486,7 +1486,6 @@
|
|||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]main[/\\]java[/\\]org[/\\]elasticsearch[/\\]script[/\\]groovy[/\\]GroovyScriptEngineService.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]BucketScriptTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]BulkTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]DateRangeTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]DoubleTermsTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]EquivalenceTests.java" checks="LineLength" />
|
||||
<suppress files="modules[/\\]lang-groovy[/\\]src[/\\]test[/\\]java[/\\]org[/\\]elasticsearch[/\\]messy[/\\]tests[/\\]FunctionScoreTests.java" checks="LineLength" />
|
||||
|
|
|
@ -18,8 +18,6 @@
|
|||
*/
|
||||
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.SearchResponse;
|
||||
import org.elasticsearch.common.joda.DateMathParser;
|
||||
|
@ -28,22 +26,13 @@ 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.ScriptEngineRegistry;
|
||||
import org.elasticsearch.script.ScriptEngineService;
|
||||
import org.elasticsearch.script.ScriptModule;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.SearchScript;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket;
|
||||
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;
|
||||
|
@ -55,10 +44,7 @@ import java.io.IOException;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -142,8 +128,8 @@ public class DateHistogramIT extends ESIntegTestCase {
|
|||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Arrays.asList(
|
||||
ExtractFieldScriptPlugin.class,
|
||||
FieldValueScriptPlugin.class);
|
||||
ScriptMocks.ExtractFieldScriptPlugin.class,
|
||||
ScriptMocks.FieldValueScriptPlugin.class);
|
||||
}
|
||||
|
||||
@After
|
||||
|
@ -469,7 +455,7 @@ public class DateHistogramIT extends ESIntegTestCase {
|
|||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateHistogram("histo")
|
||||
.field("date")
|
||||
.script(new Script("", ScriptType.INLINE, FieldValueScriptEngine.NAME, null))
|
||||
.script(new Script("", ScriptType.INLINE, ScriptMocks.FieldValueScriptEngine.NAME, null))
|
||||
.dateHistogramInterval(DateHistogramInterval.MONTH)).execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
@ -603,7 +589,7 @@ public class DateHistogramIT extends ESIntegTestCase {
|
|||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateHistogram("histo")
|
||||
.field("dates")
|
||||
.script(new Script("", ScriptType.INLINE, FieldValueScriptEngine.NAME, null))
|
||||
.script(new Script("", ScriptType.INLINE, ScriptMocks.FieldValueScriptEngine.NAME, null))
|
||||
.dateHistogramInterval(DateHistogramInterval.MONTH)).execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
@ -653,7 +639,7 @@ public class DateHistogramIT extends ESIntegTestCase {
|
|||
*/
|
||||
public void testScriptSingleValue() throws Exception {
|
||||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateHistogram("histo").script(new Script("date", ScriptType.INLINE, ExtractFieldScriptEngine.NAME, null)).dateHistogramInterval(DateHistogramInterval.MONTH))
|
||||
.addAggregation(dateHistogram("histo").script(new Script("date", ScriptType.INLINE, ScriptMocks.ExtractFieldScriptEngine.NAME, null)).dateHistogramInterval(DateHistogramInterval.MONTH))
|
||||
.execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
@ -688,7 +674,7 @@ public class DateHistogramIT extends ESIntegTestCase {
|
|||
|
||||
public void testScriptMultiValued() throws Exception {
|
||||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateHistogram("histo").script(new Script("dates", ScriptType.INLINE, ExtractFieldScriptEngine.NAME, null)).dateHistogramInterval(DateHistogramInterval.MONTH))
|
||||
.addAggregation(dateHistogram("histo").script(new Script("dates", ScriptType.INLINE, ScriptMocks.ExtractFieldScriptEngine.NAME, null)).dateHistogramInterval(DateHistogramInterval.MONTH))
|
||||
.execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
@ -1148,256 +1134,4 @@ public class DateHistogramIT extends ESIntegTestCase {
|
|||
Histogram histo = response.getAggregations().get("histo");
|
||||
assertThat(histo.getBuckets().size(), greaterThan(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock plugin for the {@link ExtractFieldScriptEngine}
|
||||
*/
|
||||
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(new ScriptEngineRegistry.ScriptEngineRegistration(ExtractFieldScriptEngine.class, ExtractFieldScriptEngine.TYPES));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
public static final List<String> TYPES = Collections.singletonList(NAME);
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTypes() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExtensions() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSandboxed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script, Map<String, String> params) {
|
||||
return script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@Override
|
||||
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, Map<String, Object> vars) {
|
||||
return new SearchScript() {
|
||||
|
||||
@Override
|
||||
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
|
||||
|
||||
final LeafSearchLookup leafLookup = lookup.getLeafSearchLookup(context);
|
||||
|
||||
return new LeafSearchScript() {
|
||||
@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<String, Object> source) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocument(int doc) {
|
||||
if (leafLookup != null) {
|
||||
leafLookup.setDocument(doc);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
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(new ScriptEngineRegistry.ScriptEngineRegistration(FieldValueScriptEngine.class, FieldValueScriptEngine.TYPES));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
public static final List<String> TYPES = Collections.singletonList(NAME);
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTypes() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExtensions() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSandboxed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script, Map<String, String> params) {
|
||||
return script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@Override
|
||||
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, Map<String, Object> vars) {
|
||||
return new SearchScript() {
|
||||
|
||||
private Map<String, Object> 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<String, Object> 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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsScores() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scriptRemoved(CompiledScript script) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,13 +16,13 @@
|
|||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.elasticsearch.messy.tests;
|
||||
package org.elasticsearch.search.aggregations.bucket;
|
||||
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.groovy.GroovyPlugin;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.Range.Bucket;
|
||||
|
@ -36,7 +36,6 @@ import org.joda.time.DateTimeZone;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
|
@ -55,12 +54,7 @@ import static org.hamcrest.core.IsNull.nullValue;
|
|||
*
|
||||
*/
|
||||
@ESIntegTestCase.SuiteScopeTestCase
|
||||
public class DateRangeTests extends ESIntegTestCase {
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(GroovyPlugin.class);
|
||||
}
|
||||
public class DateRangeIT extends ESIntegTestCase {
|
||||
|
||||
private static IndexRequestBuilder indexDoc(int month, int day, int value) throws Exception {
|
||||
return client().prepareIndex("idx", "type").setSource(jsonBuilder()
|
||||
|
@ -107,18 +101,25 @@ public class DateRangeTests extends ESIntegTestCase {
|
|||
ensureSearchable();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Arrays.asList(
|
||||
ScriptMocks.ExtractFieldScriptPlugin.class,
|
||||
ScriptMocks.FieldValueScriptPlugin.class);
|
||||
}
|
||||
|
||||
public void testDateMath() throws Exception {
|
||||
DateRangeAggregatorBuilder rangeBuilder = dateRange("range");
|
||||
if (randomBoolean()) {
|
||||
rangeBuilder.field("date");
|
||||
} else {
|
||||
rangeBuilder.script(new Script("doc['date'].value"));
|
||||
rangeBuilder.script(new Script("date", ScriptType.INLINE, ScriptMocks.ExtractFieldScriptEngine.NAME, null));
|
||||
}
|
||||
SearchResponse response = client()
|
||||
.prepareSearch("idx")
|
||||
.addAggregation(
|
||||
rangeBuilder.addUnboundedTo("a long time ago", "now-50y").addRange("recently", "now-50y", "now-1y")
|
||||
.addUnboundedFrom("last year", "now-1y")).execute().actionGet();
|
||||
.addUnboundedFrom("last year", "now-1y").timeZone(DateTimeZone.forID("EST"))).execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
|
@ -286,17 +287,22 @@ public class DateRangeTests extends ESIntegTestCase {
|
|||
}
|
||||
|
||||
public void testSingleValueFieldWithDateMath() throws Exception {
|
||||
int timeZoneOffset = randomIntBetween(-12, 12);
|
||||
DateTimeZone timezone = DateTimeZone.forOffsetHours(timeZoneOffset);
|
||||
String timeZoneSuffix = (timeZoneOffset == 0) ? "Z" : DateTime.now(timezone).toString("ZZ");
|
||||
long expectedFirstBucketCount = timeZoneOffset < 0 ? 3L : 2L;
|
||||
|
||||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateRange("range")
|
||||
.field("date")
|
||||
.addUnboundedTo("2012-02-15")
|
||||
.addRange("2012-02-15", "2012-02-15||+1M")
|
||||
.addUnboundedFrom("2012-02-15||+1M"))
|
||||
.addUnboundedFrom("2012-02-15||+1M")
|
||||
.timeZone(timezone))
|
||||
.execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
||||
|
||||
Range range = response.getAggregations().get("range");
|
||||
assertThat(range, notNullValue());
|
||||
assertThat(range.getName(), equalTo("range"));
|
||||
|
@ -305,30 +311,31 @@ public class DateRangeTests extends ESIntegTestCase {
|
|||
|
||||
Range.Bucket bucket = buckets.get(0);
|
||||
assertThat(bucket, notNullValue());
|
||||
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000Z"));
|
||||
assertThat((String) bucket.getKey(), equalTo("*-2012-02-15T00:00:00.000" + timeZoneSuffix));
|
||||
assertThat(((DateTime) bucket.getFrom()), nullValue());
|
||||
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15)));
|
||||
assertThat(((DateTime) bucket.getTo()), equalTo(date(2, 15).minusHours(timeZoneOffset)));
|
||||
assertThat(bucket.getFromAsString(), nullValue());
|
||||
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000Z"));
|
||||
assertThat(bucket.getDocCount(), equalTo(2L));
|
||||
assertThat(bucket.getToAsString(), equalTo("2012-02-15T00:00:00.000" + timeZoneSuffix));
|
||||
assertThat(bucket.getDocCount(), equalTo(expectedFirstBucketCount));
|
||||
|
||||
bucket = buckets.get(1);
|
||||
assertThat(bucket, notNullValue());
|
||||
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000Z-2012-03-15T00:00:00.000Z"));
|
||||
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15)));
|
||||
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15)));
|
||||
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000Z"));
|
||||
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000Z"));
|
||||
assertThat((String) bucket.getKey(), equalTo("2012-02-15T00:00:00.000" + timeZoneSuffix +
|
||||
"-2012-03-15T00:00:00.000" + timeZoneSuffix));
|
||||
assertThat(((DateTime) bucket.getFrom()), equalTo(date(2, 15).minusHours(timeZoneOffset)));
|
||||
assertThat(((DateTime) bucket.getTo()), equalTo(date(3, 15).minusHours(timeZoneOffset)));
|
||||
assertThat(bucket.getFromAsString(), equalTo("2012-02-15T00:00:00.000" + timeZoneSuffix));
|
||||
assertThat(bucket.getToAsString(), equalTo("2012-03-15T00:00:00.000" + timeZoneSuffix));
|
||||
assertThat(bucket.getDocCount(), equalTo(2L));
|
||||
|
||||
bucket = buckets.get(2);
|
||||
assertThat(bucket, notNullValue());
|
||||
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000Z-*"));
|
||||
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15)));
|
||||
assertThat((String) bucket.getKey(), equalTo("2012-03-15T00:00:00.000" + timeZoneSuffix + "-*"));
|
||||
assertThat(((DateTime) bucket.getFrom()), equalTo(date(3, 15).minusHours(timeZoneOffset)));
|
||||
assertThat(((DateTime) bucket.getTo()), nullValue());
|
||||
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000Z"));
|
||||
assertThat(bucket.getFromAsString(), equalTo("2012-03-15T00:00:00.000" + timeZoneSuffix));
|
||||
assertThat(bucket.getToAsString(), nullValue());
|
||||
assertThat(bucket.getDocCount(), equalTo(numDocs - 4L));
|
||||
assertThat(bucket.getDocCount(), equalTo(numDocs - 2L - expectedFirstBucketCount));
|
||||
}
|
||||
|
||||
public void testSingleValueFieldWithCustomKey() throws Exception {
|
||||
|
@ -523,7 +530,7 @@ public class DateRangeTests extends ESIntegTestCase {
|
|||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateRange("range")
|
||||
.field("dates")
|
||||
.script(new Script("new DateTime(_value.longValue(), DateTimeZone.UTC).plusMonths(1).getMillis()"))
|
||||
.script(new Script("", ScriptType.INLINE, ScriptMocks.FieldValueScriptEngine.NAME, null))
|
||||
.addUnboundedTo(date(2, 15)).addRange(date(2, 15), date(3, 15)).addUnboundedFrom(date(3, 15))).execute()
|
||||
.actionGet();
|
||||
|
||||
|
@ -577,7 +584,7 @@ public class DateRangeTests extends ESIntegTestCase {
|
|||
public void testScriptSingleValue() throws Exception {
|
||||
SearchResponse response = client().prepareSearch("idx")
|
||||
.addAggregation(dateRange("range")
|
||||
.script(new Script("doc['date'].value"))
|
||||
.script(new Script("date", ScriptType.INLINE, ScriptMocks.ExtractFieldScriptEngine.NAME, null))
|
||||
.addUnboundedTo(date(2, 15))
|
||||
.addRange(date(2, 15), date(3, 15))
|
||||
.addUnboundedFrom(date(3, 15)))
|
||||
|
@ -637,8 +644,9 @@ public class DateRangeTests extends ESIntegTestCase {
|
|||
SearchResponse response = client()
|
||||
.prepareSearch("idx")
|
||||
.addAggregation(
|
||||
dateRange("range").script(new Script("doc['dates'].values")).addUnboundedTo(date(2, 15))
|
||||
.addRange(date(2, 15), date(3, 15)).addUnboundedFrom(date(3, 15))).execute().actionGet();
|
||||
dateRange("range").script(new Script("dates", ScriptType.INLINE, ScriptMocks.ExtractFieldScriptEngine.NAME, null))
|
||||
.addUnboundedTo(date(2, 15)).addRange(date(2, 15), date(3, 15))
|
||||
.addUnboundedFrom(date(3, 15))).execute().actionGet();
|
||||
|
||||
assertSearchResponse(response);
|
||||
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.search.aggregations.bucket;
|
|||
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregator.Range;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregatorBuilder;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
public class DateRangeTests extends BaseAggregationTestCase<DateRangeAggregatorBuilder> {
|
||||
|
||||
|
@ -56,6 +57,9 @@ public class DateRangeTests extends BaseAggregationTestCase<DateRangeAggregatorB
|
|||
if (randomBoolean()) {
|
||||
factory.missing(randomIntBetween(0, 10));
|
||||
}
|
||||
if (randomBoolean()) {
|
||||
factory.timeZone(DateTimeZone.forOffsetHours(randomIntBetween(-23, 23)));
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,303 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.search.aggregations.bucket;
|
||||
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
import org.apache.lucene.search.Scorer;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.CompiledScript;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.LeafSearchScript;
|
||||
import org.elasticsearch.script.ScriptEngineRegistry;
|
||||
import org.elasticsearch.script.ScriptEngineService;
|
||||
import org.elasticsearch.script.ScriptModule;
|
||||
import org.elasticsearch.script.SearchScript;
|
||||
import org.elasticsearch.search.lookup.LeafSearchLookup;
|
||||
import org.elasticsearch.search.lookup.SearchLookup;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helper mocks for script plugins and engines
|
||||
*/
|
||||
public class ScriptMocks {
|
||||
|
||||
/**
|
||||
* Mock plugin for the {@link ScriptMocks.ExtractFieldScriptEngine}
|
||||
*/
|
||||
public static class ExtractFieldScriptPlugin extends Plugin {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return ScriptMocks.ExtractFieldScriptEngine.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Mock script engine for " + DateHistogramIT.class;
|
||||
}
|
||||
|
||||
public void onModule(ScriptModule module) {
|
||||
module.addScriptEngine(new ScriptEngineRegistry.ScriptEngineRegistration(ScriptMocks.ExtractFieldScriptEngine.class,
|
||||
ScriptMocks.ExtractFieldScriptEngine.TYPES));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
public static final List<String> TYPES = Collections.singletonList(NAME);
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTypes() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExtensions() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSandboxed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script, Map<String, String> params) {
|
||||
return script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@Override
|
||||
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, Map<String, Object> vars) {
|
||||
return new SearchScript() {
|
||||
|
||||
@Override
|
||||
public LeafSearchScript getLeafSearchScript(LeafReaderContext context) throws IOException {
|
||||
|
||||
final LeafSearchLookup leafLookup = lookup.getLeafSearchLookup(context);
|
||||
|
||||
return new LeafSearchScript() {
|
||||
@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<String, Object> source) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocument(int doc) {
|
||||
if (leafLookup != null) {
|
||||
leafLookup.setDocument(doc);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long runAsLong() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float runAsFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double runAsDouble() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsScores() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scriptRemoved(CompiledScript script) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock plugin for the {@link ScriptMocks.FieldValueScriptEngine}
|
||||
*/
|
||||
public static class FieldValueScriptPlugin extends Plugin {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return ScriptMocks.FieldValueScriptEngine.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Mock script engine for " + DateHistogramIT.class;
|
||||
}
|
||||
|
||||
public void onModule(ScriptModule module) {
|
||||
module.addScriptEngine(new ScriptEngineRegistry.ScriptEngineRegistration(ScriptMocks.FieldValueScriptEngine.class,
|
||||
ScriptMocks.FieldValueScriptEngine.TYPES));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
|
||||
public static final List<String> TYPES = Collections.singletonList(NAME);
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTypes() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExtensions() {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSandboxed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object compile(String script, Map<String, String> params) {
|
||||
return script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@Override
|
||||
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, Map<String, Object> vars) {
|
||||
return new SearchScript() {
|
||||
|
||||
private Map<String, Object> 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<String, Object> 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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsScores() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scriptRemoved(CompiledScript script) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -39,7 +39,6 @@ import org.elasticsearch.index.IndexSettings;
|
|||
import org.elasticsearch.index.mapper.ContentPath;
|
||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||
import org.elasticsearch.index.mapper.Mapper;
|
||||
import org.elasticsearch.index.mapper.core.StringFieldMapper;
|
||||
import org.elasticsearch.index.mapper.core.TextFieldMapper;
|
||||
import org.elasticsearch.index.query.MatchAllQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
|
|
Loading…
Reference in New Issue