Skip to content

Commit 830ab7e

Browse files
committed
Allow multiple date formats for date fields
1 parent 120eed0 commit 830ab7e

File tree

6 files changed

+151
-119
lines changed

6 files changed

+151
-119
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/DateFormat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
* @author Jakub Vavrik
2424
* @author Tim te Beek
2525
* @author Peter-Josef Meisch
26+
* @author Sascha Woo
2627
*/
2728
public enum DateFormat {
28-
none(""), //
2929
custom(""), //
3030
basic_date("uuuuMMdd"), //
3131
basic_date_time("uuuuMMdd'T'HHmmss.SSSXXX"), //

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/Field.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @author Aleksei Arsenev
3737
* @author Brian Kimmig
3838
* @author Morgan Lutz
39+
* @author Sascha Woo
3940
*/
4041
@Retention(RetentionPolicy.RUNTIME)
4142
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@@ -65,7 +66,7 @@
6566

6667
boolean index() default true;
6768

68-
DateFormat format() default DateFormat.none;
69+
DateFormat[] format() default { DateFormat.date_optional_time, DateFormat.epoch_millis };
6970

7071
String pattern() default "";
7172

Diff for: src/main/java/org/springframework/data/elasticsearch/annotations/InnerField.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
boolean index() default true;
4242

43-
DateFormat format() default DateFormat.none;
43+
DateFormat[] format() default { DateFormat.date_optional_time, DateFormat.epoch_millis };
4444

4545
String pattern() default "";
4646

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.IOException;
1919
import java.lang.annotation.Annotation;
20+
import java.util.ArrayList;
21+
import java.util.List;
2022

2123
import org.elasticsearch.common.Nullable;
2224
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -41,6 +43,7 @@
4143
* @author Aleksei Arsenev
4244
* @author Brian Kimmig
4345
* @author Morgan Lutz
46+
* @author Sascha Woo
4447
* @since 4.0
4548
*/
4649
public final class MappingParameters {
@@ -83,7 +86,7 @@ public final class MappingParameters {
8386
private final boolean eagerGlobalOrdinals;
8487
private final boolean enabled;
8588
private final boolean fielddata;
86-
private final DateFormat format;
89+
private final DateFormat[] format;
8790
@Nullable private final Integer ignoreAbove;
8891
private final boolean ignoreMalformed;
8992
private final boolean index;
@@ -226,8 +229,17 @@ public void writeTypeAndParametersTo(XContentBuilder builder) throws IOException
226229

227230
if (type != FieldType.Auto) {
228231
builder.field(FIELD_PARAM_TYPE, type.name().toLowerCase());
229-
if (type == FieldType.Date && format != DateFormat.none) {
230-
builder.field(FIELD_PARAM_FORMAT, format == DateFormat.custom ? datePattern : format.toString());
232+
if (type == FieldType.Date && format.length > 0) {
233+
List<String> formats = new ArrayList<>();
234+
for (int i = 0; i < format.length; i++) {
235+
DateFormat dateFormat = format[i];
236+
if (dateFormat == DateFormat.custom) {
237+
formats.add(datePattern);
238+
} else {
239+
formats.add(format.toString());
240+
}
241+
}
242+
builder.field(FIELD_PARAM_FORMAT, String.join("||", formats));
231243
}
232244
}
233245

0 commit comments

Comments
 (0)