Allow to disable automatic date detection, closes #1051.
This commit is contained in:
parent
882ccf32c8
commit
b17c2b09db
|
@ -591,6 +591,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
|
|||
// check if it fits one of the date formats
|
||||
boolean resolved = false;
|
||||
// a safe check since "1" gets parsed as well
|
||||
if (context.root().dateDetection()) {
|
||||
if (text.contains(":") || text.contains("-") || text.contains("/")) {
|
||||
for (FormatDateTimeFormatter dateTimeFormatter : context.root().dateTimeFormatters()) {
|
||||
try {
|
||||
|
@ -607,6 +608,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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) {
|
||||
|
|
|
@ -50,6 +50,7 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
DateFieldMapper.Defaults.DATE_TIME_FORMATTER,
|
||||
Joda.forPattern("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd")
|
||||
};
|
||||
public static final boolean DATE_DETECTION = false;
|
||||
}
|
||||
|
||||
public static class Builder extends ObjectMapper.Builder<Builder, RootObjectMapper> {
|
||||
|
@ -58,6 +59,8 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
|
||||
protected List<FormatDateTimeFormatter> dateTimeFormatters = newArrayList();
|
||||
|
||||
protected boolean dateDetection = Defaults.DATE_DETECTION;
|
||||
|
||||
public Builder(String name) {
|
||||
super(name);
|
||||
this.builder = this;
|
||||
|
@ -115,7 +118,8 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
}
|
||||
return new RootObjectMapper(name, enabled, dynamic, pathType, mappers,
|
||||
dates,
|
||||
dynamicTemplates.toArray(new DynamicTemplate[dynamicTemplates.size()]));
|
||||
dynamicTemplates.toArray(new DynamicTemplate[dynamicTemplates.size()]),
|
||||
dateDetection);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -161,19 +165,28 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
Map.Entry<String, Object> entry = tmpl.entrySet().iterator().next();
|
||||
((Builder) builder).add(DynamicTemplate.parse(entry.getKey(), (Map<String, Object>) entry.getValue()));
|
||||
}
|
||||
} else if (fieldName.equals("date_detection")) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final FormatDateTimeFormatter[] dateTimeFormatters;
|
||||
|
||||
private final boolean dateDetection;
|
||||
|
||||
private volatile DynamicTemplate dynamicTemplates[];
|
||||
|
||||
RootObjectMapper(String name, boolean enabled, Dynamic dynamic, ContentPath.Type pathType, Map<String, Mapper> mappers,
|
||||
FormatDateTimeFormatter[] dateTimeFormatters, DynamicTemplate dynamicTemplates[]) {
|
||||
FormatDateTimeFormatter[] dateTimeFormatters, DynamicTemplate dynamicTemplates[], boolean dateDetection) {
|
||||
super(name, name, enabled, Nested.NO, dynamic, pathType, mappers);
|
||||
this.dynamicTemplates = dynamicTemplates;
|
||||
this.dateTimeFormatters = dateTimeFormatters;
|
||||
this.dateDetection = dateDetection;
|
||||
}
|
||||
|
||||
public boolean dateDetection() {
|
||||
return this.dateDetection;
|
||||
}
|
||||
|
||||
public FormatDateTimeFormatter[] dateTimeFormatters() {
|
||||
|
@ -240,5 +253,9 @@ public class RootObjectMapper extends ObjectMapper {
|
|||
}
|
||||
builder.endArray();
|
||||
}
|
||||
|
||||
if (dateDetection != Defaults.DATE_DETECTION) {
|
||||
builder.field("date_detection", Defaults.DATE_DETECTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,4 +47,23 @@ public class SimpleDateMappingTests {
|
|||
|
||||
assertThat(doc.masterDoc().getFieldable("date_field").tokenStreamValue(), notNullValue());
|
||||
}
|
||||
|
||||
@Test public void testDateDetection() throws Exception {
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.field("date_detection", false)
|
||||
.startObject("properties").startObject("date_field").field("type", "date").endObject().endObject()
|
||||
.endObject().endObject().string();
|
||||
|
||||
DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
|
||||
|
||||
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("date_field", "2010-01-01")
|
||||
.field("date_field_x", "2010-01-01")
|
||||
.endObject()
|
||||
.copiedBytes());
|
||||
|
||||
assertThat(doc.masterDoc().get("date_field"), nullValue());
|
||||
assertThat(doc.masterDoc().get("date_field_x"), equalTo("2010-01-01"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue