Skip to content

MappingBuilder must set configured date formats for date_range fields. #2103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
if (type != FieldType.Auto) {
objectNode.put(FIELD_PARAM_TYPE, type.getMappedName());

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

// built-in formats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.data.elasticsearch.core.MappingContextBaseTests;
import org.springframework.data.elasticsearch.core.Range;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
Expand Down Expand Up @@ -721,6 +722,29 @@ void shouldMapAccordingToTheAnnotatedProperties() throws JSONException {
assertEquals(expected, mapping, false);
}

@Test // #2102
@DisplayName("should write date formats for date range fields")
void shouldWriteDateFormatsForDateRangeFields() throws JSONException {

String expected = "{\n" + //
" \"properties\": {\n" + //
" \"_class\": {\n" + //
" \"type\": \"keyword\",\n" + //
" \"index\": false,\n" + //
" \"doc_values\": false\n" + //
" },\n" + //
" \"field2\": {\n" + //
" \"type\": \"date_range\",\n" + //
" \"format\": \"date\"\n" + //
" }\n" + //
" }\n" + //
"}\n"; //

String mapping = getMappingBuilder().buildPropertyMapping(DateRangeEntity.class);

assertEquals(expected, mapping, false);
}

@Test // #1454
@DisplayName("should write type hints when context is configured to do so")
void shouldWriteTypeHintsWhenContextIsConfiguredToDoSo() throws JSONException {
Expand Down Expand Up @@ -2045,6 +2069,31 @@ public void setField5(@Nullable LocalDateTime field5) {
}
}

private static class DateRangeEntity {
@Nullable
@Id private String id;
@Nullable
@Field(type = Date_Range, format = DateFormat.date) private Range<LocalDateTime> field2;

@Nullable
public String getId() {
return id;
}

public void setId(@Nullable String id) {
this.id = id;
}

@Nullable
public Range<LocalDateTime> getField2() {
return field2;
}

public void setField2(@Nullable Range<LocalDateTime> field2) {
this.field2 = field2;
}
}

@Document(indexName = "magazine")
private static class Magazine {
@Id
Expand Down