Skip to content

Commit

Permalink
[Java.time] Retain prefixed date pattern in formatter (elastic#48703)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pgomulka committed Nov 27, 2019
1 parent 90378a9 commit fc1605d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
"Test Index and Search locale dependent mappings / dates":
- skip:
version: " - 6.1.99"
reason: JDK9 only supports this with a special sysproperty added in 6.2.0
- do:
indices.create:
index: test_index
body:
settings:
number_of_shards: 1
mappings:
properties:
date_field:
type: date
format: "8E, d MMM uuuu HH:mm:ss Z"
locale: "de"
- do:
bulk:
refresh: true
body:
- '{"index": {"_index": "test_index", "_id": "1"}}'
- '{"date_field": "Mi, 06 Dez 2000 02:55:00 -0800"}'
- '{"index": {"_index": "test_index", "_id": "2"}}'
- '{"date_field": "Do, 07 Dez 2000 02:55:00 -0800"}'

- do:
search:
rest_total_hits_as_int: true
index: test_index
body: {"query" : {"range" : {"date_field" : {"gte": "Di, 05 Dez 2000 02:55:00 -0800", "lte": "Do, 07 Dez 2000 00:00:00 -0800"}}}}
- match: { hits.total: 1 }

- do:
search:
rest_total_hits_as_int: true
index: test_index
body: {"query" : {"range" : {"date_field" : {"gte": "Di, 05 Dez 2000 02:55:00 -0800", "lte": "Fr, 08 Dez 2000 00:00:00 -0800"}}}}
- match: { hits.total: 2 }
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,23 @@ static DateFormatter forPattern(String input) {
return Joda.forPattern(input);
}

// dates starting with 8 will not be using joda but java time formatters
input = input.substring(1);

List<String> patterns = splitCombinedPatterns(input);
// support the 6.x BWC compatible way of parsing java 8 dates
String format = strip8Prefix(input);
List<String> patterns = splitCombinedPatterns(format);
List<DateFormatter> formatters = patterns.stream()
.map(DateFormatters::forPattern)
.collect(Collectors.toList());

if (formatters.size() == 1) {
return formatters.get(0);
}

return DateFormatters.merge(input, formatters);
}

static String strip8Prefix(String input) {
if (input.startsWith("8")) {
return input.substring(1);
}
return input;
}

static List<String> splitCombinedPatterns(String input) {
List<String> patterns = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,21 @@ public DocumentMapper updateFieldType(Map<String, MappedFieldType> fullNameToFie
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return mapping.toXContent(builder, params);
}

@Override
public String toString() {
return "DocumentMapper{" +
"mapperService=" + mapperService +
", type='" + type + '\'' +
", typeText=" + typeText +
", mappingSource=" + mappingSource +
", mapping=" + mapping +
", documentParser=" + documentParser +
", fieldMappers=" + fieldMappers +
", objectMappers=" + objectMappers +
", hasNestedObjects=" + hasNestedObjects +
", deleteTombstoneMetadataFieldMappers=" + Arrays.toString(deleteTombstoneMetadataFieldMappers) +
", noopTombstoneMetadataFieldMappers=" + Arrays.toString(noopTombstoneMetadataFieldMappers) +
'}';
}
}

0 comments on commit fc1605d

Please sign in to comment.