From 9d72854e4914099f035f170cbe9bfadbac0ce338 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 22 Jul 2020 13:58:38 -0400 Subject: [PATCH] Replace script unit tests with integration tests (#60027) This replaces the fancy unit tests that we were doing to test our painless integration with an extension to our existing integration tests. The integration tests are "more real", shorter, and are a nice step down the road of dropping our dependency on the painless plugin in favor of a dependency only on it's SPI. --- .../DoubleScriptFieldScriptTests.java | 128 ------------------ .../LongScriptFieldScriptTests.java | 114 ---------------- .../ScriptFieldScriptTestCase.java | 110 --------------- .../StringScriptFieldScriptTests.java | 111 --------------- .../test/runtime_fields/10_keyword.yml | 29 +++- .../test/runtime_fields/20_long.yml | 27 +++- .../test/runtime_fields/30_double.yml | 27 +++- 7 files changed, 77 insertions(+), 469 deletions(-) delete mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java delete mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java delete mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java delete mode 100644 x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java deleted file mode 100644 index 0f2e2d81e7f0c..0000000000000 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScriptTests.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.runtimefields; - -import org.apache.lucene.document.SortedNumericDocValuesField; -import org.apache.lucene.document.StoredField; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.NumericUtils; -import org.elasticsearch.common.CheckedConsumer; -import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType; -import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; -import org.elasticsearch.script.ScriptContext; -import org.elasticsearch.search.lookup.SearchLookup; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.function.IntFunction; - -import static org.hamcrest.Matchers.equalTo; - -public class DoubleScriptFieldScriptTests extends ScriptFieldScriptTestCase< - DoubleScriptFieldScript.Factory, - DoubleScriptFieldScript.LeafFactory, - Double> { - - public void testConstant() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(randomDouble())))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(randomDouble())))); - }; - assertThat(execute(indexBuilder, "value(3.14)"), equalTo(List.of(3.14, 3.14))); - } - - public void testTwoConstants() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", randomLong()))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", randomLong()))); - }; - assertThat(execute(indexBuilder, "value(3.14); value(2.72)"), equalTo(List.of(3.14, 2.72, 3.14, 2.72))); - } - - public void testSource() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 1}")))); - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 10}")))); - }; - assertThat(execute(indexBuilder, "value(source['foo'] * 10.1)"), equalTo(List.of(10.1, 101.0))); - } - - public void testTwoSourceFields() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 1, \"bar\": 2}")))); - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 10, \"bar\": 20}")))); - }; - assertThat( - execute(indexBuilder, "value(source['foo'] * 10.1); value(source['bar'] * 10.2)"), - equalTo(List.of(10.1, 20.4, 101.0, 204.0)) - ); - } - - public void testDocValues() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(1.1)))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(10.1)))); - }; - assertThat( - execute(indexBuilder, "value(doc['foo'].value * 9.9)", new NumberFieldType("foo", NumberType.DOUBLE)), - equalTo(List.of(10.89, 99.99)) - ); - } - - public void testTwoDocValuesValues() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument( - List.of( - new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(1.1)), - new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(2.2)) - ) - ); - iw.addDocument( - List.of( - new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(10.1)), - new SortedNumericDocValuesField("foo", NumericUtils.doubleToSortableLong(20.1)) - ) - ); - }; - assertThat( - execute(indexBuilder, "for (double d : doc['foo']) {value(d * 9.9)}", new NumberFieldType("foo", NumberType.DOUBLE)), - equalTo(List.of(10.89, 21.78, 99.99, 198.99)) - ); - } - - @Override - protected ScriptContext scriptContext() { - return DoubleScriptFieldScript.CONTEXT; - } - - @Override - protected DoubleScriptFieldScript.LeafFactory newLeafFactory( - DoubleScriptFieldScript.Factory factory, - Map params, - SearchLookup searchLookup - ) { - return factory.newFactory(params, searchLookup); - } - - @Override - protected IntFunction> newInstance(DoubleScriptFieldScript.LeafFactory leafFactory, LeafReaderContext context) - throws IOException { - DoubleScriptFieldScript script = leafFactory.newInstance(context); - return docId -> { - script.runForDoc(docId); - List list = new ArrayList<>(script.count()); - for (int i = 0; i < script.count(); i++) { - list.add(script.values()[i]); - } - return list; - }; - } -} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java deleted file mode 100644 index 7494183fc26a2..0000000000000 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/LongScriptFieldScriptTests.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.runtimefields; - -import org.apache.lucene.document.SortedNumericDocValuesField; -import org.apache.lucene.document.StoredField; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.CheckedConsumer; -import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType; -import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; -import org.elasticsearch.script.ScriptContext; -import org.elasticsearch.search.lookup.SearchLookup; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.function.IntFunction; - -import static org.hamcrest.Matchers.equalTo; - -public class LongScriptFieldScriptTests extends ScriptFieldScriptTestCase< - LongScriptFieldScript.Factory, - LongScriptFieldScript.LeafFactory, - Long> { - - public void testConstant() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", randomLong()))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", randomLong()))); - }; - assertThat(execute(indexBuilder, "value(10)"), equalTo(List.of(10L, 10L))); - } - - public void testTwoConstants() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", randomLong()))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", randomLong()))); - }; - assertThat(execute(indexBuilder, "value(10); value(20)"), equalTo(List.of(10L, 20L, 10L, 20L))); - } - - public void testSource() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 1}")))); - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 10}")))); - }; - assertThat(execute(indexBuilder, "value(source['foo'] * 10)"), equalTo(List.of(10L, 100L))); - } - - public void testTwoSourceFields() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 1, \"bar\": 2}")))); - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": 10, \"bar\": 20}")))); - }; - assertThat(execute(indexBuilder, "value(source['foo'] * 10); value(source['bar'] * 10)"), equalTo(List.of(10L, 20L, 100L, 200L))); - } - - public void testDocValues() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", 1))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", 10))); - }; - assertThat( - execute(indexBuilder, "value(doc['foo'].value * 10)", new NumberFieldType("foo", NumberType.LONG)), - equalTo(List.of(10L, 100L)) - ); - } - - public void testTwoDocValuesValues() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", 1), new SortedNumericDocValuesField("foo", 2))); - iw.addDocument(List.of(new SortedNumericDocValuesField("foo", 10), new SortedNumericDocValuesField("foo", 20))); - }; - assertThat( - execute(indexBuilder, "for (long l : doc['foo']) {value(l * 10)}", new NumberFieldType("foo", NumberType.LONG)), - equalTo(List.of(10L, 20L, 100L, 200L)) - ); - } - - @Override - protected ScriptContext scriptContext() { - return LongScriptFieldScript.CONTEXT; - } - - @Override - protected LongScriptFieldScript.LeafFactory newLeafFactory( - LongScriptFieldScript.Factory factory, - Map params, - SearchLookup searchLookup - ) { - return factory.newFactory(params, searchLookup); - } - - @Override - protected IntFunction> newInstance(LongScriptFieldScript.LeafFactory leafFactory, LeafReaderContext context) - throws IOException { - LongScriptFieldScript script = leafFactory.newInstance(context); - return docId -> { - script.runForDoc(docId); - List list = new ArrayList<>(script.count()); - for (int i = 0; i < script.count(); i++) { - list.add(script.values()[i]); - } - return list; - }; - } -} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java deleted file mode 100644 index d6b117fa52e08..0000000000000 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/ScriptFieldScriptTestCase.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.runtimefields; - -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.search.Collector; -import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.LeafCollector; -import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.Scorable; -import org.apache.lucene.search.ScoreMode; -import org.apache.lucene.store.Directory; -import org.elasticsearch.common.CheckedConsumer; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.fielddata.IndexFieldData; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; -import org.elasticsearch.painless.PainlessPlugin; -import org.elasticsearch.plugins.ExtensiblePlugin.ExtensionLoader; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptContext; -import org.elasticsearch.script.ScriptModule; -import org.elasticsearch.script.ScriptService; -import org.elasticsearch.search.lookup.SearchLookup; -import org.elasticsearch.test.ESTestCase; - -import java.io.IOException; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.function.IntFunction; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public abstract class ScriptFieldScriptTestCase extends ESTestCase { - protected abstract ScriptContext scriptContext(); - - protected abstract LF newLeafFactory(F factory, Map params, SearchLookup searchLookup); - - protected abstract IntFunction> newInstance(LF leafFactory, LeafReaderContext context) throws IOException; - - protected final List execute(CheckedConsumer indexBuilder, String script, MappedFieldType... types) - throws IOException { - - PainlessPlugin painlessPlugin = new PainlessPlugin(); - painlessPlugin.loadExtensions(new ExtensionLoader() { - @Override - @SuppressWarnings("unchecked") // We only ever load painless extensions here so it is fairly safe. - public List loadExtensions(Class extensionPointType) { - return (List) List.of(new RuntimeFieldsPainlessExtension()); - } - }); - ScriptModule scriptModule = new ScriptModule(Settings.EMPTY, List.of(painlessPlugin, new RuntimeFields())); - Map params = new HashMap<>(); - MapperService mapperService = mock(MapperService.class); - for (MappedFieldType type : types) { - when(mapperService.fieldType(type.name())).thenReturn(type); - } - Function> fieldDataLookup = ft -> ft.fielddataBuilder("test") - .build(null, new NoneCircuitBreakerService(), mapperService); - SearchLookup searchLookup = new SearchLookup(mapperService, fieldDataLookup); - try (ScriptService scriptService = new ScriptService(Settings.EMPTY, scriptModule.engines, scriptModule.contexts)) { - F factory = AccessController.doPrivileged( - (PrivilegedAction) () -> scriptService.compile(new Script(script), scriptContext()) - ); - - try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { - indexBuilder.accept(indexWriter); - try (DirectoryReader reader = indexWriter.getReader()) { - IndexSearcher searcher = newSearcher(reader); - LF leafFactory = newLeafFactory(factory, params, searchLookup); - List result = new ArrayList<>(); - searcher.search(new MatchAllDocsQuery(), new Collector() { - @Override - public ScoreMode scoreMode() { - return ScoreMode.COMPLETE_NO_SCORES; - } - - @Override - public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException { - IntFunction> compiled = newInstance(leafFactory, context); - return new LeafCollector() { - @Override - public void setScorer(Scorable scorer) {} - - @Override - public void collect(int docId) { - result.addAll(compiled.apply(docId)); - } - }; - } - }); - return result; - } - } - } - } -} diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java deleted file mode 100644 index 15d6d225b14e5..0000000000000 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScriptTests.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -package org.elasticsearch.xpack.runtimefields; - -import org.apache.lucene.document.SortedSetDocValuesField; -import org.apache.lucene.document.StoredField; -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.CheckedConsumer; -import org.elasticsearch.index.mapper.KeywordFieldMapper.KeywordFieldType; -import org.elasticsearch.script.ScriptContext; -import org.elasticsearch.search.lookup.SearchLookup; - -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.function.IntFunction; - -import static org.hamcrest.Matchers.equalTo; - -public class StringScriptFieldScriptTests extends ScriptFieldScriptTestCase< - StringScriptFieldScript.Factory, - StringScriptFieldScript.LeafFactory, - String> { - - public void testConstant() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedSetDocValuesField("foo", new BytesRef(randomAlphaOfLength(2))))); - iw.addDocument(List.of(new SortedSetDocValuesField("foo", new BytesRef(randomAlphaOfLength(2))))); - }; - assertThat(execute(indexBuilder, "value('cat')"), equalTo(List.of("cat", "cat"))); - } - - public void testTwoConstants() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedSetDocValuesField("foo", new BytesRef(randomAlphaOfLength(2))))); - iw.addDocument(List.of(new SortedSetDocValuesField("foo", new BytesRef(randomAlphaOfLength(2))))); - }; - assertThat(execute(indexBuilder, "value('cat'); value('dog')"), equalTo(List.of("cat", "dog", "cat", "dog"))); - } - - public void testSource() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"cat\"}")))); - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"dog\"}")))); - }; - assertThat(execute(indexBuilder, "value(source['foo'] + 'o')"), equalTo(List.of("cato", "dogo"))); - } - - public void testTwoSourceFields() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"cat\", \"bar\": \"chicken\"}")))); - iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"dog\", \"bar\": \"pig\"}")))); - }; - assertThat( - execute(indexBuilder, "value(source['foo'] + 'o'); value(source['bar'] + 'ie')"), - equalTo(List.of("cato", "chickenie", "dogo", "pigie")) - ); - } - - public void testDocValues() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument(List.of(new SortedSetDocValuesField("foo", new BytesRef("cat")))); - iw.addDocument(List.of(new SortedSetDocValuesField("foo", new BytesRef("dog")))); - }; - assertThat(execute(indexBuilder, "value(doc['foo'].value + 'o')", new KeywordFieldType("foo")), equalTo(List.of("cato", "dogo"))); - } - - public void testTwoDocValuesValues() throws IOException { - CheckedConsumer indexBuilder = iw -> { - iw.addDocument( - List.of( - new SortedSetDocValuesField("foo", new BytesRef("cat")), - new SortedSetDocValuesField("foo", new BytesRef("chicken")) - ) - ); - iw.addDocument( - List.of(new SortedSetDocValuesField("foo", new BytesRef("dog")), new SortedSetDocValuesField("foo", new BytesRef("pig"))) - ); - }; - assertThat( - execute(indexBuilder, "for (String s: doc['foo']) {value(s + 'o')}", new KeywordFieldType("foo")), - equalTo(List.of("cato", "chickeno", "dogo", "pigo")) - ); - } - - @Override - protected ScriptContext scriptContext() { - return StringScriptFieldScript.CONTEXT; - } - - @Override - protected StringScriptFieldScript.LeafFactory newLeafFactory( - StringScriptFieldScript.Factory factory, - Map params, - SearchLookup searchLookup - ) { - return factory.newFactory(params, searchLookup); - } - - @Override - protected IntFunction> newInstance(StringScriptFieldScript.LeafFactory leafFactory, LeafReaderContext context) - throws IOException { - return leafFactory.newInstance(context)::resultsForDoc; - } -} diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml index 3d9a86d699e05..1116b285894af 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/10_keyword.yml @@ -21,7 +21,25 @@ setup: type: script runtime_type: keyword script: | - value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)) + value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)); + # Test fetching from _source + day_of_week_from_source: + type: script + runtime_type: keyword + script: | + Instant instant = Instant.ofEpochMilli(source['timestamp']); + ZonedDateTime dt = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC")); + value(dt.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.ROOT)); + day_of_week_letters: + type: script + runtime_type: keyword + script: | + for (String dow: doc['day_of_week']) { + for (int i = 0; i < dow.length(); i++) { + value(dow.charAt(i).toString()); + } + } + # Test fetching many values days_starting_with_t: type: script runtime_type: keyword @@ -69,7 +87,7 @@ setup: - match: {sensor.mappings.properties.day_of_week.runtime_type: keyword } - match: sensor.mappings.properties.day_of_week.script.source: | - value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)) + value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)); - match: {sensor.mappings.properties.day_of_week.script.lang: painless } - match: {sensor.mappings.properties.days_starting_with_t.type: script } - match: {sensor.mappings.properties.days_starting_with_t.runtime_type: keyword } @@ -104,9 +122,10 @@ setup: runtime_type: keyword script: source: | - value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)) + value(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT)); lang: painless meta: {} + --- "docvalue_fields": - do: @@ -114,9 +133,11 @@ setup: index: sensor body: sort: timestamp - docvalue_fields: [day_of_week, days_starting_with_t, prefixed_node] + docvalue_fields: [day_of_week, day_of_week_from_source, day_of_week_letters, days_starting_with_t, prefixed_node] - match: {hits.total.value: 6} - match: {hits.hits.0.fields.day_of_week: [Thursday] } + - match: {hits.hits.0.fields.day_of_week_from_source: [Thursday] } + - match: {hits.hits.0.fields.day_of_week_letters: [T, a, d, h, r, s, u, y] } - match: {hits.hits.0.fields.days_starting_with_t: [Thursday] } - match: {hits.hits.0.fields.prefixed_node: [node_c] } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/20_long.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/20_long.yml index e61448ff92b8b..25a83eca3dc86 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/20_long.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/20_long.yml @@ -27,6 +27,28 @@ setup: } params: multiplier: 10 + # Test fetching from _source + voltage_times_ten_from_source: + type: script + runtime_type: long + script: + source: | + value((long)(source['voltage'] * params.multiplier)); + params: + multiplier: 10 + # Test fetching many values + temperature_digits: + type: script + runtime_type: long + script: + source: | + for (long temperature : doc['temperature']) { + long t = temperature; + while (t != 0) { + value(t % 10); + t /= 10; + } + } even_voltage_times_ten: type: script runtime_type: long @@ -87,9 +109,12 @@ setup: index: sensor body: sort: timestamp - docvalue_fields: [voltage_times_ten] + docvalue_fields: [voltage_times_ten, voltage_times_ten_from_source, temperature_digits] - match: {hits.total.value: 6} - match: {hits.hits.0.fields.voltage_times_ten: [40] } + - match: {hits.hits.0.fields.voltage_times_ten_from_source: [40] } + - match: {hits.hits.0.fields.temperature_digits: [0, 2, 2] } + - match: {hits.hits.0.fields.voltage_times_ten: [40] } - match: {hits.hits.1.fields.voltage_times_ten: [42] } - match: {hits.hits.2.fields.voltage_times_ten: [56] } - match: {hits.hits.3.fields.voltage_times_ten: [51] } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/30_double.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/30_double.yml index 7d864f2b35bf4..0928eb8c28de1 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/30_double.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/30_double.yml @@ -27,6 +27,28 @@ setup: } params: max: 5.8 + # Test fetching from _source + voltage_percent_from_source: + type: script + runtime_type: double + script: + source: | + value(source['voltage'] / params.max); + params: + max: 5.8 + # Test fetching many values + voltage_sqrts: + type: script + runtime_type: double + script: + source: | + for (double voltage : doc['voltage']) { + double v = voltage; + while (v > 1.2) { + value(v); + v = Math.sqrt(v); + } + } top_voltage: type: script runtime_type: double @@ -91,9 +113,12 @@ setup: index: sensor body: sort: timestamp - docvalue_fields: [voltage_percent] + docvalue_fields: [voltage_percent, voltage_percent_from_source, voltage_sqrts] - match: {hits.total.value: 6} - match: {hits.hits.0.fields.voltage_percent: [0.6896551724137931] } + - match: {hits.hits.0.fields.voltage_percent_from_source: [0.6896551724137931] } + # Scripts that scripts that emit multiple values are supported and their results are sorted + - match: {hits.hits.0.fields.voltage_sqrts: [1.4142135623730951, 2.0, 4.0] } - match: {hits.hits.1.fields.voltage_percent: [0.7241379310344828] } - match: {hits.hits.2.fields.voltage_percent: [0.9655172413793103] } - match: {hits.hits.3.fields.voltage_percent: [0.8793103448275862] }