Skip to content

Commit fc1605d

Browse files
committed
[Java.time] Retain prefixed date pattern in formatter (elastic#48703)
JavaDateFormatter should keep the pattern with the prefixed 8 as it will be used for serialisation. The stripped pattern should be used for the enclosed formatters. closes elastic#48698
1 parent 90378a9 commit fc1605d

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
"Test Index and Search locale dependent mappings / dates":
3+
- skip:
4+
version: " - 6.1.99"
5+
reason: JDK9 only supports this with a special sysproperty added in 6.2.0
6+
- do:
7+
indices.create:
8+
index: test_index
9+
body:
10+
settings:
11+
number_of_shards: 1
12+
mappings:
13+
properties:
14+
date_field:
15+
type: date
16+
format: "8E, d MMM uuuu HH:mm:ss Z"
17+
locale: "de"
18+
- do:
19+
bulk:
20+
refresh: true
21+
body:
22+
- '{"index": {"_index": "test_index", "_id": "1"}}'
23+
- '{"date_field": "Mi, 06 Dez 2000 02:55:00 -0800"}'
24+
- '{"index": {"_index": "test_index", "_id": "2"}}'
25+
- '{"date_field": "Do, 07 Dez 2000 02:55:00 -0800"}'
26+
27+
- do:
28+
search:
29+
rest_total_hits_as_int: true
30+
index: test_index
31+
body: {"query" : {"range" : {"date_field" : {"gte": "Di, 05 Dez 2000 02:55:00 -0800", "lte": "Do, 07 Dez 2000 00:00:00 -0800"}}}}
32+
- match: { hits.total: 1 }
33+
34+
- do:
35+
search:
36+
rest_total_hits_as_int: true
37+
index: test_index
38+
body: {"query" : {"range" : {"date_field" : {"gte": "Di, 05 Dez 2000 02:55:00 -0800", "lte": "Fr, 08 Dez 2000 00:00:00 -0800"}}}}
39+
- match: { hits.total: 2 }

server/src/main/java/org/elasticsearch/common/time/DateFormatter.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,23 @@ static DateFormatter forPattern(String input) {
136136
return Joda.forPattern(input);
137137
}
138138

139-
// dates starting with 8 will not be using joda but java time formatters
140-
input = input.substring(1);
141-
142-
List<String> patterns = splitCombinedPatterns(input);
139+
// support the 6.x BWC compatible way of parsing java 8 dates
140+
String format = strip8Prefix(input);
141+
List<String> patterns = splitCombinedPatterns(format);
143142
List<DateFormatter> formatters = patterns.stream()
144143
.map(DateFormatters::forPattern)
145144
.collect(Collectors.toList());
146145

147-
if (formatters.size() == 1) {
148-
return formatters.get(0);
149-
}
150-
151146
return DateFormatters.merge(input, formatters);
152147
}
153148

149+
static String strip8Prefix(String input) {
150+
if (input.startsWith("8")) {
151+
return input.substring(1);
152+
}
153+
return input;
154+
}
155+
154156
static List<String> splitCombinedPatterns(String input) {
155157
List<String> patterns = new ArrayList<>();
156158
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {

server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

+17
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,21 @@ public DocumentMapper updateFieldType(Map<String, MappedFieldType> fullNameToFie
357357
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
358358
return mapping.toXContent(builder, params);
359359
}
360+
361+
@Override
362+
public String toString() {
363+
return "DocumentMapper{" +
364+
"mapperService=" + mapperService +
365+
", type='" + type + '\'' +
366+
", typeText=" + typeText +
367+
", mappingSource=" + mappingSource +
368+
", mapping=" + mapping +
369+
", documentParser=" + documentParser +
370+
", fieldMappers=" + fieldMappers +
371+
", objectMappers=" + objectMappers +
372+
", hasNestedObjects=" + hasNestedObjects +
373+
", deleteTombstoneMetadataFieldMappers=" + Arrays.toString(deleteTombstoneMetadataFieldMappers) +
374+
", noopTombstoneMetadataFieldMappers=" + Arrays.toString(noopTombstoneMetadataFieldMappers) +
375+
'}';
376+
}
360377
}

0 commit comments

Comments
 (0)