Skip to content

Commit 04ceed2

Browse files
authored
DATAES-978 - Accept DateFormat.none for a date property to enable custom Converters.
Original pR: #556
1 parent 9804334 commit 04ceed2

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ Constructor arguments are mapped by name to the key values in the retrieved Docu
5858
** `name`: The name of the field as it will be represented in the Elasticsearch document, if not set, the Java field name is used.
5959
** `type`: the field type, can be one of _Text, Keyword, Long, Integer, Short, Byte, Double, Float, Half_Float, Scaled_Float, Date, Date_Nanos, Boolean, Binary, Integer_Range, Float_Range, Long_Range, Double_Range, Date_Range, Ip_Range, Object, Nested, Ip, TokenCount, Percolator, Flattened, Search_As_You_Type_.
6060
See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types]
61-
** `format` and `pattern` definitions for the _Date_ type. `format` must be defined for date types.
61+
** `format` and `pattern` definitions for the _Date_ type.
6262
** `store`: Flag whether the original field value should be store in Elasticsearch, default value is _false_.
6363
** `analyzer`, `searchAnalyzer`, `normalizer` for specifying custom analyzers and normalizer.
6464
* `@GeoPoint`: marks a field as _geo_point_ datatype.
6565
Can be omitted if the field is an instance of the `GeoPoint` class.
6666

67-
NOTE: Properties that derive from `TemporalAccessor` must either have a `@Field` annotation of type `FieldType.Date` or a custom converter must be registered for this type. +
67+
NOTE: Properties that derive from `TemporalAccessor` or are of type `java.util.Date` must either have a `@Field` annotation of type `FieldType.Date` and a
68+
format different from `DateFormat.none` or a custom converter must be registered for this type. +
6869
If you are using a custom date format, you need to use _uuuu_ for the year instead of _yyyy_.
6970
This is due to a https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats[change in Elasticsearch 7].
7071

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,13 @@ private void initDateConverter() {
163163
&& (isTemporalAccessor || isDate)) {
164164
DateFormat dateFormat = field.format();
165165

166+
String property = getOwner().getType().getSimpleName() + "." + getName();
167+
166168
if (dateFormat == DateFormat.none) {
167-
throw new MappingException(
168-
String.format("Property %s is annotated with FieldType.%s but has no DateFormat defined",
169-
getOwner().getType().getSimpleName() + "." + getName(), field.type().name()));
169+
LOGGER.warn(
170+
String.format("No DateFormat defined for property %s. Make sure you have a Converter registered for %s",
171+
property, actualType.getSimpleName()));
172+
return;
170173
}
171174

172175
ElasticsearchDateConverter converter;
@@ -177,7 +180,7 @@ private void initDateConverter() {
177180
if (!StringUtils.hasLength(pattern)) {
178181
throw new MappingException(
179182
String.format("Property %s is annotated with FieldType.%s and a custom format but has no pattern defined",
180-
getOwner().getType().getSimpleName() + "." + getName(), field.type().name()));
183+
property, field.type().name()));
181184
}
182185

183186
converter = ElasticsearchDateConverter.of(pattern);

src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,6 @@ void seqNoPrimaryTermPropertyShouldNotBeReadable() {
192192
assertThat(seqNoProperty.isReadable()).isFalse();
193193
}
194194

195-
@Test // DATAES-828
196-
void shouldRequireFormatForDateField() {
197-
assertThatExceptionOfType(MappingException.class) //
198-
.isThrownBy(() -> context.getRequiredPersistentEntity(DateFieldWithNoFormat.class)) //
199-
.withMessageContaining("date");
200-
}
201-
202-
@Test // DATAES-828
203-
void shouldRequireFormatForDateNanosField() {
204-
assertThatExceptionOfType(MappingException.class) //
205-
.isThrownBy(() -> context.getRequiredPersistentEntity(DateNanosFieldWithNoFormat.class)) //
206-
.withMessageContaining("date");
207-
}
208-
209195
@Test // DATAES-924
210196
@DisplayName("should require pattern for custom date format")
211197
void shouldRequirePatternForCustomDateFormat() {

0 commit comments

Comments
 (0)