Skip to content

Commit 120ca85

Browse files
authored
Upgrade to Elasticsearch 8.4.2.
Original Pull Request #2301 Closes #2284
1 parent 589b2ad commit 120ca85

File tree

11 files changed

+62
-40
lines changed

11 files changed

+62
-40
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
<springdata.commons>3.0.0-SNAPSHOT</springdata.commons>
2222

2323
<!-- version of the RestHighLevelClient -->
24-
<elasticsearch-rhlc>7.17.5</elasticsearch-rhlc>
24+
<elasticsearch-rhlc>7.17.6</elasticsearch-rhlc>
2525
<!-- version of the new ElasticsearchClient -->
26-
<elasticsearch-java>8.3.3</elasticsearch-java>
26+
<elasticsearch-java>8.4.2</elasticsearch-java>
2727

2828
<log4j>2.18.0</log4j>
2929
<netty>4.1.65.Final</netty>
3030

3131
<blockhound-junit>1.0.6.RELEASE</blockhound-junit>
32-
<hoverfly>0.14.2</hoverfly>
32+
<hoverfly>0.14.3</hoverfly>
3333
<jsonassert>1.5.1</jsonassert>
3434
<testcontainers>1.17.3</testcontainers>
3535
<wiremock>2.33.2</wiremock>

src/main/asciidoc/preface.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +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.3.3 | 6.0.x | 3.0.x?
40+
| 2022.0 (Turing) | 5.0.x | 8.4.2 | 6.0.x | 3.0.x?
4141
| 2021.2 (Raj) | 4.4.x | 7.17.3 | 5.3.x | 2.7.x
4242
| 2021.1 (Q) | 4.3.x | 7.15.2 | 5.3.x | 2.6.x
4343
| 2021.0 (Pascal) | 4.2.xfootnote:oom[Out of maintenance] | 7.12.0 | 5.3.x | 2.5.x

src/main/asciidoc/reference/elasticsearch-migration-guide-4.4-5.0.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ The following classes have been converted to `Record`, you might need to adjust
5757

5858
=== New HttpHeaders class
5959

60-
Until version 4.4 the client configuration used the `HttpHeaders` class from the `org.springframework:spring-web`
60+
Until version 4.4 the client configuration used the `HttpHeaders` class from the `org.springframework:spring-web`
6161
project.
6262
This introduces a dependency on that artifact.
6363
Users that do not use spring-web then face an error as this class cannot be found.
6464

6565
In version 5.0 we introduce our own `HttpHeaders` to configure the clients.
6666

67-
So if you are using headers in the client configuration, you need to replace `org.springframework.http.HttpHeaders`
68-
with `org.springframework.data.elasticsearch.support.HttpHeaders`.
67+
So if you are using headers in the client configuration, you need to replace `org.springframework.http.HttpHeaders`
68+
with `org.springframework.data.elasticsearch.support.HttpHeaders`.
6969

7070
Hint: You can pass a `org.springframework.http
71-
.HttpHeaders` to the `addAll()` method of `org.springframework.data.elasticsearch.support.HttpHeaders`.
71+
.HttpHeaders` to the `addAll()` method of `org.springframework.data.elasticsearch.support.HttpHeaders`.
7272

7373
[[elasticsearch-migration-guide-4.4-5.0.new-clients]]
7474
== New Elasticsearch client
@@ -151,4 +151,4 @@ The old deprecated `RestHighLevelClient` can still be used, but you will need to
151151
----
152152
====
153153

154-
Make sure to specify the version 7.17.5 explicitly, otherwise maven will resolve to 8.3.3, and this does not exist.
154+
Make sure to specify the version 7.17.6 explicitly, otherwise maven will resolve to 8.4.2, and this does not exist.

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.time.Duration;
3030
import java.util.Arrays;
3131
import java.util.List;
32+
import java.util.function.Consumer;
3233
import java.util.function.Function;
3334
import java.util.function.Supplier;
3435
import java.util.stream.Collectors;
@@ -250,23 +251,26 @@ private static ElasticsearchTransport getElasticsearchTransport(RestClient restC
250251
TransportOptions.Builder transportOptionsBuilder = transportOptions != null ? transportOptions.toBuilder()
251252
: new RestClientOptions(RequestOptions.DEFAULT).toBuilder();
252253

253-
// need to add the compatibility header, this is only done automatically when not passing in custom options.
254-
// code copied from RestClientTransport as it is not available outside the package
255-
ContentType jsonContentType = null;
256-
if (Version.VERSION == null) {
257-
jsonContentType = ContentType.APPLICATION_JSON;
258-
} else {
259-
jsonContentType = ContentType.create("application/vnd.elasticsearch+json",
260-
new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));
261-
}
262-
transportOptionsBuilder.addHeader("Accept", jsonContentType.toString());
254+
ContentType jsonContentType = Version.VERSION == null ? ContentType.APPLICATION_JSON
255+
: ContentType.create("application/vnd.elasticsearch+json",
256+
new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));
257+
258+
Consumer<String> setHeaderIfNotPresent = header -> {
259+
if (transportOptionsBuilder.build().headers().stream() //
260+
.noneMatch((h) -> h.getKey().equalsIgnoreCase(header))) {
261+
// need to add the compatibility header, this is only done automatically when not passing in custom options.
262+
// code copied from RestClientTransport as it is not available outside the package
263+
transportOptionsBuilder.addHeader(header, jsonContentType.toString());
264+
}
265+
};
266+
267+
setHeaderIfNotPresent.accept("Content-Type");
268+
setHeaderIfNotPresent.accept("Accept");
263269

264270
TransportOptions transportOptionsWithHeader = transportOptionsBuilder
265271
.addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType).build();
266272

267-
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper(),
268-
transportOptionsWithHeader);
269-
return transport;
273+
return new RestClientTransport(restClient, new JacksonJsonpMapper(), transportOptionsWithHeader);
270274
}
271275

272276
private static List<String> formattedHosts(List<InetSocketAddress> hosts, boolean useSsl) {
@@ -333,7 +337,7 @@ public void process(HttpResponse response, HttpContext context) throws IOExcepti
333337
+ ((header.getName().equals("Authorization")) ? ": *****" : ": " + header.getValue()))
334338
.collect(Collectors.joining(", ", "[", "]"));
335339

336-
// no way of logging the body, in this callback, it is not read yset, later there is no callback possibility in
340+
// no way of logging the body, in this callback, it is not read yet, later there is no callback possibility in
337341
// RestClient or RestClientTransport
338342
ClientLogger.logRawResponse(logId, response.getStatusLine().getStatusCode(), headers);
339343
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ public co.elastic.clients.elasticsearch.core.ReindexRequest reindex(ReindexReque
808808
.maxDocs(reindexRequest.getMaxDocs()).waitForCompletion(waitForCompletion) //
809809
.refresh(reindexRequest.getRefresh()) //
810810
.requireAlias(reindexRequest.getRequireAlias()) //
811-
.requestsPerSecond(reindexRequest.getRequestsPerSecond()) //
811+
.requestsPerSecond(toFloat(reindexRequest.getRequestsPerSecond())) //
812812
.slices(slices(reindexRequest.getSlices()));
813813

814814
return builder.build();
@@ -948,8 +948,7 @@ public UpdateByQueryRequest documentUpdateByQueryRequest(UpdateQuery updateQuery
948948
.script(getScript(updateQuery.getScriptData())) //
949949
.maxDocs(updateQuery.getMaxDocs() != null ? Long.valueOf(updateQuery.getMaxDocs()) : null) //
950950
.pipeline(updateQuery.getPipeline()) //
951-
.requestsPerSecond(
952-
updateQuery.getRequestsPerSecond() != null ? updateQuery.getRequestsPerSecond().longValue() : null) //
951+
.requestsPerSecond(updateQuery.getRequestsPerSecond()) //
953952
.slices(slices(updateQuery.getSlices() != null ? Long.valueOf(updateQuery.getSlices()) : null));
954953

955954
if (updateQuery.getAbortOnVersionConflict() != null) {
@@ -1107,7 +1106,7 @@ public MsearchRequest searchMsearchRequest(
11071106
query.getRescorerQueries().forEach(rescorerQuery -> bb.rescore(getRescore(rescorerQuery)));
11081107

11091108
if (!query.getRuntimeFields().isEmpty()) {
1110-
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
1109+
Map<String, RuntimeField> runtimeMappings = new HashMap<>();
11111110
query.getRuntimeFields().forEach(runtimeField -> {
11121111
RuntimeField esRuntimeField = RuntimeField.of(rt -> {
11131112
RuntimeField.Builder builder = rt
@@ -1119,7 +1118,7 @@ public MsearchRequest searchMsearchRequest(
11191118
}
11201119
return builder;
11211120
});
1122-
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(esRuntimeField));
1121+
runtimeMappings.put(runtimeField.getName(), esRuntimeField);
11231122
});
11241123
bb.runtimeMappings(runtimeMappings);
11251124
}
@@ -1251,12 +1250,11 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
12511250

12521251
if (!query.getRuntimeFields().isEmpty()) {
12531252

1254-
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
1253+
Map<String, RuntimeField> runtimeMappings = new HashMap<>();
12551254
query.getRuntimeFields()
1256-
.forEach(runtimeField -> runtimeMappings.put(runtimeField.getName(),
1257-
Collections.singletonList(RuntimeField.of(rt -> rt //
1258-
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType())) //
1259-
.script(s -> s.inline(is -> is.source(runtimeField.getScript())))))));
1255+
.forEach(runtimeField -> runtimeMappings.put(runtimeField.getName(), RuntimeField.of(rt -> rt //
1256+
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType())) //
1257+
.script(s -> s.inline(is -> is.source(runtimeField.getScript()))))));
12601258
builder.runtimeMappings(runtimeMappings);
12611259
}
12621260

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ClusterHealth clusterHealth(HealthResponse healthResponse) {
9090
.withNumberOfPendingTasks(healthResponse.numberOfPendingTasks()) //
9191
.withRelocatingShards(healthResponse.relocatingShards()) //
9292
.withStatus(healthResponse.status().toString()) //
93-
.withTaskMaxWaitingTimeMillis(healthResponse.taskMaxWaitingInQueueMillis().toEpochMilli()) //
93+
.withTaskMaxWaitingTimeMillis(healthResponse.taskMaxWaitingInQueueMillis()) //
9494
.withTimedOut(healthResponse.timedOut()) //
9595
.withUnassignedShards(healthResponse.unassignedShards()) //
9696
.build(); //
@@ -266,7 +266,7 @@ public ReindexResponse reindexResponse(co.elastic.clients.elasticsearch.core.Rei
266266

267267
// noinspection ConstantConditions
268268
return ReindexResponse.builder() //
269-
.withTook(timeToLong(reindexResponse.took())) //
269+
.withTook(reindexResponse.took()) //
270270
.withTimedOut(reindexResponse.timedOut()) //
271271
.withTotal(reindexResponse.total()) //
272272
.withCreated(reindexResponse.created()) //
@@ -277,9 +277,10 @@ public ReindexResponse reindexResponse(co.elastic.clients.elasticsearch.core.Rei
277277
.withNoops(reindexResponse.noops()) //
278278
.withBulkRetries(reindexResponse.retries().bulk()) //
279279
.withSearchRetries(reindexResponse.retries().search()) //
280-
.withThrottledMillis(reindexResponse.throttledMillis().toEpochMilli()) //
280+
.withThrottledMillis(reindexResponse.throttledMillis()) //
281281
.withRequestsPerSecond(reindexResponse.requestsPerSecond()) //
282-
.withThrottledUntilMillis(reindexResponse.throttledUntilMillis().toEpochMilli()).withFailures(failures) //
282+
.withThrottledUntilMillis(reindexResponse.throttledUntilMillis()) //
283+
.withFailures(failures) //
283284
.build();
284285
}
285286

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,15 @@ static Integer waitForActiveShardsCount(@Nullable String value) {
319319
}
320320
}
321321

322+
/**
323+
* Converts a Long to a Float, returning null if the input is null.
324+
*
325+
* @param value the long value
326+
* @return a FLoat with the given value
327+
* @since 5.0
328+
*/
329+
@Nullable
330+
static Float toFloat(@Nullable Long value) {
331+
return value != null ? Float.valueOf(value) : null;
332+
}
322333
}

src/test/java/org/springframework/data/elasticsearch/client/elc/DevTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.io.IOException;
4040
import java.time.LocalDateTime;
4141
import java.time.format.DateTimeFormatter;
42-
import java.util.Collections;
4342
import java.util.Objects;
4443
import java.util.function.Function;
4544

@@ -108,7 +107,7 @@ void someTest() throws IOException {
108107

109108
client.search(sr -> sr //
110109
.index(index) //
111-
.runtimeMappings("priceWithTax", Collections.singletonList(runtimeField)), //
110+
.runtimeMappings("priceWithTax", runtimeField), //
112111
Person.class); //
113112
}
114113

src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchOperationsProducer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public ElasticsearchClient elasticsearchClient() {
6868
configurationBuilder.withBasicAuth(user, password);
6969
}
7070

71+
String proxy = System.getenv("DATAES_ELASTICSEARCH_PROXY");
72+
73+
if (hasText(proxy)) {
74+
configurationBuilder.withProxy(proxy);
75+
}
76+
7177
ClientConfiguration clientConfiguration = configurationBuilder //
7278
.build();
7379

src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/QueryKeywordsIntegrationTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.stream.Collectors;
2424

2525
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Disabled;
2627
import org.junit.jupiter.api.DisplayName;
2728
import org.junit.jupiter.api.Order;
2829
import org.junit.jupiter.api.Test;
@@ -265,6 +266,7 @@ void shouldReturnEmptyListOnDerivedMethodWithEmptyInputList() {
265266
assertThat(products).isEmpty();
266267
}
267268

269+
@Disabled("issue #2300, Elasticsearch bug https://github.com/elastic/elasticsearch/issues/89760")
268270
@Test // #1909
269271
@DisplayName("should find by property exists")
270272
void shouldFindByPropertyExists() {
@@ -274,6 +276,7 @@ void shouldFindByPropertyExists() {
274276
assertThat(searchHits.getTotalHits()).isEqualTo(6);
275277
}
276278

279+
@Disabled("issue #2300, Elasticsearch bug https://github.com/elastic/elasticsearch/issues/89760")
277280
@Test // #1909
278281
@DisplayName("should find by property is not null")
279282
void shouldFindByPropertyIsNotNull() {

src/test/resources/testcontainers-elasticsearch.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
#
1717
sde.testcontainers.image-name=docker.elastic.co/elasticsearch/elasticsearch
18-
sde.testcontainers.image-version=8.3.3
18+
sde.testcontainers.image-version=8.4.2
1919
#
2020
#
2121
# needed as we do a DELETE /* at the end of the tests, will be required from 8.0 on, produces a warning since 7.13

0 commit comments

Comments
 (0)