Skip to content

Commit bbd5e81

Browse files
authored
Component templates.
Original Pull Request #2534 Closes #1458
1 parent df7a614 commit bbd5e81

File tree

44 files changed

+2282
-409
lines changed

Some content is hidden

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

44 files changed

+2282
-409
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ But besides the values defined by the enum, it is possible to have similarities
1414
Therefore, the annotation property was changed from the type of the enum to a simple `String`.
1515
The previous enum values like `Similarity.Default` do still exist as String constants, so existing code will compile unmodified.
1616
Adaptions are necessary when this enum was used at other places than as a property of the `@Field` annotation.
17+
18+
[[elasticsearch-migration-guide-5.0-5.1.deprecations]]
19+
== Deprecations
20+
21+
=== template functions
22+
23+
The functions in the `IndexOperations` and `ReactiverIndexOperations` to manage index templates that were introduced in Spring Data Elasticsearch 4.1
24+
have been deprecated. They were using the old Elasticsearch API that was deprecated in Elasticsearch version 7.8.
25+
26+
Please use the new functions that are based on the compsable index template API instead.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,20 @@ protected AbstractElasticsearchTemplate doCopy() {
115115
// region child templates
116116
@Override
117117
public IndexOperations indexOps(Class<?> clazz) {
118-
return new IndicesTemplate(client.indices(), elasticsearchConverter, clazz);
118+
return new IndicesTemplate(client.indices(), getClusterTemplate(), elasticsearchConverter, clazz);
119119
}
120120

121121
@Override
122122
public IndexOperations indexOps(IndexCoordinates index) {
123-
return new IndicesTemplate(client.indices(), elasticsearchConverter, index);
123+
return new IndicesTemplate(client.indices(), getClusterTemplate(), elasticsearchConverter, index);
124124
}
125125

126126
@Override
127127
public ClusterOperations cluster() {
128+
return getClusterTemplate();
129+
}
130+
131+
private ClusterTemplate getClusterTemplate() {
128132
return new ClusterTemplate(client.cluster(), elasticsearchConverter);
129133
}
130134
// endregion

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

Lines changed: 97 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
import org.springframework.data.elasticsearch.core.ResourceUtil;
3838
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
3939
import org.springframework.data.elasticsearch.core.document.Document;
40-
import org.springframework.data.elasticsearch.core.index.AliasActions;
41-
import org.springframework.data.elasticsearch.core.index.AliasData;
40+
import org.springframework.data.elasticsearch.core.index.*;
41+
import org.springframework.data.elasticsearch.core.index.DeleteIndexTemplateRequest;
4242
import org.springframework.data.elasticsearch.core.index.DeleteTemplateRequest;
43+
import org.springframework.data.elasticsearch.core.index.ExistsIndexTemplateRequest;
4344
import org.springframework.data.elasticsearch.core.index.ExistsTemplateRequest;
45+
import org.springframework.data.elasticsearch.core.index.GetIndexTemplateRequest;
4446
import org.springframework.data.elasticsearch.core.index.GetTemplateRequest;
45-
import org.springframework.data.elasticsearch.core.index.MappingBuilder;
47+
import org.springframework.data.elasticsearch.core.index.PutIndexTemplateRequest;
4648
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
47-
import org.springframework.data.elasticsearch.core.index.Settings;
48-
import org.springframework.data.elasticsearch.core.index.TemplateData;
4949
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
5050
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
5151
import org.springframework.lang.Nullable;
@@ -62,30 +62,37 @@ public class IndicesTemplate extends ChildTemplate<ElasticsearchTransport, Elast
6262

6363
private static final Logger LOGGER = LoggerFactory.getLogger(IndicesTemplate.class);
6464

65+
// we need a cluster client as well because ES has put some methods from the indices API into the cluster client
66+
// (component templates)
67+
private final ClusterTemplate clusterTemplate;
6568
protected final ElasticsearchConverter elasticsearchConverter;
6669
@Nullable protected final Class<?> boundClass;
6770
@Nullable protected final IndexCoordinates boundIndex;
6871

69-
public IndicesTemplate(ElasticsearchIndicesClient client, ElasticsearchConverter elasticsearchConverter,
70-
Class<?> boundClass) {
72+
public IndicesTemplate(ElasticsearchIndicesClient client, ClusterTemplate clusterTemplate,
73+
ElasticsearchConverter elasticsearchConverter, Class<?> boundClass) {
7174
super(client, elasticsearchConverter);
7275

76+
Assert.notNull(clusterTemplate, "cluster must not be null");
7377
Assert.notNull(elasticsearchConverter, "elasticsearchConverter must not be null");
7478
Assert.notNull(boundClass, "boundClass may not be null");
7579

80+
this.clusterTemplate = clusterTemplate;
7681
this.elasticsearchConverter = elasticsearchConverter;
7782
this.boundClass = boundClass;
7883
this.boundIndex = null;
7984

8085
}
8186

82-
public IndicesTemplate(ElasticsearchIndicesClient client, ElasticsearchConverter elasticsearchConverter,
83-
IndexCoordinates boundIndex) {
87+
public IndicesTemplate(ElasticsearchIndicesClient client, ClusterTemplate clusterTemplate,
88+
ElasticsearchConverter elasticsearchConverter, IndexCoordinates boundIndex) {
8489
super(client, elasticsearchConverter);
8590

91+
Assert.notNull(clusterTemplate, "cluster must not be null");
8692
Assert.notNull(elasticsearchConverter, "elasticsearchConverter must not be null");
8793
Assert.notNull(boundIndex, "boundIndex must not be null");
8894

95+
this.clusterTemplate = clusterTemplate;
8996
this.elasticsearchConverter = elasticsearchConverter;
9097
this.boundClass = null;
9198
this.boundIndex = boundIndex;
@@ -338,6 +345,87 @@ public boolean deleteTemplate(DeleteTemplateRequest deleteTemplateRequest) {
338345
return execute(client -> client.deleteTemplate(deleteTemplateRequestES)).acknowledged();
339346
}
340347

348+
@Override
349+
public boolean putIndexTemplate(PutIndexTemplateRequest putIndexTemplateRequest) {
350+
351+
co.elastic.clients.elasticsearch.indices.PutIndexTemplateRequest putIndexTemplateRequestES = requestConverter
352+
.indicesPutIndexTemplateRequest(putIndexTemplateRequest);
353+
354+
return execute(client -> client.putIndexTemplate(putIndexTemplateRequestES)).acknowledged();
355+
}
356+
357+
@Override
358+
public boolean existsIndexTemplate(ExistsIndexTemplateRequest existsIndexTemplateRequest) {
359+
360+
Assert.notNull(existsIndexTemplateRequest, "existsIndexTemplateRequest must not be null");
361+
362+
co.elastic.clients.elasticsearch.indices.ExistsIndexTemplateRequest existsTemplateRequestES = requestConverter
363+
.indicesExistsIndexTemplateRequest(existsIndexTemplateRequest);
364+
return execute(client -> client.existsIndexTemplate(existsTemplateRequestES)).value();
365+
}
366+
367+
@Override
368+
public List<TemplateResponse> getIndexTemplate(GetIndexTemplateRequest getIndexTemplateRequest) {
369+
370+
Assert.notNull(getIndexTemplateRequest, "getIndexTemplateRequest must not be null");
371+
372+
co.elastic.clients.elasticsearch.indices.GetIndexTemplateRequest getIndexTemplateRequestES = requestConverter
373+
.indicesGetIndexTemplateRequest(getIndexTemplateRequest);
374+
var getIndexTemplateResponse = execute(client -> client.getIndexTemplate(getIndexTemplateRequestES));
375+
return responseConverter.getIndexTemplates(getIndexTemplateResponse);
376+
}
377+
378+
@Override
379+
public boolean deleteIndexTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequest) {
380+
381+
Assert.notNull(deleteIndexTemplateRequest, "deleteIndexTemplateRequest must not be null");
382+
383+
co.elastic.clients.elasticsearch.indices.DeleteIndexTemplateRequest deleteIndexTemplateRequestES = requestConverter
384+
.indicesDeleteIndexTemplateRequest(deleteIndexTemplateRequest);
385+
return execute(client -> client.deleteIndexTemplate(deleteIndexTemplateRequestES)).acknowledged();
386+
}
387+
388+
@Override
389+
public boolean putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest) {
390+
391+
Assert.notNull(putComponentTemplateRequest, "putComponentTemplateRequest must not be null");
392+
393+
co.elastic.clients.elasticsearch.cluster.PutComponentTemplateRequest putComponentTemplateRequestES = requestConverter
394+
.clusterPutComponentTemplateRequest(putComponentTemplateRequest);
395+
// the new Elasticsearch client has this call in the cluster index
396+
return clusterTemplate.execute(client -> client.putComponentTemplate(putComponentTemplateRequestES)).acknowledged();
397+
}
398+
399+
@Override
400+
public boolean existsComponentTemplate(ExistsComponentTemplateRequest existsComponentTemplateRequest) {
401+
402+
Assert.notNull(existsComponentTemplateRequest, "existsComponentTemplateRequest must not be null");
403+
404+
co.elastic.clients.elasticsearch.cluster.ExistsComponentTemplateRequest existsComponentTemplateRequestES = requestConverter
405+
.clusterExistsComponentTemplateRequest(existsComponentTemplateRequest);
406+
return clusterTemplate.execute(client -> client.existsComponentTemplate(existsComponentTemplateRequestES)).value();
407+
}
408+
409+
@Override
410+
public List<TemplateResponse> getComponentTemplate(GetComponentTemplateRequest getComponentTemplateRequest) {
411+
412+
co.elastic.clients.elasticsearch.cluster.GetComponentTemplateRequest getComponentTemplateRequestES = requestConverter
413+
.clusterGetComponentTemplateRequest(getComponentTemplateRequest);
414+
var response = clusterTemplate.execute(client -> client.getComponentTemplate(getComponentTemplateRequestES));
415+
return responseConverter.clusterGetComponentTemplates(response);
416+
}
417+
418+
@Override
419+
public boolean deleteComponentTemplate(DeleteComponentTemplateRequest deleteComponentTemplateRequest) {
420+
421+
Assert.notNull(deleteComponentTemplateRequest, "deleteComponentTemplateRequest must not be null");
422+
423+
co.elastic.clients.elasticsearch.cluster.DeleteComponentTemplateRequest deleteComponentTemplateRequestES = requestConverter
424+
.clusterDeleteComponentTemplateRequest(deleteComponentTemplateRequest);
425+
return clusterTemplate.execute(client -> client.deleteComponentTemplate(deleteComponentTemplateRequestES))
426+
.acknowledged();
427+
}
428+
341429
@Override
342430
public List<IndexInformation> getInformation(IndexCoordinates indexCoordinates) {
343431

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import co.elastic.clients.transport.ElasticsearchTransport;
2121
import reactor.core.publisher.Mono;
2222

23-
import org.springframework.data.elasticsearch.client.erhlc.ReactiveClusterOperations;
2423
import org.springframework.data.elasticsearch.core.cluster.ClusterHealth;
24+
import org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations;
2525
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2626

2727
/**

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package org.springframework.data.elasticsearch.client.elc;
1717

1818
import co.elastic.clients.ApiClient;
19-
import co.elastic.clients.elasticsearch.cluster.HealthRequest;
20-
import co.elastic.clients.elasticsearch.cluster.HealthResponse;
19+
import co.elastic.clients.elasticsearch.cluster.*;
2120
import co.elastic.clients.transport.ElasticsearchTransport;
2221
import co.elastic.clients.transport.TransportOptions;
22+
import co.elastic.clients.transport.endpoints.BooleanResponse;
2323
import co.elastic.clients.util.ObjectBuilder;
2424
import reactor.core.publisher.Mono;
2525

@@ -53,4 +53,47 @@ public Mono<HealthResponse> health(HealthRequest healthRequest) {
5353
public Mono<HealthResponse> health(Function<HealthRequest.Builder, ObjectBuilder<HealthRequest>> fn) {
5454
return health(fn.apply(new HealthRequest.Builder()).build());
5555
}
56+
57+
public Mono<PutComponentTemplateResponse> putComponentTemplate(
58+
PutComponentTemplateRequest putComponentTemplateRequest) {
59+
return Mono.fromFuture(transport.performRequestAsync(putComponentTemplateRequest,
60+
PutComponentTemplateRequest._ENDPOINT, transportOptions));
61+
}
62+
63+
public Mono<PutComponentTemplateResponse> putComponentTemplate(
64+
Function<PutComponentTemplateRequest.Builder, ObjectBuilder<PutComponentTemplateRequest>> fn) {
65+
return putComponentTemplate(fn.apply(new PutComponentTemplateRequest.Builder()).build());
66+
}
67+
68+
public Mono<GetComponentTemplateResponse> getComponentTemplate(
69+
GetComponentTemplateRequest getComponentTemplateRequest) {
70+
return Mono.fromFuture(transport.performRequestAsync(getComponentTemplateRequest,
71+
GetComponentTemplateRequest._ENDPOINT, transportOptions));
72+
}
73+
74+
public Mono<GetComponentTemplateResponse> getComponentTemplate(
75+
Function<GetComponentTemplateRequest.Builder, ObjectBuilder<GetComponentTemplateRequest>> fn) {
76+
return getComponentTemplate(fn.apply(new GetComponentTemplateRequest.Builder()).build());
77+
}
78+
79+
public Mono<BooleanResponse> existsComponentTemplate(ExistsComponentTemplateRequest existsComponentTemplateRequest) {
80+
return Mono.fromFuture(transport.performRequestAsync(existsComponentTemplateRequest,
81+
ExistsComponentTemplateRequest._ENDPOINT, transportOptions));
82+
}
83+
84+
public Mono<BooleanResponse> existsComponentTemplate(
85+
Function<ExistsComponentTemplateRequest.Builder, ObjectBuilder<ExistsComponentTemplateRequest>> fn) {
86+
return existsComponentTemplate(fn.apply(new ExistsComponentTemplateRequest.Builder()).build());
87+
}
88+
89+
public Mono<DeleteComponentTemplateResponse> deleteComponentTemplate(
90+
DeleteComponentTemplateRequest deleteComponentTemplateRequest) {
91+
return Mono.fromFuture(transport.performRequestAsync(deleteComponentTemplateRequest,
92+
DeleteComponentTemplateRequest._ENDPOINT, transportOptions));
93+
}
94+
95+
public Mono<DeleteComponentTemplateResponse> deleteComponentTemplate(
96+
Function<DeleteComponentTemplateRequest.Builder, ObjectBuilder<DeleteComponentTemplateRequest>> fn) {
97+
return deleteComponentTemplate(fn.apply(new DeleteComponentTemplateRequest.Builder()).build());
98+
}
5699
}

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client.elc;
1717

18-
import static co.elastic.clients.util.ApiTypeHelper.*;
19-
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
18+
import static co.elastic.clients.util.ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck;
19+
import static org.springframework.data.elasticsearch.client.elc.TypeUtils.result;
2020

2121
import co.elastic.clients.elasticsearch._types.Result;
2222
import co.elastic.clients.elasticsearch.core.*;
@@ -46,24 +46,14 @@
4646
import org.springframework.data.elasticsearch.NoSuchIndexException;
4747
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
4848
import org.springframework.data.elasticsearch.client.UnsupportedBackendOperation;
49-
import org.springframework.data.elasticsearch.client.erhlc.ReactiveClusterOperations;
50-
import org.springframework.data.elasticsearch.core.AbstractReactiveElasticsearchTemplate;
51-
import org.springframework.data.elasticsearch.core.AggregationContainer;
52-
import org.springframework.data.elasticsearch.core.IndexedObjectInformation;
53-
import org.springframework.data.elasticsearch.core.MultiGetItem;
54-
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
55-
import org.springframework.data.elasticsearch.core.ReactiveIndexOperations;
49+
import org.springframework.data.elasticsearch.core.*;
50+
import org.springframework.data.elasticsearch.core.cluster.ReactiveClusterOperations;
5651
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
5752
import org.springframework.data.elasticsearch.core.document.Document;
5853
import org.springframework.data.elasticsearch.core.document.SearchDocument;
5954
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
6055
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
61-
import org.springframework.data.elasticsearch.core.query.BaseQuery;
62-
import org.springframework.data.elasticsearch.core.query.BulkOptions;
63-
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
64-
import org.springframework.data.elasticsearch.core.query.Query;
65-
import org.springframework.data.elasticsearch.core.query.SearchTemplateQuery;
66-
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
56+
import org.springframework.data.elasticsearch.core.query.*;
6757
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
6858
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
6959
import org.springframework.data.elasticsearch.core.reindex.ReindexResponse;
@@ -616,16 +606,23 @@ public <T> Publisher<T> executeWithClusterClient(ClusterClientCallback<Publisher
616606

617607
@Override
618608
public ReactiveIndexOperations indexOps(IndexCoordinates index) {
619-
return new ReactiveIndicesTemplate(client.indices(), converter, index);
609+
return new ReactiveIndicesTemplate(client.indices(), getReactiveClusterTemplate(), converter, index);
620610
}
621611

622612
@Override
623613
public ReactiveIndexOperations indexOps(Class<?> clazz) {
624-
return new ReactiveIndicesTemplate(client.indices(), converter, clazz);
614+
return new ReactiveIndicesTemplate(client.indices(), getReactiveClusterTemplate(), converter, clazz);
625615
}
626616

627617
@Override
628618
public ReactiveClusterOperations cluster() {
619+
return getReactiveClusterTemplate();
620+
}
621+
622+
/**
623+
* @since 5.1
624+
*/
625+
private ReactiveClusterTemplate getReactiveClusterTemplate() {
629626
return new ReactiveClusterTemplate(client.cluster(), converter);
630627
}
631628

0 commit comments

Comments
 (0)