diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java index b15929da68c..6b850b30a06 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java @@ -90,15 +90,7 @@ public class RankFeaturesFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java index 3fb70acc54c..2cef0e9da1f 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java @@ -224,15 +224,7 @@ public final class ParentJoinFieldMapper extends FieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } @Override diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java index 83aa99badb3..28d09c45804 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java @@ -226,15 +226,7 @@ public class PercolatorFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } Query percolateQuery(String name, PercolateQuery.QueryStore queryStore, List documents, diff --git a/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java b/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java index 9e1b38d62ec..e333ec4f7a6 100644 --- a/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java +++ b/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java @@ -108,15 +108,7 @@ public class Murmur3FieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected String parseSourceValue(Object value) { - return value.toString(); - } - }; + return SourceValueFetcher.toString(name(), mapperService, format); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java index a6d6305031d..5733c7a7899 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java @@ -100,15 +100,7 @@ public class BinaryFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceValueFetcher.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceValueFetcher.java index aa0b4dbfc1c..54d78044077 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceValueFetcher.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceValueFetcher.java @@ -87,4 +87,34 @@ public abstract class SourceValueFetcher implements ValueFetcher { * {@link FieldMapper#parseCreateField} or {@link FieldMapper#parse}. */ protected abstract Object parseSourceValue(Object value); + + /** + * Creates a {@link SourceValueFetcher} that passes through source values unmodified. + */ + public static SourceValueFetcher identity(String fieldName, MapperService mapperService, String format) { + if (format != null) { + throw new IllegalArgumentException("Field [" + fieldName + "] doesn't support formats."); + } + return new SourceValueFetcher(fieldName, mapperService) { + @Override + protected Object parseSourceValue(Object value) { + return value; + } + }; + } + + /** + * Creates a {@link SourceValueFetcher} that converts source values to strings. + */ + public static SourceValueFetcher toString(String fieldName, MapperService mapperService, String format) { + if (format != null) { + throw new IllegalArgumentException("Field [" + fieldName + "] doesn't support formats."); + } + return new SourceValueFetcher(fieldName, mapperService) { + @Override + protected Object parseSourceValue(Object value) { + return value.toString(); + } + }; + } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java index 19e1c7887bb..f92c0619ca3 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java @@ -690,15 +690,7 @@ public class TextFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value.toString(); - } - }; + return SourceValueFetcher.toString(name(), mapperService, format); } @Override diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java b/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java index c448d84349b..ba08d73a24e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java @@ -108,12 +108,7 @@ public class ExternalMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java b/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java index 2d388810ec0..855f4f2775d 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java @@ -78,12 +78,7 @@ public class FakeStringFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - return new SourceValueFetcher(name(), mapperService) { - @Override - protected String parseSourceValue(Object value) { - return value.toString(); - } - }; + return SourceValueFetcher.toString(name(), mapperService, format); } } diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java index 38901eb2f71..da77569edcc 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java @@ -138,15 +138,7 @@ public class HistogramFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } @Override diff --git a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java index ee53bdc0cb5..fd723ed5a80 100644 --- a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java +++ b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java @@ -474,15 +474,7 @@ public final class FlatObjectFieldMapper extends DynamicKeyFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - return new SourceValueFetcher(name(), mapperService) { - @Override - protected Object parseSourceValue(Object value) { - return value; - } - }; + return SourceValueFetcher.identity(name(), mapperService, format); } } diff --git a/x-pack/plugin/mapper-version/src/main/java/org/elasticsearch/xpack/versionfield/VersionStringFieldMapper.java b/x-pack/plugin/mapper-version/src/main/java/org/elasticsearch/xpack/versionfield/VersionStringFieldMapper.java index 49975516b6a..b6a085f3966 100644 --- a/x-pack/plugin/mapper-version/src/main/java/org/elasticsearch/xpack/versionfield/VersionStringFieldMapper.java +++ b/x-pack/plugin/mapper-version/src/main/java/org/elasticsearch/xpack/versionfield/VersionStringFieldMapper.java @@ -134,16 +134,7 @@ public class VersionStringFieldMapper extends ParametrizedFieldMapper { @Override public ValueFetcher valueFetcher(MapperService mapperService, SearchLookup searchLookup, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } - - return new SourceValueFetcher(name(), mapperService, null) { - @Override - protected String parseSourceValue(Object value) { - return value.toString(); - } - }; + return SourceValueFetcher.toString(name(), mapperService, format); } @Override diff --git a/x-pack/plugin/mapper-version/src/test/java/org/elasticsearch/xpack/versionfield/VersionStringFieldTypeTests.java b/x-pack/plugin/mapper-version/src/test/java/org/elasticsearch/xpack/versionfield/VersionStringFieldTypeTests.java index ad98c45f14e..7e53e8842ff 100644 --- a/x-pack/plugin/mapper-version/src/test/java/org/elasticsearch/xpack/versionfield/VersionStringFieldTypeTests.java +++ b/x-pack/plugin/mapper-version/src/test/java/org/elasticsearch/xpack/versionfield/VersionStringFieldTypeTests.java @@ -29,6 +29,6 @@ public class VersionStringFieldTypeTests extends FieldTypeTestCase { assertEquals(Collections.singletonList("true"), fetchSourceValue(mapper, true)); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> fetchSourceValue(mapper, "value", "format")); - assertEquals("Field [field] of type [version] doesn't support formats.", e.getMessage()); + assertEquals("Field [field] doesn't support formats.", e.getMessage()); } }