Skip to content

Commit 7c340bc

Browse files
committed
Polishing and documentation.
1 parent 271e1ee commit 7c340bc

File tree

5 files changed

+81
-36
lines changed

5 files changed

+81
-36
lines changed

Diff for: src/main/asciidoc/reference/elasticsearch-migration-guide-4.2-4.3.adoc

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ This section describes breaking changes from version 4.2.x to 4.3.x and how remo
1111

1212
=== Handling of field and sourceFilter properties of Query
1313

14-
Up to version 4.2 the `fields` property of a `Query` was interpreted and added to the include list of the `sourceFilter`. This was not correct, as these are different things for Elasticsearch. This has been corrected. As a consequence code might not work anymore that relies on using `fields` to specify which fields should be returned from the document's
15-
`_source' and should be changed to use the `sourceFilter`.
14+
Up to version 4.2 the `fields` property of a `Query` was interpreted and added to the include list of the `sourceFilter`. This was not correct, as these are different things for Elasticsearch. This has been corrected. As a consequence code might not work anymore that relies on using `fields` to specify which fields should be returned from the document's `_source' and should be changed to use the `sourceFilter`.

Diff for: src/main/asciidoc/reference/elasticsearch-object-mapping.adoc

+66-15
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ Constructor arguments are mapped by name to the key values in the retrieved Docu
4747
* `@Field`: Applied at the field level and defines properties of the field, most of the attributes map to the respective https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html[Elasticsearch Mapping] definitions (the following list is not complete, check the annotation Javadoc for a complete reference):
4848
** `name`: The name of the field as it will be represented in the Elasticsearch document, if not set, the Java field name is used.
4949
** `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_.
50-
See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types]. If the field type is not specified, it defaults to `FieldType.Auto`. This means, that no mapping entry is written for the property and that Elasticsearch will add a mapping entry dynamically when the first data for this property is stored
51-
(check the Elasticsearch documentation for dynamic mapping rules).
50+
See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types].
51+
If the field type is not specified, it defaults to `FieldType.Auto`.
52+
This means, that no mapping entry is written for the property and that Elasticsearch will add a mapping entry dynamically when the first data for this property is stored (check the Elasticsearch documentation for dynamic mapping rules).
5253
** `format`: One or more built-in date formats, see the next section <<elasticsearch.mapping.meta-model.date-formats>>.
5354
** `pattern`: One or more custom date formats, see the next section <<elasticsearch.mapping.meta-model.date-formats>>.
5455
** `store`: Flag whether the original field value should be store in Elasticsearch, default value is _false_.
@@ -61,21 +62,20 @@ The mapping metadata infrastructure is defined in a separate spring-data-commons
6162
[[elasticsearch.mapping.meta-model.date-formats]]
6263
==== Date format mapping
6364

64-
Properties that derive from `TemporalAccessor` or are of type `java.util.Date` must either have a `@Field` annotation
65-
of type `FieldType.Date` or a custom converter must be registered for this type. This paragraph describes the use of
65+
Properties that derive from `TemporalAccessor` or are of type `java.util.Date` must either have a `@Field` annotation of type `FieldType.Date` or a custom converter must be registered for this type.
66+
This paragraph describes the use of
6667
`FieldType.Date`.
6768

68-
There are two attributes of the `@Field` annotation that define which date format information is written to the
69-
mapping (also see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats[Elasticsearch Built In Formats] and https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats[Elasticsearch Custom Date Formats])
69+
There are two attributes of the `@Field` annotation that define which date format information is written to the mapping (also see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#built-in-date-formats[Elasticsearch Built In Formats] and https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html#custom-date-formats[Elasticsearch Custom Date Formats])
7070

71-
The `format` attributes is used to define at least one of the predefined formats. If it is not defined, then a
72-
default value of __date_optional_time_ and _epoch_millis_ is used.
71+
The `format` attributes is used to define at least one of the predefined formats.
72+
If it is not defined, then a default value of __date_optional_time_ and _epoch_millis_ is used.
7373

74-
The `pattern` attribute can be used to add additional custom format strings. If you want to use only custom date formats, you must set the `format` property to empty `{}`.
74+
The `pattern` attribute can be used to add additional custom format strings.
75+
If you want to use only custom date formats, you must set the `format` property to empty `{}`.
7576

7677
The following table shows the different attributes and the mapping created from their values:
7778

78-
7979
[cols=2*,options=header]
8080
|===
8181
| annotation
@@ -101,12 +101,59 @@ The following table shows the different attributes and the mapping created from
101101
NOTE: If you are using a custom date format, you need to use _uuuu_ for the year instead of _yyyy_.
102102
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].
103103

104+
==== Range types
105+
106+
When a field is annotated with a type of one of _Integer_Range, Float_Range, Long_Range, Double_Range, Date_Range,_ or _Ip_Range_ the field must be an instance of a class that will be mapped to an Elasticsearch range, for example:
107+
108+
====
109+
[source,java]
110+
----
111+
class SomePersonData {
112+
113+
@Field(type = FieldType.Integer_Range)
114+
private ValidAge validAge;
115+
116+
// getter and setter
117+
}
118+
119+
class ValidAge {
120+
@Field(name="gte")
121+
private Integer from;
122+
123+
@Field(name="lte")
124+
private Integer to;
125+
126+
// getter and setter
127+
}
128+
----
129+
====
130+
131+
As an alternative Spring Data Elasticsearch provides a `Range<T>` class so that the previous example can be written as:
132+
133+
====
134+
[source,java]
135+
----
136+
class SomePersonData {
137+
138+
@Field(type = FieldType.Integer_Range)
139+
private Range<Integer> validAge;
140+
141+
// getter and setter
142+
}
143+
----
144+
====
145+
146+
Supported classes for the type `<T>` are `Integer`, `Long`, `Float`, `Double`, `Date` and classes that implement the
147+
`TemporalAccessor` interface.
148+
104149
==== Mapped field names
105150

106-
Without further configuration, Spring Data Elasticsearch will use the property name of an object as field name in Elasticsearch. This can be changed for individual field by using the `@Field` annotation on that property.
151+
Without further configuration, Spring Data Elasticsearch will use the property name of an object as field name in Elasticsearch.
152+
This can be changed for individual field by using the `@Field` annotation on that property.
107153

108-
It is also possible to define a `FieldNamingStrategy` in the configuration of the client (<<elasticsearch.clients>>). If for example a `SnakeCaseFieldNamingStrategy` is configured, the property _sampleProperty_ of the object would be mapped to _sample_property_ in Elasticsearch. A `FieldNamingStrategy` applies to all entities; it can be overwritten by
109-
setting a specific name with `@Field` on a property.
154+
It is also possible to define a `FieldNamingStrategy` in the configuration of the client (<<elasticsearch.clients>>).
155+
If for example a `SnakeCaseFieldNamingStrategy` is configured, the property _sampleProperty_ of the object would be mapped to _sample_property_ in Elasticsearch.
156+
A `FieldNamingStrategy` applies to all entities; it can be overwritten by setting a specific name with `@Field` on a property.
110157

111158
[[elasticsearch.mapping.meta-model.rules]]
112159
=== Mapping Rules
@@ -171,19 +218,23 @@ NOTE: Type hints will not be written for nested Objects unless the properties ty
171218

172219
===== Disabling Type Hints
173220

174-
It may be necessary to disable writing of type hints when the index that should be used already exists without having the type hints defined in its mapping and with the mapping mode set to strict. In this case, writing the type hint will produce an error, as the field cannot be added automatically.
221+
It may be necessary to disable writing of type hints when the index that should be used already exists without having the type hints defined in its mapping and with the mapping mode set to strict.
222+
In this case, writing the type hint will produce an error, as the field cannot be added automatically.
175223

176224
Type hints can be disabled for the whole application by overriding the method `writeTypeHints()` in a configuration class derived from `AbstractElasticsearchConfiguration` (see <<elasticsearch.clients>>).
177225

178226
As an alternativ they can be disabled for a single index with the `@Document` annotation:
227+
179228
====
180229
[source,java]
181230
----
182231
@Document(indexName = "index", writeTypeHint = WriteTypeHint.FALSE)
183232
----
184233
====
185234

186-
WARNING: We strongly advise against disabling Type Hints. Only do this if you are forced to. Disabling type hints can lead to documents not being retrieved correctly from Elasticsearch in case of polymorphic data or document retrieval may fail completely.
235+
WARNING: We strongly advise against disabling Type Hints.
236+
Only do this if you are forced to.
237+
Disabling type hints can lead to documents not being retrieved correctly from Elasticsearch in case of polymorphic data or document retrieval may fail completely.
187238

188239
==== Geospatial Types
189240

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

+11-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
/**
2424
* Simple value object to work with ranges and boundaries.
25-
*
25+
*
2626
* @author Sascha Woo
2727
* @since 4.3
2828
*/
@@ -33,12 +33,12 @@ public class Range<T> {
3333
/**
3434
* The lower bound of the range.
3535
*/
36-
private Bound<T> lowerBound;
36+
private final Bound<T> lowerBound;
3737

3838
/**
3939
* The upper bound of the range.
4040
*/
41-
private Bound<T> upperBound;
41+
private final Bound<T> upperBound;
4242

4343
/**
4444
* Creates a new {@link Range} with inclusive bounds for both values.
@@ -89,12 +89,10 @@ public static <T> Range<T> leftUnbounded(Bound<T> to) {
8989
}
9090

9191
/**
92-
* Creates a new {@link Range} with the given lower and upper bound. Prefer {@link #from(Bound)} for a more builder
93-
* style API.
92+
* Creates a new {@link Range} with the given lower and upper bound.
9493
*
9594
* @param lowerBound must not be {@literal null}.
9695
* @param upperBound must not be {@literal null}.
97-
* @see #from(Bound)
9896
*/
9997
public static <T> Range<T> of(Bound<T> lowerBound, Bound<T> upperBound) {
10098
return new Range<>(lowerBound, upperBound);
@@ -146,10 +144,7 @@ public static <T> Range<T> unbounded() {
146144
return (Range<T>) UNBOUNDED;
147145
}
148146

149-
private Range() {
150-
}
151-
152-
private Range(Bound<T> lowerBound, Bound<T> upperBound) {
147+
private Range(Bound<T> lowerBound, Bound<T> upperBound) {
153148

154149
Assert.notNull(lowerBound, "Lower bound must not be null!");
155150
Assert.notNull(upperBound, "Upper bound must not be null!");
@@ -164,7 +159,8 @@ private Range(Bound<T> lowerBound, Bound<T> upperBound) {
164159
* @param value must not be {@literal null}.
165160
* @return
166161
*/
167-
public boolean contains(T value) {
162+
@SuppressWarnings("unchecked")
163+
public boolean contains(T value) {
168164

169165
Assert.notNull(value, "Reference value must not be null!");
170166
Assert.isInstanceOf(Comparable.class, value, "value must implements Comparable!");
@@ -243,7 +239,8 @@ public static final class Bound<T> {
243239
@SuppressWarnings({ "rawtypes", "unchecked" }) //
244240
private static final Bound<?> UNBOUNDED = new Bound(Optional.empty(), true);
245241

246-
private final Optional<T> value;
242+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
243+
private final Optional<T> value;
247244
private final boolean inclusive;
248245

249246
/**
@@ -360,7 +357,8 @@ public static <T> Bound<T> unbounded() {
360357
return (Bound<T>) UNBOUNDED;
361358
}
362359

363-
private Bound(Optional<T> value, boolean inclusive) {
360+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
361+
private Bound(Optional<T> value, boolean inclusive) {
364362
this.value = value;
365363
this.inclusive = inclusive;
366364
}

Diff for: src/test/java/org/springframework/data/elasticsearch/core/RangeTests.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.time.LocalDate;
21-
import java.util.Arrays;
21+
import java.util.Collections;
2222

2323
import org.junit.jupiter.api.Test;
2424

@@ -166,9 +166,7 @@ public void shouldThrowExceptionIfNotComparable() {
166166

167167
// given
168168
// when
169-
Throwable thrown = catchThrowable(() -> {
170-
Range.just(Arrays.asList("test"));
171-
});
169+
Throwable thrown = catchThrowable(() -> Range.just(Collections.singletonList("test")));
172170
// then
173171
assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
174172
.hasMessageContaining("value must implements Comparable!");

Diff for: src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -2275,8 +2275,7 @@ public void setSaved(@Nullable String saved) {
22752275
}
22762276
}
22772277

2278-
private static class ElectricCar extends Car {
2279-
}
2278+
private static class ElectricCar extends Car {}
22802279

22812280
private static class PersonWithCars {
22822281
@Id @Nullable String id;

0 commit comments

Comments
 (0)