Skip to content

Commit 0b0c802

Browse files
authored
DATAES-751 - Introduce ClientCallback for the rest client.
Original PR: #401
1 parent ab0c6a8 commit 0b0c802

15 files changed

+229
-201
lines changed

src/main/java/org/springframework/data/elasticsearch/ElasticsearchException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
* @author Rizwan Idrees
2626
* @author Mohsin Husen
2727
* @author Peter-Josef Meisch
28+
* @deprecated since 4.0, use {@link org.springframework.dao.UncategorizedDataAccessException}
2829
*/
30+
@Deprecated
2931
public class ElasticsearchException extends RuntimeException {
3032

3133
@Nullable private Map<String, String> failedDocuments;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch;
17+
18+
import org.springframework.dao.UncategorizedDataAccessException;
19+
20+
/**
21+
* @author Peter-Josef Meisch
22+
* @since 4.0
23+
*/
24+
public class UncategorizedElasticsearchException extends UncategorizedDataAccessException {
25+
public UncategorizedElasticsearchException(String msg, Throwable cause) {
26+
super(msg, cause);
27+
}
28+
}

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

Lines changed: 34 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
import org.elasticsearch.client.RequestOptions;
3434
import org.elasticsearch.client.Response;
3535
import org.elasticsearch.client.RestClient;
36-
import org.elasticsearch.client.RestHighLevelClient;
3736
import org.elasticsearch.client.indices.CreateIndexRequest;
3837
import org.elasticsearch.client.indices.GetIndexRequest;
3938
import org.elasticsearch.client.indices.PutMappingRequest;
4039
import org.elasticsearch.cluster.metadata.AliasMetaData;
41-
import org.springframework.data.elasticsearch.ElasticsearchException;
40+
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
4241
import org.springframework.data.elasticsearch.core.client.support.AliasData;
43-
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
4442
import org.springframework.data.elasticsearch.core.document.Document;
4543
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
4644
import org.springframework.data.elasticsearch.core.query.AliasQuery;
@@ -60,29 +58,22 @@
6058
*/
6159
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
6260

63-
private RestHighLevelClient client;
61+
private ElasticsearchRestTemplate restTemplate;
6462

65-
public DefaultIndexOperations(RestHighLevelClient client, ElasticsearchConverter elasticsearchConverter,
66-
Class<?> boundClass) {
67-
super(elasticsearchConverter, boundClass);
68-
this.client = client;
63+
public DefaultIndexOperations(ElasticsearchRestTemplate restTemplate, Class<?> boundClass) {
64+
super(restTemplate.getElasticsearchConverter(), boundClass);
65+
this.restTemplate = restTemplate;
6966
}
7067

71-
public DefaultIndexOperations(RestHighLevelClient client, ElasticsearchConverter elasticsearchConverter,
72-
IndexCoordinates boundIndex) {
73-
super(elasticsearchConverter, boundIndex);
74-
this.client = client;
68+
public DefaultIndexOperations(ElasticsearchRestTemplate restTemplate, IndexCoordinates boundIndex) {
69+
super(restTemplate.getElasticsearchConverter(), boundIndex);
70+
this.restTemplate = restTemplate;
7571
}
7672

7773
@Override
7874
protected boolean doCreate(String indexName, @Nullable Document settings) {
7975
CreateIndexRequest request = requestFactory.createIndexRequest(indexName, settings);
80-
try {
81-
return client.indices().create(request, RequestOptions.DEFAULT).isAcknowledged();
82-
} catch (IOException e) {
83-
throw new ElasticsearchException(
84-
"Error for creating index: " + indexName + ", client: " + client.getLowLevelClient().getNodes(), e);
85-
}
76+
return restTemplate.execute(client -> client.indices().create(request, RequestOptions.DEFAULT).isAcknowledged());
8677
}
8778

8879
@Override
@@ -92,23 +83,15 @@ protected boolean doDelete(String indexName) {
9283

9384
if (doExists(indexName)) {
9485
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
95-
try {
96-
return client.indices().delete(request, RequestOptions.DEFAULT).isAcknowledged();
97-
} catch (IOException e) {
98-
throw new ElasticsearchException("Error while deleting index request: " + request.toString(), e);
99-
}
86+
return restTemplate.execute(client -> client.indices().delete(request, RequestOptions.DEFAULT).isAcknowledged());
10087
}
10188
return false;
10289
}
10390

10491
@Override
10592
protected boolean doExists(String indexName) {
10693
GetIndexRequest request = new GetIndexRequest(indexName);
107-
try {
108-
return client.indices().exists(request, RequestOptions.DEFAULT);
109-
} catch (IOException e) {
110-
throw new ElasticsearchException("Error while for indexExists request: " + request.toString(), e);
111-
}
94+
return restTemplate.execute(client -> client.indices().exists(request, RequestOptions.DEFAULT));
11295
}
11396

11497
@Override
@@ -117,36 +100,28 @@ protected boolean doPutMapping(IndexCoordinates index, Document mapping) {
117100
Assert.notNull(index, "No index defined for putMapping()");
118101

119102
PutMappingRequest request = requestFactory.putMappingRequest(index, mapping);
120-
try {
121-
return client.indices().putMapping(request, RequestOptions.DEFAULT).isAcknowledged();
122-
} catch (IOException e) {
123-
throw new ElasticsearchException("Failed to put mapping for " + index.getIndexName(), e);
124-
}
103+
return restTemplate
104+
.execute(client -> client.indices().putMapping(request, RequestOptions.DEFAULT).isAcknowledged());
125105
}
126106

127107
@Override
128108
protected Map<String, Object> doGetMapping(IndexCoordinates index) {
129109

130110
Assert.notNull(index, "No index defined for getMapping()");
131111

132-
RestClient restClient = client.getLowLevelClient();
133-
try {
112+
return restTemplate.execute(client -> {
113+
RestClient restClient = client.getLowLevelClient();
134114
Request request = new Request("GET", '/' + index.getIndexName() + "/_mapping");
135115
Response response = restClient.performRequest(request);
136116
return convertMappingResponse(EntityUtils.toString(response.getEntity()));
137-
} catch (Exception e) {
138-
throw new ElasticsearchException("Error while getting mapping for indexName : " + index.getIndexName(), e);
139-
}
117+
});
140118
}
141119

142120
@Override
143121
protected boolean doAddAlias(AliasQuery query, IndexCoordinates index) {
144122
IndicesAliasesRequest request = requestFactory.indicesAddAliasesRequest(query, index);
145-
try {
146-
return client.indices().updateAliases(request, RequestOptions.DEFAULT).isAcknowledged();
147-
} catch (IOException e) {
148-
throw new ElasticsearchException("failed to update aliases with request: " + request, e);
149-
}
123+
return restTemplate
124+
.execute(client -> client.indices().updateAliases(request, RequestOptions.DEFAULT).isAcknowledged());
150125
}
151126

152127
@Override
@@ -156,29 +131,23 @@ protected boolean doRemoveAlias(AliasQuery query, IndexCoordinates index) {
156131
Assert.notNull(query.getAliasName(), "No alias defined");
157132

158133
IndicesAliasesRequest indicesAliasesRequest = requestFactory.indicesRemoveAliasesRequest(query, index);
159-
try {
160-
return client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT).isAcknowledged();
161-
} catch (IOException e) {
162-
throw new ElasticsearchException(
163-
"failed to update aliases with indicesRemoveAliasesRequest: " + indicesAliasesRequest, e);
164-
}
134+
return restTemplate.execute(
135+
client -> client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT).isAcknowledged());
165136
}
166137

167138
@Override
168139
protected List<AliasMetaData> doQueryForAlias(String indexName) {
169140
List<AliasMetaData> aliases = null;
170-
RestClient restClient = client.getLowLevelClient();
171-
Response response;
172-
String aliasResponse;
141+
return restTemplate.execute(client -> {
142+
RestClient restClient = client.getLowLevelClient();
143+
Response response;
144+
String aliasResponse;
173145

174-
try {
175146
response = restClient.performRequest(new Request("GET", '/' + indexName + "/_alias/*"));
176147
aliasResponse = EntityUtils.toString(response.getEntity());
177-
} catch (Exception e) {
178-
throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName, e);
179-
}
180148

181-
return convertAliasResponse(aliasResponse);
149+
return convertAliasResponse(aliasResponse);
150+
});
182151
}
183152

184153
@Override
@@ -190,26 +159,20 @@ protected Map<String, Object> doGetSettings(String indexName, boolean includeDef
190159
.indices(indexName) //
191160
.includeDefaults(includeDefaults);
192161

193-
try {
194-
GetSettingsResponse response = client.indices() //
195-
.getSettings(request, RequestOptions.DEFAULT);
162+
//
163+
GetSettingsResponse response = restTemplate.execute(client -> client.indices() //
164+
.getSettings(request, RequestOptions.DEFAULT));
196165

197-
return convertSettingsResponseToMap(response, indexName);
198-
} catch (IOException e) {
199-
throw new ElasticsearchException("failed to get settings for index: " + indexName, e);
200-
}
166+
return convertSettingsResponseToMap(response, indexName);
201167
}
202168

203169
@Override
204170
protected void doRefresh(IndexCoordinates index) {
205171

206172
Assert.notNull(index, "No index defined for refresh()");
207173

208-
try {
209-
client.indices().refresh(refreshRequest(index.getIndexNames()), RequestOptions.DEFAULT);
210-
} catch (IOException e) {
211-
throw new ElasticsearchException("failed to refresh index: " + index, e);
212-
}
174+
restTemplate
175+
.execute(client -> client.indices().refresh(refreshRequest(index.getIndexNames()), RequestOptions.DEFAULT));
213176
}
214177

215178
// region Helper methods
@@ -225,7 +188,7 @@ private Map<String, Object> convertMappingResponse(String mappingResponse) {
225188

226189
return result;
227190
} catch (IOException e) {
228-
throw new ElasticsearchException("Could not map alias response : " + mappingResponse, e);
191+
throw new UncategorizedElasticsearchException("Could not map alias response : " + mappingResponse, e);
229192
}
230193
}
231194

@@ -261,7 +224,7 @@ private List<AliasMetaData> convertAliasResponse(String aliasResponse) {
261224
}
262225
return aliasMetaDataList;
263226
} catch (IOException e) {
264-
throw new ElasticsearchException("Could not map alias response : " + aliasResponse, e);
227+
throw new UncategorizedElasticsearchException("Could not map alias response : " + aliasResponse, e);
265228
}
266229
}
267230
// endregion

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@
1616

1717
package org.springframework.data.elasticsearch.core;
1818

19-
import java.net.ConnectException;
19+
import java.io.IOException;
2020
import java.util.List;
2121

2222
import org.elasticsearch.ElasticsearchException;
2323
import org.elasticsearch.ElasticsearchStatusException;
24+
import org.elasticsearch.common.ValidationException;
2425
import org.springframework.dao.DataAccessException;
2526
import org.springframework.dao.DataAccessResourceFailureException;
27+
import org.springframework.dao.DataIntegrityViolationException;
2628
import org.springframework.dao.support.PersistenceExceptionTranslator;
2729
import org.springframework.data.elasticsearch.NoSuchIndexException;
30+
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
2831
import org.springframework.util.CollectionUtils;
2932
import org.springframework.util.ObjectUtils;
3033
import org.springframework.util.StringUtils;
3134

3235
/**
3336
* @author Christoph Strobl
37+
* @author Peter-Josef Meisch
3438
* @since 3.2
3539
*/
3640
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
@@ -46,9 +50,15 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
4650
return new NoSuchIndexException(ObjectUtils.nullSafeToString(elasticsearchException.getMetadata("es.index")),
4751
ex);
4852
}
53+
return new UncategorizedElasticsearchException(ex.getMessage(), ex);
4954
}
5055

51-
if (ex.getCause() instanceof ConnectException) {
56+
if (ex instanceof ValidationException) {
57+
return new DataIntegrityViolationException(ex.getMessage(), ex);
58+
}
59+
60+
Throwable cause = ex.getCause();
61+
if (cause instanceof IOException) {
5262
return new DataAccessResourceFailureException(ex.getMessage(), ex);
5363
}
5464

@@ -60,8 +70,9 @@ private boolean indexAvailable(ElasticsearchException ex) {
6070
List<String> metadata = ex.getMetadata("es.index_uuid");
6171
if (metadata == null) {
6272
if (ex instanceof ElasticsearchStatusException) {
63-
return StringUtils.hasText(ObjectUtils.nullSafeToString(((ElasticsearchStatusException) ex).getIndex()));
73+
return StringUtils.hasText(ObjectUtils.nullSafeToString(ex.getIndex()));
6474
}
75+
return false;
6576
}
6677
return !CollectionUtils.contains(metadata.iterator(), "_na_");
6778
}

0 commit comments

Comments
 (0)