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.
This commit is contained in:
Adrien Grand 2016-03-15 10:36:46 +01:00
parent d5e408b273
commit 435558a5c0
3 changed files with 15 additions and 5 deletions

View File

@ -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) {

View File

@ -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));
}
}

View File

@ -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"));
}