From 04ceed29054d167a8b1bff90760f9d79048cd445 Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Thu, 19 Nov 2020 23:12:26 +0100 Subject: [PATCH] DATAES-978 - Accept DateFormat.none for a date property to enable custom Converters. Original pR: #556 --- .../reference/elasticsearch-object-mapping.adoc | 5 +++-- .../SimpleElasticsearchPersistentProperty.java | 11 +++++++---- ...leElasticsearchPersistentPropertyUnitTests.java | 14 -------------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc b/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc index 246b09c95..44986e18f 100644 --- a/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc +++ b/src/main/asciidoc/reference/elasticsearch-object-mapping.adoc @@ -58,13 +58,14 @@ Constructor arguments are mapped by name to the key values in the retrieved Docu ** `name`: The name of the field as it will be represented in the Elasticsearch document, if not set, the Java field name is used. ** `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_. See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types] -** `format` and `pattern` definitions for the _Date_ type. `format` must be defined for date types. +** `format` and `pattern` definitions for the _Date_ type. ** `store`: Flag whether the original field value should be store in Elasticsearch, default value is _false_. ** `analyzer`, `searchAnalyzer`, `normalizer` for specifying custom analyzers and normalizer. * `@GeoPoint`: marks a field as _geo_point_ datatype. Can be omitted if the field is an instance of the `GeoPoint` class. -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. + +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 +format different from `DateFormat.none` or a custom converter must be registered for this type. + If you are using a custom date format, you need to use _uuuu_ for the year instead of _yyyy_. 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]. diff --git a/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java b/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java index 4b6eb3599..291734999 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java @@ -163,10 +163,13 @@ public class SimpleElasticsearchPersistentProperty extends && (isTemporalAccessor || isDate)) { DateFormat dateFormat = field.format(); + String property = getOwner().getType().getSimpleName() + "." + getName(); + if (dateFormat == DateFormat.none) { - throw new MappingException( - String.format("Property %s is annotated with FieldType.%s but has no DateFormat defined", - getOwner().getType().getSimpleName() + "." + getName(), field.type().name())); + LOGGER.warn( + String.format("No DateFormat defined for property %s. Make sure you have a Converter registered for %s", + property, actualType.getSimpleName())); + return; } ElasticsearchDateConverter converter; @@ -177,7 +180,7 @@ public class SimpleElasticsearchPersistentProperty extends if (!StringUtils.hasLength(pattern)) { throw new MappingException( String.format("Property %s is annotated with FieldType.%s and a custom format but has no pattern defined", - getOwner().getType().getSimpleName() + "." + getName(), field.type().name())); + property, field.type().name())); } converter = ElasticsearchDateConverter.of(pattern); diff --git a/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java index 1512f430c..4e3dfc6f5 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java @@ -192,20 +192,6 @@ public class SimpleElasticsearchPersistentPropertyUnitTests { assertThat(seqNoProperty.isReadable()).isFalse(); } - @Test // DATAES-828 - void shouldRequireFormatForDateField() { - assertThatExceptionOfType(MappingException.class) // - .isThrownBy(() -> context.getRequiredPersistentEntity(DateFieldWithNoFormat.class)) // - .withMessageContaining("date"); - } - - @Test // DATAES-828 - void shouldRequireFormatForDateNanosField() { - assertThatExceptionOfType(MappingException.class) // - .isThrownBy(() -> context.getRequiredPersistentEntity(DateNanosFieldWithNoFormat.class)) // - .withMessageContaining("date"); - } - @Test // DATAES-924 @DisplayName("should require pattern for custom date format") void shouldRequirePatternForCustomDateFormat() {