Skip to content

Commit 2be2759

Browse files
committed
MappingBuilder must set configured date formats for date_range fields.
Original Pull Request #2103 Closes #2102 (cherry picked from commit bf08000)
1 parent aa22d82 commit 2be2759

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

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 {
@@ -1911,6 +1935,31 @@ public void setField5(@Nullable LocalDateTime field5) {
19111935
}
19121936
}
19131937

1938+
private static class DateRangeEntity {
1939+
@Nullable
1940+
@Id private String id;
1941+
@Nullable
1942+
@Field(type = Date_Range, format = DateFormat.date) private Range<LocalDateTime> field2;
1943+
1944+
@Nullable
1945+
public String getId() {
1946+
return id;
1947+
}
1948+
1949+
public void setId(@Nullable String id) {
1950+
this.id = id;
1951+
}
1952+
1953+
@Nullable
1954+
public Range<LocalDateTime> getField2() {
1955+
return field2;
1956+
}
1957+
1958+
public void setField2(@Nullable Range<LocalDateTime> field2) {
1959+
this.field2 = field2;
1960+
}
1961+
}
1962+
19141963
@Document(indexName = "magazine")
19151964
private static class Magazine {
19161965
@Id @Nullable private String id;

0 commit comments

Comments
 (0)