diff --git a/x-pack/plugin/runtime-fields/qa/rest/build.gradle b/x-pack/plugin/runtime-fields/qa/rest/build.gradle index c6a8372d637ef..2822286f2b9e9 100644 --- a/x-pack/plugin/runtime-fields/qa/rest/build.gradle +++ b/x-pack/plugin/runtime-fields/qa/rest/build.gradle @@ -37,7 +37,6 @@ yamlRestTest { 'search/115_multiple_field_collapsing/two levels fields collapsing', // Broken. Gotta fix. 'field_caps/30_filter/Field caps with index filter', // We don't support filtering field caps on runtime fields. What should we do? 'search.aggregation/10_histogram/*', // runtime_script doesn't support sub-fields. Maybe it should? - 'search/10_source_filtering/docvalue_fields with explicit format', 'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers', 'search.aggregation/200_top_hits_metric/top_hits aggregation with nested documents', 'search/140_pre_filter_search_shards/pre_filter_shard_size with shards that have no hit', diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldType.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldType.java index 3779fc9d922bb..e835d45f861f2 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldType.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldType.java @@ -15,6 +15,7 @@ import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.script.Script; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.xpack.runtimefields.DoubleScriptFieldScript; import org.elasticsearch.xpack.runtimefields.fielddata.ScriptDoubleFieldData; @@ -46,6 +47,17 @@ public Object valueForDisplay(Object value) { return value; // These should come back as a Double } + @Override + public DocValueFormat docValueFormat(String format, ZoneId timeZone) { + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones"); + } + if (format == null) { + return DocValueFormat.RAW; + } + return new DocValueFormat.Decimal(format); + } + @Override public ScriptDoubleFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { return new ScriptDoubleFieldData.Builder(name(), leafFactory(searchLookup.get())); diff --git a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldType.java b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldType.java index 7207aa3a9bbc0..24ecde240c3f1 100644 --- a/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldType.java +++ b/x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldType.java @@ -15,6 +15,7 @@ import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.script.Script; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.xpack.runtimefields.LongScriptFieldScript; import org.elasticsearch.xpack.runtimefields.fielddata.ScriptLongFieldData; @@ -46,6 +47,17 @@ public Object valueForDisplay(Object value) { return value; // These should come back as a Long } + @Override + public DocValueFormat docValueFormat(String format, ZoneId timeZone) { + if (timeZone != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones"); + } + if (format == null) { + return DocValueFormat.RAW; + } + return new DocValueFormat.Decimal(format); + } + @Override public ScriptLongFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { return new ScriptLongFieldData.Builder(name(), leafFactory(searchLookup.get())); diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldTypeTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldTypeTests.java index bcfaff215e811..877d27a818239 100644 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldTypeTests.java +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptDoubleMappedFieldTypeTests.java @@ -54,6 +54,15 @@ import static org.hamcrest.Matchers.equalTo; public class ScriptDoubleMappedFieldTypeTests extends AbstractNonTextScriptMappedFieldTypeTestCase { + public void testFormat() throws IOException { + assertThat(simpleMappedFieldType().docValueFormat("#.0", null).format(1), equalTo("1.0")); + assertThat(simpleMappedFieldType().docValueFormat("#.0", null).format(1.2), equalTo("1.2")); + assertThat(simpleMappedFieldType().docValueFormat("#,##0.##", null).format(11), equalTo("11")); + assertThat(simpleMappedFieldType().docValueFormat("#,##0.##", null).format(1123), equalTo("1,123")); + assertThat(simpleMappedFieldType().docValueFormat("#,##0.00", null).format(1123), equalTo("1,123.00")); + assertThat(simpleMappedFieldType().docValueFormat("#,##0.00", null).format(1123.1), equalTo("1,123.10")); + } + @Override public void testDocValues() throws IOException { try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { diff --git a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldTypeTests.java b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldTypeTests.java index 3fe3ef6d24be0..1a4e7b8243854 100644 --- a/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldTypeTests.java +++ b/x-pack/plugin/runtime-fields/src/test/java/org/elasticsearch/xpack/runtimefields/mapper/ScriptLongMappedFieldTypeTests.java @@ -54,6 +54,12 @@ import static org.hamcrest.Matchers.equalTo; public class ScriptLongMappedFieldTypeTests extends AbstractNonTextScriptMappedFieldTypeTestCase { + public void testFormat() throws IOException { + assertThat(simpleMappedFieldType().docValueFormat("#.0", null).format(1), equalTo("1.0")); + assertThat(simpleMappedFieldType().docValueFormat("#,##0.##", null).format(11), equalTo("11")); + assertThat(simpleMappedFieldType().docValueFormat("#,##0.##", null).format(1123), equalTo("1,123")); + } + @Override public void testDocValues() throws IOException { try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {