more strict check before trying to parse and detect a string as a date
fixes #2694
This commit is contained in:
parent
d16efbe47f
commit
9b68e98ea2
|
@ -636,7 +636,7 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
|
||||||
if (!resolved && context.root().dateDetection()) {
|
if (!resolved && context.root().dateDetection()) {
|
||||||
String text = context.parser().text();
|
String text = context.parser().text();
|
||||||
// a safe check since "1" gets parsed as well
|
// a safe check since "1" gets parsed as well
|
||||||
if (text.contains(":") || text.contains("-") || text.contains("/")) {
|
if (Strings.countOccurrencesOf(text, ":") > 1 || Strings.countOccurrencesOf(text, "-") > 1 || Strings.countOccurrencesOf(text, "/") > 1) {
|
||||||
for (FormatDateTimeFormatter dateTimeFormatter : context.root().dynamicDateTimeFormatters()) {
|
for (FormatDateTimeFormatter dateTimeFormatter : context.root().dynamicDateTimeFormatters()) {
|
||||||
try {
|
try {
|
||||||
dateTimeFormatter.parser().parseMillis(text);
|
dateTimeFormatter.parser().parseMillis(text);
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.index.mapper.FieldMapper;
|
||||||
import org.elasticsearch.index.mapper.MapperParsingException;
|
import org.elasticsearch.index.mapper.MapperParsingException;
|
||||||
import org.elasticsearch.index.mapper.ParsedDocument;
|
import org.elasticsearch.index.mapper.ParsedDocument;
|
||||||
import org.elasticsearch.index.mapper.core.DateFieldMapper;
|
import org.elasticsearch.index.mapper.core.DateFieldMapper;
|
||||||
|
import org.elasticsearch.index.mapper.core.StringFieldMapper;
|
||||||
import org.elasticsearch.test.unit.index.mapper.MapperTests;
|
import org.elasticsearch.test.unit.index.mapper.MapperTests;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
@ -48,8 +49,9 @@ public class SimpleDateMappingTests {
|
||||||
.startObject()
|
.startObject()
|
||||||
.field("date_field1", "2011/01/22")
|
.field("date_field1", "2011/01/22")
|
||||||
.field("date_field2", "2011/01/22 00:00:00")
|
.field("date_field2", "2011/01/22 00:00:00")
|
||||||
// .field("date_field3", "2011/01/22 +02")
|
.field("wrong_date1", "-4")
|
||||||
// .field("date_field4", "2011/01/22 00:00:00 +02:00")
|
.field("wrong_date2", "2012/2")
|
||||||
|
.field("wrong_date3", "2012/test")
|
||||||
.endObject()
|
.endObject()
|
||||||
.bytes());
|
.bytes());
|
||||||
|
|
||||||
|
@ -57,10 +59,13 @@ public class SimpleDateMappingTests {
|
||||||
assertThat(fieldMapper, instanceOf(DateFieldMapper.class));
|
assertThat(fieldMapper, instanceOf(DateFieldMapper.class));
|
||||||
fieldMapper = defaultMapper.mappers().smartNameFieldMapper("date_field2");
|
fieldMapper = defaultMapper.mappers().smartNameFieldMapper("date_field2");
|
||||||
assertThat(fieldMapper, instanceOf(DateFieldMapper.class));
|
assertThat(fieldMapper, instanceOf(DateFieldMapper.class));
|
||||||
// fieldMapper = defaultMapper.mappers().smartNameFieldMapper("date_field3");
|
|
||||||
// assertThat(fieldMapper, instanceOf(DateFieldMapper.class));
|
fieldMapper = defaultMapper.mappers().smartNameFieldMapper("wrong_date1");
|
||||||
// fieldMapper = defaultMapper.mappers().smartNameFieldMapper("date_field4");
|
assertThat(fieldMapper, instanceOf(StringFieldMapper.class));
|
||||||
// assertThat(fieldMapper, instanceOf(DateFieldMapper.class));
|
fieldMapper = defaultMapper.mappers().smartNameFieldMapper("wrong_date2");
|
||||||
|
assertThat(fieldMapper, instanceOf(StringFieldMapper.class));
|
||||||
|
fieldMapper = defaultMapper.mappers().smartNameFieldMapper("wrong_date3");
|
||||||
|
assertThat(fieldMapper, instanceOf(StringFieldMapper.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue