Skip to content

Commit ac64a6a

Browse files
authored
Use Elasticsearch 8.2.2.
Original Pull Request #2174 Closes #2158
1 parent c826adb commit ac64a6a

File tree

42 files changed

+337
-152
lines changed

Some content is hidden

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

42 files changed

+337
-152
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<!-- version of the RestHighLevelClient -->
2222
<elasticsearch-rhlc>7.17.3</elasticsearch-rhlc>
2323
<!-- version of the new ElasticsearchClient -->
24-
<elasticsearch-java>7.17.3</elasticsearch-java>
25-
<log4j>2.17.1</log4j>
24+
<elasticsearch-java>8.2.2</elasticsearch-java>
25+
<log4j>2.17.2</log4j>
2626
<netty>4.1.65.Final</netty>
2727
<springdata.commons>3.0.0-SNAPSHOT</springdata.commons>
2828
<testcontainers>1.16.2</testcontainers>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.client.RestClient;
4444
import org.elasticsearch.client.RestClientBuilder;
4545
import org.elasticsearch.client.RestHighLevelClient;
46+
import org.elasticsearch.client.RestHighLevelClientBuilder;
4647
import org.springframework.http.HttpHeaders;
4748
import org.springframework.http.HttpStatus;
4849
import org.springframework.util.Assert;
@@ -132,7 +133,7 @@ public static ElasticsearchRestClient create(ClientConfiguration clientConfigura
132133
return clientBuilder;
133134
});
134135

135-
RestHighLevelClient client = new RestHighLevelClient(builder);
136+
RestHighLevelClient client = new RestHighLevelClientBuilder(builder.build()).setApiCompatibilityMode(true).build();
136137
return () -> client;
137138
}
138139

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

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
2020
import co.elastic.clients.transport.ElasticsearchTransport;
2121
import co.elastic.clients.transport.TransportOptions;
22+
import co.elastic.clients.transport.Version;
2223
import co.elastic.clients.transport.rest_client.RestClientOptions;
2324
import co.elastic.clients.transport.rest_client.RestClientTransport;
2425

@@ -41,8 +42,10 @@
4142
import org.apache.http.HttpResponseInterceptor;
4243
import org.apache.http.client.config.RequestConfig;
4344
import org.apache.http.entity.ByteArrayEntity;
45+
import org.apache.http.entity.ContentType;
4446
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
4547
import org.apache.http.message.BasicHeader;
48+
import org.apache.http.message.BasicNameValuePair;
4649
import org.apache.http.protocol.HttpContext;
4750
import org.elasticsearch.client.RequestOptions;
4851
import org.elasticsearch.client.RestClient;
@@ -242,6 +245,18 @@ private static ElasticsearchTransport getElasticsearchTransport(RestClient restC
242245

243246
TransportOptions.Builder transportOptionsBuilder = transportOptions != null ? transportOptions.toBuilder()
244247
: new RestClientOptions(RequestOptions.DEFAULT).toBuilder();
248+
249+
// need to add the compatibility header, this is only done automatically when not passing in custom options.
250+
// code copied from RestClientTransport as it is not available outside the package
251+
ContentType jsonContentType = null;
252+
if (Version.VERSION == null) {
253+
jsonContentType = ContentType.APPLICATION_JSON;
254+
} else {
255+
jsonContentType = ContentType.create("application/vnd.elasticsearch+json",
256+
new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));
257+
}
258+
transportOptionsBuilder.addHeader("Accept", jsonContentType.toString());
259+
245260
TransportOptions transportOptionsWithHeader = transportOptionsBuilder
246261
.addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType).build();
247262

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,18 @@ public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptio
273273
// endregion
274274

275275
@Override
276-
protected String getClusterVersion() {
276+
public String getClusterVersion() {
277277
return execute(client -> client.info().version().number());
278278

279279
}
280280

281281
@Override
282-
protected String getVendor() {
282+
public String getVendor() {
283283
return "Elasticsearch";
284284
}
285285

286286
@Override
287-
protected String getRuntimeLibraryVersion() {
287+
public String getRuntimeLibraryVersion() {
288288
return Version.VERSION.toString();
289289
}
290290

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,17 @@ public Flux<? extends AggregationContainer<?>> aggregate(Query query, Class<?> e
413413
// endregion
414414

415415
@Override
416-
protected Mono<String> getVendor() {
416+
public Mono<String> getVendor() {
417417
return Mono.just("Elasticsearch");
418418
}
419419

420420
@Override
421-
protected Mono<String> getRuntimeLibraryVersion() {
421+
public Mono<String> getRuntimeLibraryVersion() {
422422
return Mono.just(Version.VERSION != null ? Version.VERSION.toString() : "null");
423423
}
424424

425425
@Override
426-
protected Mono<String> getClusterVersion() {
426+
public Mono<String> getClusterVersion() {
427427
return Mono.from(execute(ReactiveElasticsearchClient::info)).map(infoResponse -> infoResponse.version().number());
428428
}
429429

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ public co.elastic.clients.elasticsearch.core.ReindexRequest reindex(ReindexReque
820820
.refresh(reindexRequest.getRefresh()) //
821821
.requireAlias(reindexRequest.getRequireAlias()) //
822822
.requestsPerSecond(reindexRequest.getRequestsPerSecond()) //
823-
.slices(reindexRequest.getSlices());
823+
.slices(slices(reindexRequest.getSlices()));
824824

825825
return builder.build();
826826
}
@@ -963,24 +963,24 @@ public UpdateByQueryRequest documentUpdateByQueryRequest(UpdateQuery updateQuery
963963
.pipeline(updateQuery.getPipeline()) //
964964
.requestsPerSecond(
965965
updateQuery.getRequestsPerSecond() != null ? updateQuery.getRequestsPerSecond().longValue() : null) //
966-
.slices(updateQuery.getSlices() != null ? Long.valueOf(updateQuery.getSlices()) : null) //
967-
;
966+
.slices(slices(updateQuery.getSlices() != null ? Long.valueOf(updateQuery.getSlices()) : null));
968967

969968
if (updateQuery.getAbortOnVersionConflict() != null) {
970969
ub.conflicts(updateQuery.getAbortOnVersionConflict() ? Conflicts.Abort : Conflicts.Proceed);
971970
}
972971

973-
if (updateQuery.getBatchSize() != null) {
974-
ub.size(Long.valueOf(updateQuery.getBatchSize()));
975-
}
976-
977972
if (updateQuery.getQuery() != null) {
978973
Query queryQuery = updateQuery.getQuery();
974+
975+
if (updateQuery.getBatchSize() != null) {
976+
((BaseQuery) queryQuery).setMaxResults(updateQuery.getBatchSize());
977+
}
979978
ub.query(getQuery(queryQuery, null));
980979

981980
// no indicesOptions available like in old client
982981

983982
ub.scroll(time(queryQuery.getScrollTime()));
983+
984984
}
985985

986986
// no maxRetries available like in old client
@@ -1164,11 +1164,11 @@ private <T> void prepareSearchRequest(Query query, @Nullable Class<T> clazz, Ind
11641164

11651165
if (!query.getRuntimeFields().isEmpty()) {
11661166

1167-
Map<String, RuntimeField> runtimeMappings = new HashMap<>();
1167+
Map<String, List<RuntimeField>> runtimeMappings = new HashMap<>();
11681168
query.getRuntimeFields().forEach(runtimeField -> {
1169-
runtimeMappings.put(runtimeField.getName(), RuntimeField.of(rt -> rt //
1169+
runtimeMappings.put(runtimeField.getName(), Collections.singletonList(RuntimeField.of(rt -> rt //
11701170
.type(RuntimeFieldType._DESERIALIZER.parse(runtimeField.getType())) //
1171-
.script(s -> s.inline(is -> is.source(runtimeField.getScript())))));
1171+
.script(s -> s.inline(is -> is.source(runtimeField.getScript()))))));
11721172
});
11731173
builder.runtimeMappings(runtimeMappings);
11741174
}
@@ -1328,6 +1328,7 @@ private co.elastic.clients.elasticsearch._types.query_dsl.Query getQuery(@Nullab
13281328
} else {
13291329
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
13301330
}
1331+
13311332
return esQuery;
13321333
}
13331334

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

+8-8
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(Long.parseLong(healthResponse.taskMaxWaitingInQueueMillis())) //
93+
.withTaskMaxWaitingTimeMillis(healthResponse.taskMaxWaitingInQueueMillis().toEpochMilli()) //
9494
.withTimedOut(healthResponse.timedOut()) //
9595
.withUnassignedShards(healthResponse.unassignedShards()) //
9696
.build(); //
@@ -144,11 +144,11 @@ public Document indicesGetMapping(GetMappingResponse getMappingResponse, IndexCo
144144
if (indexMappingRecord == null) {
145145

146146
if (mappings.size() != 1) {
147-
LOGGER.warn("no mapping returned for index {}", indexCoordinates.getIndexName());
148-
return Document.create();
149-
}
150-
String index = mappings.keySet().iterator().next();
151-
indexMappingRecord = mappings.get(index);
147+
LOGGER.warn("no mapping returned for index {}", indexCoordinates.getIndexName());
148+
return Document.create();
149+
}
150+
String index = mappings.keySet().iterator().next();
151+
indexMappingRecord = mappings.get(index);
152152
}
153153

154154
return Document.parse(toJson(indexMappingRecord.mappings(), jsonpMapper));
@@ -277,9 +277,9 @@ 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(Long.parseLong(reindexResponse.throttledMillis())) //
280+
.withThrottledMillis(reindexResponse.throttledMillis().toEpochMilli()) //
281281
.withRequestsPerSecond(reindexResponse.requestsPerSecond()) //
282-
.withThrottledUntilMillis(Long.parseLong(reindexResponse.throttledUntilMillis())).withFailures(failures) //
282+
.withThrottledUntilMillis(reindexResponse.throttledUntilMillis().toEpochMilli()).withFailures(failures) //
283283
.build();
284284
}
285285

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import co.elastic.clients.elasticsearch._types.*;
1919
import co.elastic.clients.elasticsearch._types.mapping.FieldType;
2020
import co.elastic.clients.elasticsearch.core.search.BoundaryScanner;
21-
import co.elastic.clients.elasticsearch.core.search.BuiltinHighlighterType;
2221
import co.elastic.clients.elasticsearch.core.search.HighlighterEncoder;
2322
import co.elastic.clients.elasticsearch.core.search.HighlighterFragmenter;
2423
import co.elastic.clients.elasticsearch.core.search.HighlighterOrder;
@@ -170,11 +169,11 @@ static HighlighterType highlighterType(@Nullable String value) {
170169
if (value != null) {
171170
switch (value.toLowerCase()) {
172171
case "unified":
173-
return HighlighterType.of(b -> b.builtin(BuiltinHighlighterType.Unified));
172+
return HighlighterType.Unified;
174173
case "plain":
175-
return HighlighterType.of(b -> b.builtin(BuiltinHighlighterType.Plain));
174+
return HighlighterType.Plain;
176175
case "fvh":
177-
return HighlighterType.of(b -> b.builtin(BuiltinHighlighterType.FastVector));
176+
return HighlighterType.FastVector;
178177
default:
179178
return null;
180179
}
@@ -308,6 +307,16 @@ static SearchType searchType(@Nullable Query.SearchType searchType) {
308307
return null;
309308
}
310309

310+
@Nullable
311+
static Slices slices(@Nullable Long count) {
312+
313+
if (count == null) {
314+
return null;
315+
}
316+
317+
return Slices.of(s -> s.value(Math.toIntExact(count)));
318+
}
319+
311320
@Nullable
312321
static SortMode sortMode(Order.Mode mode) {
313322

src/main/java/org/springframework/data/elasticsearch/client/reactive/WebClientProvider.java

+10
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ static WebClientProvider getWebClientProvider(ClientConfiguration clientConfigur
257257
if (suppliedHeaders != null && suppliedHeaders != HttpHeaders.EMPTY) {
258258
httpHeaders.addAll(suppliedHeaders);
259259
}
260+
261+
// this WebClientProvider is built with ES 7 and not used on 8 anymore
262+
httpHeaders.add("Accept", "application/vnd.elasticsearch+json;compatible-with=7");
263+
264+
var contentTypeHeader = "Content-Type";
265+
if (httpHeaders.containsKey(contentTypeHeader)) {
266+
httpHeaders.remove(contentTypeHeader);
267+
}
268+
httpHeaders.add(contentTypeHeader, "application/vnd.elasticsearch+json;compatible-with=7");
269+
260270
}));
261271

262272
return provider;

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
145145

146146
/**
147147
* Set the {@link EntityCallbacks} instance to use when invoking {@link EntityCallbacks callbacks} like the
148-
* {@link org.springframework.data.elasticsearch.core.event.BeforeConvertCallback}.
149-
* Overrides potentially existing {@link EntityCallbacks}.
148+
* {@link org.springframework.data.elasticsearch.core.event.BeforeConvertCallback}. Overrides potentially existing
149+
* {@link EntityCallbacks}.
150150
*
151151
* @param entityCallbacks must not be {@literal null}.
152152
* @throws IllegalArgumentException if the given instance is {@literal null}.
@@ -588,19 +588,19 @@ protected <T> SearchDocumentResponse.EntityCreator<T> getEntityCreator(ReadDocum
588588
* @return the version as string if it can be retrieved
589589
*/
590590
@Nullable
591-
abstract protected String getClusterVersion();
591+
public abstract String getClusterVersion();
592592

593593
/**
594594
* @return the vendor name of the used cluster and client library
595595
* @since 4.3
596596
*/
597-
abstract protected String getVendor();
597+
public abstract String getVendor();
598598

599599
/**
600600
* @return the version of the used client runtime library.
601601
* @since 4.3
602602
*/
603-
abstract protected String getRuntimeLibraryVersion();
603+
public abstract String getRuntimeLibraryVersion();
604604

605605
// endregion
606606

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,15 @@ public ElasticsearchPersistentEntity<?> getPersistentEntityFor(@Nullable Class<?
611611
* @return the vendor name of the used cluster and client library
612612
* @since 4.3
613613
*/
614-
abstract protected Mono<String> getVendor();
614+
public abstract Mono<String> getVendor();
615615

616616
/**
617617
* @return the version of the used client runtime library.
618618
* @since 4.3
619619
*/
620-
abstract protected Mono<String> getRuntimeLibraryVersion();
620+
public abstract Mono<String> getRuntimeLibraryVersion();
621621

622-
abstract protected Mono<String> getClusterVersion();
622+
public abstract Mono<String> getClusterVersion();
623623

624624
/**
625625
* Value class to capture client independent information from a response to an index request.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ private RuntimeException translateException(Exception exception) {
607607

608608
// region helper methods
609609
@Override
610-
protected String getClusterVersion() {
610+
public String getClusterVersion() {
611611
try {
612612
return execute(client -> client.info(RequestOptions.DEFAULT)).getVersion().getNumber();
613613
} catch (Exception ignored) {}
@@ -629,12 +629,12 @@ public Query idsQuery(List<String> ids) {
629629
}
630630

631631
@Override
632-
protected String getVendor() {
632+
public String getVendor() {
633633
return "Elasticsearch";
634634
}
635635

636636
@Override
637-
protected String getRuntimeLibraryVersion() {
637+
public String getRuntimeLibraryVersion() {
638638
return Version.CURRENT.toString();
639639
}
640640

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ protected SearchRequest prepareSearchRequest(SearchRequest request, boolean useS
676676

677677
// region Helper methods
678678
@Override
679-
protected Mono<String> getClusterVersion() {
679+
public Mono<String> getClusterVersion() {
680680
try {
681681
return Mono.from(execute(ReactiveElasticsearchClient::info))
682682
.map(mainResponse -> mainResponse.getVersion().toString());
@@ -689,7 +689,7 @@ protected Mono<String> getClusterVersion() {
689689
* @since 4.3
690690
*/
691691
@Override
692-
protected Mono<String> getVendor() {
692+
public Mono<String> getVendor() {
693693
return Mono.just("Elasticsearch");
694694
}
695695

@@ -698,7 +698,7 @@ protected Mono<String> getVendor() {
698698
* @since 4.3
699699
*/
700700
@Override
701-
protected Mono<String> getRuntimeLibraryVersion() {
701+
public Mono<String> getRuntimeLibraryVersion() {
702702
return Mono.just(Version.CURRENT.toString());
703703
}
704704

0 commit comments

Comments
 (0)