Skip to content

Commit 07ee01f

Browse files
authored
DATAES-750 - Migration guide and documentation update.
Original PR: spring-projects#444
1 parent b278bf9 commit 07ee01f

11 files changed

+255
-49
lines changed

src/main/asciidoc/index.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ BioMed Central Development Team; Oliver Drotbohm; Greg Turnquist; Christoph Stro
55
ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1600]]
66
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
77

8-
(C) 2013-2019 The original author(s).
8+
(C) 2013-2020 The original author(s).
99

1010
NOTE: Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
1111

1212
toc::[]
1313

1414
include::preface.adoc[]
15+
1516
:leveloffset: +1
1617
include::{spring-data-commons-docs}/repositories.adoc[]
1718
:leveloffset: -1
@@ -23,9 +24,15 @@ include::{spring-data-commons-docs}/repositories.adoc[]
2324
include::reference/elasticsearch-clients.adoc[]
2425
include::reference/elasticsearch-object-mapping.adoc[]
2526
include::reference/elasticsearch-operations.adoc[]
27+
2628
include::reference/elasticsearch-repositories.adoc[]
29+
2730
include::{spring-data-commons-docs}/auditing.adoc[]
2831
include::reference/elasticsearch-auditing.adoc[]
32+
33+
include::{spring-data-commons-docs}/entity-callbacks.adoc[]
34+
include::reference/elasticsearch-entity-callbacks.adoc[leveloffset=+1]
35+
2936
include::reference/elasticsearch-misc.adoc[]
3037
:leveloffset: -1
3138

src/main/asciidoc/preface.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The Spring Data Elasticsearch project applies core Spring concepts to the develo
99
You will notice similarities to the Spring data solr and mongodb support in the Spring Framework.
1010

1111
include::reference/elasticsearch-new.adoc[leveloffset=+1]
12+
include::reference/elasticsearch-migration-guide-3.2-4.0.adoc[leveloffset=+1]
1213

1314
[[preface.metadata]]
1415
== Project Metadata
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[[elasticsearch.entity-callbacks]]
2+
= Elasticsearch EntityCallbacks
3+
4+
Spring Data Elasticsearch uses the `EntityCallback` API internally for its auditing support and reacts on the following callbacks:
5+
6+
.Supported Entity Callbacks
7+
[%header,cols="4"]
8+
|===
9+
| Callback
10+
| Method
11+
| Description
12+
| Order
13+
14+
| Reactive/BeforeConvertCallback
15+
| `onBeforeConvert(T entity, IndexCoordinates index)`
16+
| Invoked before a domain object is converted to `org.springframework.data.elasticsearch.core.document.Document`. Can return the `entity` or a modified entity which then will be converted.
17+
| `Ordered.LOWEST_PRECEDENCE`
18+
19+
| Reactive/AfterConvertCallback
20+
| `onAfterConvert(T entity, Document document, IndexCoordinates indexCoordinates)`
21+
| Invoked after a domain object is converted from `org.springframework.data.elasticsearch.core.document.Document` on reading result data from Elasticsearch.
22+
| `Ordered.LOWEST_PRECEDENCE`
23+
24+
| Reactive/AuditingEntityCallback
25+
| `onBeforeConvert(Object entity, IndexCoordinates index)`
26+
| Marks an auditable entity _created_ or _modified_
27+
| 100
28+
29+
| Reactive/AfterSaveCallback
30+
| `T onAfterSave(T entity, IndexCoordinates index)`
31+
| Invoked after a domain object is saved.
32+
| `Ordered.LOWEST_PRECEDENCE`
33+
34+
|===
35+
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
[[elasticsearch-migration-guide-3.2-4.0]]
2+
== Upgrading from 3.2.x to 4.0.x
3+
4+
This section describes breaking changes from version 3.2.x to 4.0.x and how removed features can be replaced by new introduced features.
5+
6+
7+
=== Removal of the used Jackson Mapper.
8+
9+
One of the changes in version 4.0.x is that Spring Data Elasticsearch does not use the Jackson Mapper anymore to map an entity to the JSON representation needed for Elasticsearch (see <<elasticsearch.mapping>>). In version 3.2.x the Jackson Mapper was the default that was used. It was possible to switch to the meta-model based converter (named `ElasticsearchEntityMapper`) by explicitly configuring it (<<elasticsearch.mapping.meta-model>>).
10+
11+
In version 4.0.x the meta-model based converter is the only one that is available and does not need to be configured explicitly. If you had a custom configuration to enable the meta-model converter by providing a bean like this:
12+
13+
[code,java]
14+
----
15+
@Bean
16+
@Override
17+
public EntityMapper entityMapper() {
18+
19+
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(
20+
elasticsearchMappingContext(), new DefaultConversionService()
21+
);
22+
entityMapper.setConversions(elasticsearchCustomConversions());
23+
24+
return entityMapper;
25+
}
26+
----
27+
28+
You now have to remove this bean, the `ElasticsearchEntityMapper` interface has been removed.
29+
30+
.Entity configuration
31+
Some users had custom Jackson annotations on the entity class, for example in order to define a custom name for the mapped document in Elasticsearch or to configure date conversions. These are not taken into account anymore. The needed functionality is now provided with Spring Data Elasticsearch's `@Field` annotation. Please see <<elasticsearch.mapping.meta-model.annotations>> for detailed information.
32+
33+
34+
=== Removal of implicit index name from query objects
35+
36+
In 3.2.x the different query classes like `IndexQuery` or `SearchQuery` had properties that were taking the index name or index names that they were operating upon. If these were not set, the passed in entity was inspected to retrieve the index name that was set in the `@Document` annotation. +
37+
In 4.0.x the index name(s) must now be provided in an additional parameter of type `IndexCoordinates`. By separating this, it now is possible to use one query object against different indices.
38+
39+
So for example the following code:
40+
41+
[code,java]
42+
----
43+
IndexQuery indexQuery = new IndexQueryBuilder()
44+
.withId(person.getId().toString())
45+
.withObject(person)
46+
.build();
47+
48+
String documentId = elasticsearchOperations.index(indexQuery);
49+
----
50+
51+
must be changed to:
52+
53+
[code,java]
54+
----
55+
IndexCoordinates indexCoordinates = elasticsearchOperations.getIndexCoordinatesFor(person.getClass());
56+
57+
IndexQuery indexQuery = new IndexQueryBuilder()
58+
.withId(person.getId().toString())
59+
.withObject(person)
60+
.build();
61+
62+
String documentId = elasticsearchOperations.index(indexQuery, indexCoordinates);
63+
----
64+
65+
To make it easier to work with entities and use the index name that is contained in the entitie's `@Document` annotation, new methods have been added like `DocumentOperations.save(T entity)`;
66+
67+
68+
=== The new Operations interfaces
69+
70+
In version 3.2 there was the `ElasticsearchOperations` interface that defined all the methods for the `ElasticsearchTemplate` class. In version 4 the functions have been split into different interfaces, aligning these interfaces with the Elasticsearch API:
71+
72+
* `DocumentOperations` are the functions related documents like saving, or deleting
73+
* `SearchOperations` contains the functions to search in Elasticsearch
74+
* `IndexOperations` define the functions to operate on indexes, like index creation or mappings creation.
75+
76+
`ElasticsearchOperations` now extends `DocumentOperations` and `SearchOperations` and has methods get access to an `IndexOperations` instance.
77+
78+
NOTE: All the functions from the `ElasticsearchOperations` interface in version 3.2 that are now moved to the `IndexOperations` interface are still available, they are marked as deprecated and have default implementations that delegate to the new implementation:
79+
80+
[code,java]
81+
----
82+
/**
83+
* Create an index for given indexName .
84+
*
85+
* @param indexName the name of the index
86+
* @return {@literal true} if the index was created
87+
* @deprecated since 4.0, use {@link IndexOperations#create()}
88+
*/
89+
@Deprecated
90+
default boolean createIndex(String indexName) {
91+
return indexOps(IndexCoordinates.of(indexName)).create();
92+
}
93+
----
94+
95+
96+
=== Deprecations
97+
98+
==== Methods and classes
99+
100+
Many functions and classes have been deprecated. These functions still work, but the Javadocs show with what they should be replaced.
101+
102+
.Example from ElasticsearchOperations
103+
[code,java]
104+
----
105+
/**
106+
* Retrieves an object from an index.
107+
*
108+
* @param query the query defining the id of the object to get
109+
* @param clazz the type of the object to be returned
110+
* @return the found object
111+
* @deprecated since 4.0, use {@link #get(String, Class, IndexCoordinates)}
112+
*/
113+
@Deprecated
114+
@Nullable
115+
<T> T queryForObject(GetQuery query, Class<T> clazz);
116+
----
117+
118+
==== Elasticsearch deprecations
119+
120+
Since version 7 the Elasticsearch `TransportClient` is deprecated, it will be removed with Elasticsearch version 8. Spring Data Elasticsearch deprecates the `ElasticsearchTemplate` class which uses the `TransportClient` in version 4.0.
121+
122+
Mapping types were removed from Elasticsearch 7, they still exist as deprecated values in the Spring Data `@Document` annotation and the `IndexCoordinates` class but they are not used anymore internally.
123+
124+
=== Removals
125+
126+
* As already described, the `ElasticsearchEntityMapper` interface has been removed.
127+
128+
* The `SearchQuery` interface has been merged into it's base interface `Query`, so it's occurrences can just be replaced with `Query`.
129+
130+
* The method `org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor<T> resultsExtractor);` and the `org.springframework.data.elasticsearch.core.ResultsExtractor` interface have been removed. These could be used to parse the result from Elasticsearch for cases in which the response mapping done with the Jackson based mapper was not enough. Since version 4.0, there are the new <<elasticsearch.operations.searchresulttypes>> to return the information from an Elasticsearch response, so there is no need to expose this low level functionality.
131+
132+
* The low level methods `startScroll`, `continueScroll` and `clearScroll` have been removed from the `ElasticsearchOperations` interface. For low level scroll API access, there now are `searchScrollStart`, `searchScrollContinue` and `searchScrollClear` methods on the `ElasticsearchRestTemplate` class.
133+

src/main/asciidoc/reference/elasticsearch-new.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
* Removal of the Jackson `ObjectMapper`, now using the <<elasticsearch.mapping.meta-model,MappingElasticsearchConverter>>
1212
* Cleanup of the API in the `*Operations` interfaces, grouping and renaming methods so that they match the Elasticsearch API, deprecating the old methods, aligning with other Spring Data modules.
1313
* Introduction of `SearchHit<T>` class to represent a found document together with the relevant result metadata for this document (i.e. _sortValues_).
14-
* Introduction of the `SearchHits<T>` class to represent a whole search result together with the metadata for the complete search result (i.e. _max_score_).
14+
* Introduction of the `SearchHits<T>` class to represent a whole search result together with the
15+
metadata for the complete search result (i.e. _max_score_).
16+
* Introduction of `SearchPage<T>` class to represent a paged result containing a `SearchHits<T>` instance.
1517
* Introduction of the `GeoDistanceOrder` class to be able to create sorting by geographical distance
18+
* Implementation of Auditing Support
19+
* Implementation of lifecycle entity callbacks
1620

1721
[[new-features.3-2-0]]
1822
== New in Spring Data Elasticsearch 3.2

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The main reasons for the removal of the Jackson based mapper are:
99

1010
* Custom mappings of fields needed to be done with annotations like `@JsonFormat` or `@JsonInclude`. This often caused problems when the same object was used in different JSON based datastores or sent over a JSON based API.
1111
* Custom field types and formats also need to be stored into the Elasticsearch index mappings. The Jackson based annotations did not fully provide all the information that is necessary to represent the types of Elasticsearch.
12-
* Fields must be mapped not only when converting fromand to entities, but also an query argument, returned data and on other places.
12+
* Fields must be mapped not only when converting from and to entities, but also in query argument, returned data and on other places.
1313

1414
Using the `MappingElasticsearchConverter` now covers all these cases.
1515

@@ -29,23 +29,23 @@ The following annotations are available:
2929

3030
* `@Document`: Applied at the class level to indicate this class is a candidate for mapping to the database. The most important attributes are:
3131
** `indexName`: the name of the index to store this entity in
32-
** `type`: the mapping type. If not set, the lowercased simple name of the class is used.
32+
** `type`: [line-through]#the mapping type. If not set, the lowercased simple name of the class is used.# (deprecated since version 4.0)
3333
** `shards`: the number of shards for the index.
3434
** `replicas`: the number of replicas for the index.
3535
** `refreshIntervall`: Refresh interval for the index. Used for index creation. Default value is _"1s"_.
3636
** `indexStoreType`: Index storage type for the index. Used for index creation. Default value is _"fs"_.
3737
** `createIndex`: Configuration whether to create an index on repository bootstrapping. Default value is _true_.
3838
** `versionType`: Configuration of version management. Default value is _EXTERNAL_.
39+
3940
* `@Id`: Applied at the field level to mark the field used for identity purpose.
40-
* `@Transient`: By default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database
41+
* `@Transient`: By default all fields are mapped to the document when it is stored or retrieved, this annotation excludes the field.
4142
* `@PersistenceConstructor`: Marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved Document.
42-
* `@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:
43+
* `@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):
4344
** `name`: The name of the field as it will be represented in the Elasticsearch document, if not set, the Java field name is used.
44-
** `type`: the field type, can be one of _Text, Integer, Long, Date, Float, Double, Boolean, Object, Auto, Nested, Ip, Attachment, Keyword_. See https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html[Elasticsearch Mapping Types]
45+
** `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]
4546
** `format` and `pattern` custom definitions for the _Date_ type.
4647
** `store`: Flag wether the original field value should be store in Elasticsearch, default value is _false_.
4748
** `analyzer`, `searchAnalyzer`, `normalizer` for specifying custom custom analyzers and normalizer.
48-
** `copy_to`: the target field to copy multiple document fields to.
4949
* `@GeoPoint`: marks a field as _geo_point_ datatype. Can be omitted if the field is an instance of the `GeoPoint` class.
5050

5151
The mapping metadata infrastructure is defined in a separate spring-data-commons project that is technology agnostic.

src/main/asciidoc/reference/elasticsearch-operations.adoc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ include::reactive-elasticsearch-operations.adoc[leveloffset=+1]
125125

126126
When a document is retrieved with the methods of the `DocumentOperations` interface, just the found entity will be returned. When searching with the methods of the `SearchOperations` interface, additional information is available for each entity, for example the _score_ or the _sortValues_ of the found entity.
127127

128-
In order to return this information, each entity is wrapped in a `SearchHit` object that contains this entity-specific additional information. These `SearchHit` objects themselves are returned within a `SearchHits` object which additionally contains informations about the whole search ike the _maxScore_ or requested aggregations.
128+
In order to return this information, each entity is wrapped in a `SearchHit` object that contains this entity-specific additional information. These `SearchHit` objects themselves are returned within a `SearchHits` object which additionally contains informations about the whole search like the _maxScore_ or requested aggregations. The following classes and interfaces are now available:
129129

130130
.SearchHit<T>
131131
Contains the following information:
132132

133133
* Id
134134
* Score
135135
* Sort Values
136-
* Highligth fields
136+
* Highlight fields
137137
* The retrieved entity of type <T>
138138

139139
.SearchHits<T>
@@ -145,3 +145,12 @@ Contains the following information:
145145
* A list of `SearchHit<T>` objects
146146
* Returned aggregations
147147

148+
.SearchPage<T>
149+
Defines a Spring Data `Page` that contains a `SearchHits<T>` element and can be used for paging access using repository methods.
150+
151+
.SearchScrollHits<T>
152+
Returned by the low level scroll API functions in `ElasticsearchRestTemplate`, it enriches a `SearchHits<T>` with the Elasticsearch scroll id.
153+
154+
.SearchHitsIterator<T>
155+
An Iterator returned by the streaming functions of the `SearchOperations` interface.
156+

src/main/java/org/springframework/data/elasticsearch/annotations/FieldType.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@
2424
* @author Aleksei Arsenev
2525
*/
2626
public enum FieldType {
27-
Auto,
28-
Text,
29-
Keyword,
30-
Long,
31-
Integer,
32-
Short,
33-
Byte,
34-
Double,
35-
Float,
36-
Half_Float,
37-
Scaled_Float,
38-
Date,
39-
Date_Nanos,
40-
Boolean,
41-
Binary,
42-
Integer_Range,
43-
Float_Range,
44-
Long_Range,
45-
Double_Range,
46-
Date_Range,
47-
Ip_Range,
48-
Object,
49-
Nested,
50-
Ip,
51-
TokenCount,
52-
Percolator,
53-
Flattened,
54-
Search_As_You_Type
27+
Auto, //
28+
Text, //
29+
Keyword, //
30+
Long, //
31+
Integer, //
32+
Short, //
33+
Byte, //
34+
Double, //
35+
Float, //
36+
Half_Float, //
37+
Scaled_Float, //
38+
Date, //
39+
Date_Nanos, //
40+
Boolean, //
41+
Binary, //
42+
Integer_Range, //
43+
Float_Range, //
44+
Long_Range, //
45+
Double_Range, //
46+
Date_Range, //
47+
Ip_Range, //
48+
Object, //
49+
Nested, //
50+
Ip, //
51+
TokenCount, //
52+
Percolator, //
53+
Flattened, //
54+
Search_As_You_Type //
5555
}

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ public <T> T get(GetQuery query, Class<T> clazz, IndexCoordinates index) {
198198
return get(query.getId(), clazz, index);
199199
}
200200

201+
@Override
202+
@Nullable
203+
public <T> T queryForObject(GetQuery query, Class<T> clazz) {
204+
return get(query.getId(), clazz, getIndexCoordinatesFor(clazz));
205+
}
206+
201207
@Override
202208
public boolean exists(String id, Class<?> clazz) {
203209
return exists(id, getIndexCoordinatesFor(clazz));

src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,16 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
256256
@Nullable
257257
<T> T get(GetQuery query, Class<T> clazz, IndexCoordinates index);
258258

259+
/**
260+
* Retrieves an object from an index.
261+
*
262+
* @param query the query defining the id of the object to get
263+
* @param clazz the type of the object to be returned
264+
* @return the found object
265+
* @deprecated since 4.0, use {@link #get(String, Class, IndexCoordinates)}
266+
*/
267+
@Deprecated
268+
@Nullable
269+
<T> T queryForObject(GetQuery query, Class<T> clazz);
259270
// endregion
260271
}

0 commit comments

Comments
 (0)