Skip to content

Commit 1e4b70b

Browse files
authored
Refactor code using Elasticsearch libs.
Original Pull Request spring-projects#2196 Closes spring-projects#2157
1 parent f917fb7 commit 1e4b70b

File tree

116 files changed

+1715
-328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+1715
-328
lines changed

src/main/asciidoc/preface.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ built and tested.
3737
[cols="^,^,^,^,^",options="header"]
3838
|===
3939
| Spring Data Release Train | Spring Data Elasticsearch | Elasticsearch | Spring Framework | Spring Boot
40+
| 2022.0 (Turing) | 5.0.x | 8.2.2 | 6.0.x | 3.0.x?
4041
| 2021.2 (Raj) | 4.4.x | 7.17.3 | 5.3.x | 2.7.x
4142
| 2021.1 (Q) | 4.3.x | 7.15.2 | 5.3.x | 2.6.x
4243
| 2021.0 (Pascal) | 4.2.xfootnote:oom[Out of maintenance] | 7.12.0 | 5.3.x | 2.5.x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
[[elasticsearch-migration-guide-4.4-5.0]]
2+
= Upgrading from 4.4.x to 5.0.x
3+
4+
This section describes breaking changes from version 4.4.x to 5.0.x and how removed features can be replaced by new
5+
introduced features.
6+
7+
[[elasticsearch-migration-guide-4.4-4.5.deprecations]]
8+
== Deprecations
9+
10+
=== `org.springframework.data.elasticsearch.client.erhlc` package
11+
12+
See <<elasticsearch-migration-guide-4.4-5.0.breaking-changes-packages>>, all classes in this package have been
13+
deprecated, as the default client implementations to use are the ones based on the new Java Client from
14+
Elasticsearch, se <<elasticsearch-migration-guide-4.4-5.0.new-clients>>
15+
16+
[[elasticsearch-migration-guide-4.4-5.0.breaking-changes]]
17+
== Breaking Changes
18+
19+
=== Removal of deprecated calls
20+
21+
==== suggest calls in operations interfaces have been removed
22+
23+
Both `SearchOperations` and `ReactiveSearchOperations` had deprecated calls that were using Elasticsearch classes as
24+
parameters. These now have been removed and so the dependency on Elasticsearch classes in these APIs has been cleaned.
25+
26+
[[elasticsearch-migration-guide-4.4-5.0.breaking-changes-packages]]
27+
=== Package changes
28+
29+
All the classes that are using or depend on the deprecated Elasticsearch `RestHighLevelClient` have been moved to the
30+
package `org.springframework.data.elasticsearch.client.erhlc`. By this change we now have a clear separation of code
31+
using the old deprecated Elasticsearch libraries, code using the new Elasticsearch client and code that is
32+
independent of the client implementation. Also the reactive implementation that was provided up to now has been moved
33+
here, as this implementation contains code that was copied and adapted from Elasticsearch libraries.
34+
35+
36+
[[elasticsearch-migration-guide-4.4-5.0.new-clients]]
37+
== New Elasticsearch client
38+
39+
Spring Data Elasticsearch now uses the new `ElasticsearchClient` and has
40+
deprecated the use of the previous `RestHighLevelClient`.
41+
42+
=== How to use the new client
43+
44+
In order to use the new client the following steps are necessary:
45+
46+
==== Add dependencies
47+
48+
The dependencies for the new Elasticsearch client are still optional in Spring Data Elasticsearch so they need to be added explicitly:
49+
50+
====
51+
[source,xml]
52+
----
53+
<dependencies>
54+
<dependency>
55+
<groupId>co.elastic.clients</groupId>
56+
<artifactId>elasticsearch-java</artifactId>
57+
<version>7.17.3</version>
58+
<exclusions>
59+
<exclusion>
60+
<groupId>commons-logging</groupId>
61+
<artifactId>commons-logging</artifactId>
62+
</exclusion>
63+
</exclusions>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.elasticsearch.client</groupId>
67+
<artifactId>elasticsearch-rest-client</artifactId> <!-- is Apache 2-->
68+
<version>7.17.3</version>
69+
<exclusions>
70+
<exclusion>
71+
<groupId>commons-logging</groupId>
72+
<artifactId>commons-logging</artifactId>
73+
</exclusion>
74+
</exclusions>
75+
</dependency>
76+
</dependencies>
77+
----
78+
====
79+
80+
When using Spring Boot, it is necessary to set the following property in the _pom.xml_.
81+
82+
====
83+
[source,xml]
84+
----
85+
<properties>
86+
<jakarta-json.version>2.0.1</jakarta-json.version>
87+
</properties>
88+
----
89+
====
90+
91+
==== New configuration classes
92+
93+
===== Imperative style
94+
95+
In order configure Spring Data Elasticsearch to use the new client, it is necessary to create a configuration bean that derives from `org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration`:
96+
97+
====
98+
[source,java]
99+
----
100+
@Configuration
101+
public class NewRestClientConfig extends ElasticsearchConfiguration {
102+
103+
@Override
104+
public ClientConfiguration clientConfiguration() {
105+
return ClientConfiguration.builder() //
106+
.connectedTo("localhost:9200") //
107+
.build();
108+
}
109+
}
110+
----
111+
====
112+
113+
The configuration is done in the same way as with the old client, but it is not necessary anymore to create more than the configuration bean.
114+
With this configuration, the following beans will be available in the Spring application context:
115+
116+
* a `RestClient` bean, that is the configured low level `RestClient` that is used by the Elasticsearch client
117+
* an `ElasticsearchClient` bean, this is the new client that uses the `RestClient`
118+
* an `ElasticsearchOperations` bean, available with the bean names _elasticsearchOperations_ and _elasticsearchTemplate_, this uses the `ElasticsearchClient`
119+
120+
===== Reactive style
121+
122+
To use the new client in a reactive environment the only difference is the class from which to derive the configuration:
123+
124+
====
125+
[source,java]
126+
----
127+
@Configuration
128+
public class NewRestClientConfig extends ReactiveElasticsearchConfiguration {
129+
130+
@Override
131+
public ClientConfiguration clientConfiguration() {
132+
return ClientConfiguration.builder() //
133+
.connectedTo("localhost:9200") //
134+
.build();
135+
}
136+
}
137+
----
138+
====
139+
140+
With this configuration, the following beans will be available in the Spring application context:
141+
142+
* a `RestClient` bean, that is the configured low level `RestClient` that is used by the Elasticsearch client
143+
* an `ReactiveElasticsearchClient` bean, this is the new reactive client that uses the `RestClient`
144+
* an `ReactiveElasticsearchOperations` bean, available with the bean names _reactiveElasticsearchOperations_ and _reactiveElasticsearchTemplate_, this uses the `ReactiveElasticsearchClient`

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

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
[[new-features]]
22
= What's new
33

4+
[[new-features.5-0-0]]
5+
== New in Spring Data Elasticsearch 5.0
6+
7+
* Upgrade to Java 17 baseline
8+
* Upgrade to Spring Framework 6
9+
410
[[new-features.4-4-0]]
511
== New in Spring Data Elasticsearch 4.4
612

src/main/asciidoc/reference/migration-guides.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ include::elasticsearch-migration-guide-4.1-4.2.adoc[]
1212
include::elasticsearch-migration-guide-4.2-4.3.adoc[]
1313

1414
include::elasticsearch-migration-guide-4.3-4.4.adoc[]
15+
16+
include::elasticsearch-migration-guide-4.4-5.0.adoc[]
17+
1518
:leveloffset: -1

src/main/java/org/springframework/data/elasticsearch/client/ClientConfiguration.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import javax.net.ssl.SSLContext;
2828

2929
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
30-
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
30+
import org.springframework.data.elasticsearch.client.erhlc.ReactiveRestClients;
31+
import org.springframework.data.elasticsearch.client.erhlc.RestClients;
3132
import org.springframework.http.HttpHeaders;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.web.reactive.function.client.WebClient;
@@ -68,8 +69,8 @@ static ClientConfiguration localhost() {
6869
}
6970

7071
/**
71-
* Creates a new {@link ClientConfiguration} instance configured to a single host given {@code hostAndPort}.
72-
* For example given the endpoint http://localhost:9200
72+
* Creates a new {@link ClientConfiguration} instance configured to a single host given {@code hostAndPort}. For
73+
* example given the endpoint http://localhost:9200
7374
*
7475
* <pre class="code">
7576
* ClientConfiguration configuration = ClientConfiguration.create("localhost:9200");
@@ -82,8 +83,8 @@ static ClientConfiguration create(String hostAndPort) {
8283
}
8384

8485
/**
85-
* Creates a new {@link ClientConfiguration} instance configured to a single host given {@link InetSocketAddress}.
86-
* For example given the endpoint http://localhost:9200
86+
* Creates a new {@link ClientConfiguration} instance configured to a single host given {@link InetSocketAddress}. For
87+
* example given the endpoint http://localhost:9200
8788
*
8889
* <pre class="code">
8990
* ClientConfiguration configuration = ClientConfiguration

src/main/java/org/springframework/data/elasticsearch/client/ClientConfigurationBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import org.springframework.data.elasticsearch.client.ClientConfiguration.ClientConfigurationBuilderWithRequiredEndpoint;
3232
import org.springframework.data.elasticsearch.client.ClientConfiguration.MaybeSecureClientConfigurationBuilder;
3333
import org.springframework.data.elasticsearch.client.ClientConfiguration.TerminalClientConfigurationBuilder;
34-
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
34+
import org.springframework.data.elasticsearch.client.erhlc.ReactiveRestClients;
35+
import org.springframework.data.elasticsearch.client.erhlc.RestClients;
3536
import org.springframework.http.HttpHeaders;
3637
import org.springframework.lang.Nullable;
3738
import org.springframework.util.Assert;

src/main/java/org/springframework/data/elasticsearch/client/elc/JsonUtils.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
import java.io.ByteArrayOutputStream;
2222
import java.io.UnsupportedEncodingException;
23+
import java.nio.charset.StandardCharsets;
2324

2425
import org.apache.commons.logging.Log;
2526
import org.apache.commons.logging.LogFactory;
27+
import org.springframework.lang.Nullable;
2628

2729
/**
2830
* @author Peter-Josef Meisch
@@ -41,13 +43,28 @@ public static String toJson(Object object, JsonpMapper mapper) {
4143
JsonGenerator generator = mapper.jsonProvider().createGenerator(baos);
4244
mapper.serialize(object, generator);
4345
generator.close();
44-
String jsonMapping = "{}";
46+
String json = "{}";
4547
try {
46-
jsonMapping = baos.toString("UTF-8");
48+
json = baos.toString("UTF-8");
4749
} catch (UnsupportedEncodingException e) {
4850
LOGGER.warn("could not read json", e);
4951
}
5052

51-
return jsonMapping;
53+
return json;
5254
}
55+
56+
@Nullable
57+
public static String queryToJson(@Nullable co.elastic.clients.elasticsearch._types.query_dsl.Query query, JsonpMapper mapper) {
58+
59+
if (query == null) {
60+
return null;
61+
}
62+
63+
var baos = new ByteArrayOutputStream();
64+
var generator = mapper.jsonProvider().createGenerator(baos);
65+
query.serialize(generator, mapper);
66+
generator.close();
67+
return baos.toString(StandardCharsets.UTF_8);
68+
}
69+
5370
}

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveClusterTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import co.elastic.clients.elasticsearch.cluster.HealthResponse;
2020
import reactor.core.publisher.Mono;
2121

22+
import org.springframework.data.elasticsearch.client.erhlc.ReactiveClusterOperations;
2223
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth;
23-
import org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations;
2424
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2525

2626
/**

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
import org.springframework.data.elasticsearch.NoSuchIndexException;
4040
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
4141
import org.springframework.data.elasticsearch.client.UnsupportedBackendOperation;
42+
import org.springframework.data.elasticsearch.client.erhlc.ReactiveClusterOperations;
4243
import org.springframework.data.elasticsearch.client.util.ScrollState;
4344
import org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate;
4445
import org.springframework.data.elasticsearch.core.AggregationContainer;
4546
import org.springframework.data.elasticsearch.core.IndexedObjectInformation;
4647
import org.springframework.data.elasticsearch.core.MultiGetItem;
4748
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
4849
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
49-
import org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations;
5050
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
5151
import org.springframework.data.elasticsearch.core.document.Document;
5252
import org.springframework.data.elasticsearch.core.document.SearchDocument;

src/main/java/org/springframework/data/elasticsearch/client/elc/SearchDocumentResponseBuilder.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import org.apache.commons.logging.Log;
3131
import org.apache.commons.logging.LogFactory;
32-
import org.elasticsearch.search.SearchHits;
3332
import org.springframework.data.elasticsearch.core.TotalHitsRelation;
3433
import org.springframework.data.elasticsearch.core.document.SearchDocument;
3534
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
@@ -76,11 +75,11 @@ public static <T> SearchDocumentResponse from(ResponseBody<EntityAsMap> response
7675
}
7776

7877
/**
79-
* creates a {@link SearchDocumentResponseBuilder} from {@link SearchHits} with the given scrollId aggregations and
78+
* creates a {@link SearchDocumentResponseBuilder} from {@link HitsMetadata} with the given scrollId aggregations and
8079
* suggestES
8180
*
8281
* @param <T> entity type
83-
* @param hitsMetadata the {@link SearchHits} to process
82+
* @param hitsMetadata the {@link HitsMetadata} to process
8483
* @param scrollId scrollId
8584
* @param aggregations aggregations
8685
* @param suggestES the suggestion response from Elasticsearch

src/main/java/org/springframework/data/elasticsearch/config/AbstractElasticsearchConfiguration.java renamed to src/main/java/org/springframework/data/elasticsearch/client/erhlc/AbstractElasticsearchConfiguration.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch.config;
16+
package org.springframework.data.elasticsearch.client.erhlc;
1717

1818
import org.elasticsearch.client.RestHighLevelClient;
1919
import org.springframework.context.annotation.Bean;
20+
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
2021
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
21-
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
2222
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2323

2424
/**
2525
* @author Christoph Strobl
2626
* @author Peter-Josef Meisch
2727
* @since 3.2
2828
* @see ElasticsearchConfigurationSupport
29+
* @deprecated since 5.0
2930
*/
31+
@Deprecated
3032
public abstract class AbstractElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
3133

3234
/**
+4-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch.config;
16+
package org.springframework.data.elasticsearch.client.erhlc;
1717

1818
import org.elasticsearch.action.support.IndicesOptions;
1919
import org.springframework.context.annotation.Bean;
20-
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
20+
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
2121
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
22-
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
2322
import org.springframework.data.elasticsearch.core.RefreshPolicy;
2423
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2524
import org.springframework.lang.Nullable;
@@ -29,7 +28,9 @@
2928
* @author Peter-Josef Meisch
3029
* @since 3.2
3130
* @see ElasticsearchConfigurationSupport
31+
* @deprecated since 5.0
3232
*/
33+
@Deprecated
3334
public abstract class AbstractReactiveElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
3435

3536
/**

src/main/java/org/springframework/data/elasticsearch/core/CriteriaFilterProcessor.java renamed to src/main/java/org/springframework/data/elasticsearch/client/erhlc/CriteriaFilterProcessor.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch.core;
16+
package org.springframework.data.elasticsearch.client.erhlc;
1717

1818
import static org.springframework.data.elasticsearch.core.query.Criteria.*;
1919

@@ -47,7 +47,9 @@
4747
* @author Mohsin Husen
4848
* @author Artur Konczak
4949
* @author Peter-Josef Meisch
50+
* @deprecated since 5.0
5051
*/
52+
@Deprecated
5153
class CriteriaFilterProcessor {
5254

5355
@Nullable

src/main/java/org/springframework/data/elasticsearch/core/CriteriaQueryProcessor.java renamed to src/main/java/org/springframework/data/elasticsearch/client/erhlc/CriteriaQueryProcessor.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch.core;
16+
package org.springframework.data.elasticsearch.client.erhlc;
1717

1818
import static org.elasticsearch.index.query.Operator.*;
1919
import static org.elasticsearch.index.query.QueryBuilders.*;
@@ -44,7 +44,9 @@
4444
* @author Rasmus Faber-Espensen
4545
* @author James Bodkin
4646
* @author Peter-Josef Meisch
47+
* @deprecated since 5.0
4748
*/
49+
@Deprecated
4850
class CriteriaQueryProcessor {
4951

5052
@Nullable

0 commit comments

Comments
 (0)