From 435558a5c02da92c00598c4f30d0d8933778b1cb Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 15 Mar 2016 10:36:46 +0100 Subject: [PATCH] Also map floating-point numbers as floats when numeric detection is on. #17104 I overlooked it in #15319 since numeric detection triggers a totally different path in the code of dynamic mappings. --- .../org/elasticsearch/index/mapper/DocumentParser.java | 2 +- .../index/mapper/DynamicMappingTests.java | 10 +++++++++- .../index/mapper/numeric/SimpleNumericTests.java | 8 +++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index cf0c0fbba33..0c9f2daa6cb 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -705,7 +705,7 @@ final class DocumentParser implements Closeable { Double.parseDouble(text); Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "double"); if (builder == null) { - builder = new DoubleFieldMapper.Builder(currentFieldName); + builder = new FloatFieldMapper.Builder(currentFieldName); } return builder; } catch (NumberFormatException e) { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java index d2a8080be86..e3a81d2a06e 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java @@ -512,7 +512,13 @@ public class DynamicMappingTests extends ESSingleNodeTestCase { } public void testDefaultFloatingPointMappings() throws IOException { - DocumentMapper mapper = createIndex("test").mapperService().documentMapperWithAutoCreate("type").getDocumentMapper(); + MapperService mapperService = createIndex("test").mapperService(); + String mapping = jsonBuilder().startObject() + .startObject("type") + .field("numeric_detection", true) + .endObject().endObject().string(); + mapperService.merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE, false); + DocumentMapper mapper = mapperService.documentMapper("type"); doTestDefaultFloatingPointMappings(mapper, XContentFactory.jsonBuilder()); doTestDefaultFloatingPointMappings(mapper, XContentFactory.yamlBuilder()); doTestDefaultFloatingPointMappings(mapper, XContentFactory.smileBuilder()); @@ -524,6 +530,7 @@ public class DynamicMappingTests extends ESSingleNodeTestCase { .field("foo", 3.2f) // float .field("bar", 3.2d) // double .field("baz", (double) 3.2f) // double that can be accurately represented as a float + .field("quux", "3.2") // float detected through numeric detection .endObject().bytes(); ParsedDocument parsedDocument = mapper.parse("index", "type", "id", source); Mapping update = parsedDocument.dynamicMappingsUpdate(); @@ -531,5 +538,6 @@ public class DynamicMappingTests extends ESSingleNodeTestCase { assertThat(update.root().getMapper("foo"), instanceOf(FloatFieldMapper.class)); assertThat(update.root().getMapper("bar"), instanceOf(FloatFieldMapper.class)); assertThat(update.root().getMapper("baz"), instanceOf(FloatFieldMapper.class)); + assertThat(update.root().getMapper("quux"), instanceOf(FloatFieldMapper.class)); } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/numeric/SimpleNumericTests.java b/core/src/test/java/org/elasticsearch/index/mapper/numeric/SimpleNumericTests.java index c10ccd14262..ae29e5c15ec 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/numeric/SimpleNumericTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/numeric/SimpleNumericTests.java @@ -31,13 +31,14 @@ import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.IndexService; +import org.elasticsearch.index.fielddata.IndexNumericFieldData.NumericType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParsedDocument; -import org.elasticsearch.index.mapper.core.DoubleFieldMapper; +import org.elasticsearch.index.mapper.core.FloatFieldMapper; import org.elasticsearch.index.mapper.core.LongFieldMapper; import org.elasticsearch.index.mapper.core.NumberFieldMapper; import org.elasticsearch.index.mapper.core.TextFieldMapper; @@ -89,7 +90,7 @@ public class SimpleNumericTests extends ESSingleNodeTestCase { assertThat(mapper, instanceOf(LongFieldMapper.class)); mapper = defaultMapper.mappers().smartNameFieldMapper("s_double"); - assertThat(mapper, instanceOf(DoubleFieldMapper.class)); + assertThat(mapper, instanceOf(FloatFieldMapper.class)); } public void testNumericDetectionDefault() throws Exception { @@ -478,7 +479,8 @@ public class SimpleNumericTests extends ESSingleNodeTestCase { Document luceneDoc = doc.docs().get(0); assertPrecisionStepEquals(NumberFieldMapper.Defaults.PRECISION_STEP_64_BIT, luceneDoc.getField("long")); - assertPrecisionStepEquals(NumberFieldMapper.Defaults.PRECISION_STEP_64_BIT, luceneDoc.getField("double")); + assertThat(luceneDoc.getField("double").numericValue(), instanceOf(Float.class)); + assertPrecisionStepEquals(NumberFieldMapper.Defaults.PRECISION_STEP_32_BIT, luceneDoc.getField("double")); assertPrecisionStepEquals(NumberFieldMapper.Defaults.PRECISION_STEP_64_BIT, luceneDoc.getField("date")); }