Mapping: Allow to enable automatic numeric types detection new fields with a string value, closes #1233.
This commit is contained in:
parent
3202af0dc1
commit
bf51a4e5df
|
@ -611,6 +611,32 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (!resolved && context.root().numericDetection()) {
|
||||
try {
|
||||
Long.parseLong(text);
|
||||
Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "long");
|
||||
if (builder == null) {
|
||||
builder = longField(currentFieldName);
|
||||
}
|
||||
mapper = builder.build(builderContext);
|
||||
resolved = true;
|
||||
} catch (Exception e) {
|
||||
// not a long number
|
||||
}
|
||||
if (!resolved) {
|
||||
try {
|
||||
Double.parseDouble(text);
|
||||
Mapper.Builder builder = context.root().findTemplateBuilder(context, currentFieldName, "double");
|
||||
if (builder == null) {
|
||||
builder = doubleField(currentFieldName);
|
||||
}
|
||||
mapper = builder.build(builderContext);
|
||||
resolved = true;
|
||||
} catch (Exception e) {
|
||||
// not a long number
|
||||
}
|
||||
}
|
||||
}
|
||||
// DON'T do automatic ip detection logic, since it messes up with docs that have hosts and ips
|
||||
// check if its an ip
|
||||
// if (!resolved && text.indexOf('.') != -1) {
|
||||
|
|
|
@ -54,6 +54,7 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
Joda.forPattern("yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z")
|
||||
};
|
||||
public static final boolean DATE_DETECTION = true;
|
||||
public static final boolean NUMERIC_DETECTION = false;
|
||||
}
|
||||
|
||||
public static class Builder extends ObjectMapper.Builder<Builder, RootObjectMapper> {
|
||||
|
@ -65,6 +66,7 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
protected List<FormatDateTimeFormatter> dynamicDateTimeFormatters = newArrayList();
|
||||
|
||||
protected boolean dateDetection = Defaults.DATE_DETECTION;
|
||||
protected boolean numericDetection = Defaults.NUMERIC_DETECTION;
|
||||
|
||||
public Builder(String name) {
|
||||
super(name);
|
||||
|
@ -117,7 +119,7 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
return new RootObjectMapper(name, enabled, dynamic, pathType, mappers,
|
||||
dates,
|
||||
dynamicTemplates.toArray(new DynamicTemplate[dynamicTemplates.size()]),
|
||||
dateDetection);
|
||||
dateDetection, numericDetection);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,6 +167,8 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
}
|
||||
} else if (fieldName.equals("date_detection")) {
|
||||
((Builder) builder).dateDetection = nodeBooleanValue(fieldNode);
|
||||
} else if (fieldName.equals("numeric_detection")) {
|
||||
((Builder) builder).numericDetection = nodeBooleanValue(fieldNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -172,21 +176,27 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
private final FormatDateTimeFormatter[] dynamicDateTimeFormatters;
|
||||
|
||||
private final boolean dateDetection;
|
||||
private final boolean numericDetection;
|
||||
|
||||
private volatile DynamicTemplate dynamicTemplates[];
|
||||
|
||||
RootObjectMapper(String name, boolean enabled, Dynamic dynamic, ContentPath.Type pathType, Map<String, Mapper> mappers,
|
||||
FormatDateTimeFormatter[] dynamicDateTimeFormatters, DynamicTemplate dynamicTemplates[], boolean dateDetection) {
|
||||
FormatDateTimeFormatter[] dynamicDateTimeFormatters, DynamicTemplate dynamicTemplates[], boolean dateDetection, boolean numericDetection) {
|
||||
super(name, name, enabled, Nested.NO, dynamic, pathType, mappers);
|
||||
this.dynamicTemplates = dynamicTemplates;
|
||||
this.dynamicDateTimeFormatters = dynamicDateTimeFormatters;
|
||||
this.dateDetection = dateDetection;
|
||||
this.numericDetection = numericDetection;
|
||||
}
|
||||
|
||||
public boolean dateDetection() {
|
||||
return this.dateDetection;
|
||||
}
|
||||
|
||||
public boolean numericDetection() {
|
||||
return this.numericDetection;
|
||||
}
|
||||
|
||||
public FormatDateTimeFormatter[] dynamicDateTimeFormatters() {
|
||||
return dynamicDateTimeFormatters;
|
||||
}
|
||||
|
@ -255,5 +265,8 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
if (dateDetection != Defaults.DATE_DETECTION) {
|
||||
builder.field("date_detection", dateDetection);
|
||||
}
|
||||
if (numericDetection != Defaults.NUMERIC_DETECTION) {
|
||||
builder.field("numeric_detection", numericDetection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Licensed to Elastic Search and Shay Banon under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. Elastic Search licenses this
|
||||
* file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.index.mapper.numeric;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.mapper.DocumentMapper;
|
||||
import org.elasticsearch.index.mapper.FieldMapper;
|
||||
import org.elasticsearch.index.mapper.MapperTests;
|
||||
import org.elasticsearch.index.mapper.ParsedDocument;
|
||||
import org.elasticsearch.index.mapper.core.DoubleFieldMapper;
|
||||
import org.elasticsearch.index.mapper.core.LongFieldMapper;
|
||||
import org.elasticsearch.index.mapper.core.StringFieldMapper;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/**
|
||||
*/
|
||||
@Test
|
||||
public class SimpleNumericTests {
|
||||
|
||||
public void testNumericDetectionEnabled() throws Exception {
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.field("numeric_detection", true)
|
||||
.endObject().endObject().string();
|
||||
|
||||
DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
|
||||
|
||||
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("s_long", "100")
|
||||
.field("s_double", "100.0")
|
||||
.endObject()
|
||||
.copiedBytes());
|
||||
|
||||
FieldMapper mapper = defaultMapper.mappers().smartNameFieldMapper("s_long");
|
||||
assertThat(mapper, instanceOf(LongFieldMapper.class));
|
||||
|
||||
mapper = defaultMapper.mappers().smartNameFieldMapper("s_double");
|
||||
assertThat(mapper, instanceOf(DoubleFieldMapper.class));
|
||||
}
|
||||
|
||||
public void testNumericDetectionDefault() throws Exception {
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.endObject().endObject().string();
|
||||
|
||||
DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
|
||||
|
||||
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("s_long", "100")
|
||||
.field("s_double", "100.0")
|
||||
.endObject()
|
||||
.copiedBytes());
|
||||
|
||||
FieldMapper mapper = defaultMapper.mappers().smartNameFieldMapper("s_long");
|
||||
assertThat(mapper, instanceOf(StringFieldMapper.class));
|
||||
|
||||
mapper = defaultMapper.mappers().smartNameFieldMapper("s_double");
|
||||
assertThat(mapper, instanceOf(StringFieldMapper.class));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue