Skip to content

Commit bf08000

Browse files
authored
MappingBuilder must set configured date formats for date_range fields.
Original Pull Request spring-projects#2103 Closes spring-projects#2102
1 parent 5eaea38 commit bf08000

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/index/MappingParameters.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
239239
if (type != FieldType.Auto) {
240240
objectNode.put(FIELD_PARAM_TYPE, type.getMappedName());
241241

242-
if (type == FieldType.Date) {
242+
if (type == FieldType.Date || type == FieldType.Date_Nanos || type == FieldType.Date_Range) {
243243
List<String> formats = new ArrayList<>();
244244

245245
// built-in formats

Diff for: src/test/java/org/springframework/data/elasticsearch/core/index/MappingBuilderUnitTests.java

+49
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.annotation.Transient;
4343
import org.springframework.data.elasticsearch.annotations.*;
4444
import org.springframework.data.elasticsearch.core.MappingContextBaseTests;
45+
import org.springframework.data.elasticsearch.core.Range;
4546
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
4647
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
4748
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
@@ -721,6 +722,29 @@ void shouldMapAccordingToTheAnnotatedProperties() throws JSONException {
721722
assertEquals(expected, mapping, false);
722723
}
723724

725+
@Test // #2102
726+
@DisplayName("should write date formats for date range fields")
727+
void shouldWriteDateFormatsForDateRangeFields() throws JSONException {
728+
729+
String expected = "{\n" + //
730+
" \"properties\": {\n" + //
731+
" \"_class\": {\n" + //
732+
" \"type\": \"keyword\",\n" + //
733+
" \"index\": false,\n" + //
734+
" \"doc_values\": false\n" + //
735+
" },\n" + //
736+
" \"field2\": {\n" + //
737+
" \"type\": \"date_range\",\n" + //
738+
" \"format\": \"date\"\n" + //
739+
" }\n" + //
740+
" }\n" + //
741+
"}\n"; //
742+
743+
String mapping = getMappingBuilder().buildPropertyMapping(DateRangeEntity.class);
744+
745+
assertEquals(expected, mapping, false);
746+
}
747+
724748
@Test // #1454
725749
@DisplayName("should write type hints when context is configured to do so")
726750
void shouldWriteTypeHintsWhenContextIsConfiguredToDoSo() throws JSONException {
@@ -2045,6 +2069,31 @@ public void setField5(@Nullable LocalDateTime field5) {
20452069
}
20462070
}
20472071

2072+
private static class DateRangeEntity {
2073+
@Nullable
2074+
@Id private String id;
2075+
@Nullable
2076+
@Field(type = Date_Range, format = DateFormat.date) private Range<LocalDateTime> field2;
2077+
2078+
@Nullable
2079+
public String getId() {
2080+
return id;
2081+
}
2082+
2083+
public void setId(@Nullable String id) {
2084+
this.id = id;
2085+
}
2086+
2087+
@Nullable
2088+
public Range<LocalDateTime> getField2() {
2089+
return field2;
2090+
}
2091+
2092+
public void setField2(@Nullable Range<LocalDateTime> field2) {
2093+
this.field2 = field2;
2094+
}
2095+
}
2096+
20482097
@Document(indexName = "magazine")
20492098
private static class Magazine {
20502099
@Id

0 commit comments

Comments
 (0)