33
33
import org .elasticsearch .client .RequestOptions ;
34
34
import org .elasticsearch .client .Response ;
35
35
import org .elasticsearch .client .RestClient ;
36
- import org .elasticsearch .client .RestHighLevelClient ;
37
36
import org .elasticsearch .client .indices .CreateIndexRequest ;
38
37
import org .elasticsearch .client .indices .GetIndexRequest ;
39
38
import org .elasticsearch .client .indices .PutMappingRequest ;
40
39
import org .elasticsearch .cluster .metadata .AliasMetaData ;
41
- import org .springframework .data .elasticsearch .ElasticsearchException ;
40
+ import org .springframework .data .elasticsearch .UncategorizedElasticsearchException ;
42
41
import org .springframework .data .elasticsearch .core .client .support .AliasData ;
43
- import org .springframework .data .elasticsearch .core .convert .ElasticsearchConverter ;
44
42
import org .springframework .data .elasticsearch .core .document .Document ;
45
43
import org .springframework .data .elasticsearch .core .mapping .IndexCoordinates ;
46
44
import org .springframework .data .elasticsearch .core .query .AliasQuery ;
60
58
*/
61
59
class DefaultIndexOperations extends AbstractDefaultIndexOperations implements IndexOperations {
62
60
63
- private RestHighLevelClient client ;
61
+ private ElasticsearchRestTemplate restTemplate ;
64
62
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 ;
69
66
}
70
67
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 ;
75
71
}
76
72
77
73
@ Override
78
74
protected boolean doCreate (String indexName , @ Nullable Document settings ) {
79
75
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 ());
86
77
}
87
78
88
79
@ Override
@@ -92,23 +83,15 @@ protected boolean doDelete(String indexName) {
92
83
93
84
if (doExists (indexName )) {
94
85
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 ());
100
87
}
101
88
return false ;
102
89
}
103
90
104
91
@ Override
105
92
protected boolean doExists (String indexName ) {
106
93
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 ));
112
95
}
113
96
114
97
@ Override
@@ -117,36 +100,28 @@ protected boolean doPutMapping(IndexCoordinates index, Document mapping) {
117
100
Assert .notNull (index , "No index defined for putMapping()" );
118
101
119
102
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 ());
125
105
}
126
106
127
107
@ Override
128
108
protected Map <String , Object > doGetMapping (IndexCoordinates index ) {
129
109
130
110
Assert .notNull (index , "No index defined for getMapping()" );
131
111
132
- RestClient restClient = client . getLowLevelClient ();
133
- try {
112
+ return restTemplate . execute ( client -> {
113
+ RestClient restClient = client . getLowLevelClient ();
134
114
Request request = new Request ("GET" , '/' + index .getIndexName () + "/_mapping" );
135
115
Response response = restClient .performRequest (request );
136
116
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
+ });
140
118
}
141
119
142
120
@ Override
143
121
protected boolean doAddAlias (AliasQuery query , IndexCoordinates index ) {
144
122
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 ());
150
125
}
151
126
152
127
@ Override
@@ -156,29 +131,23 @@ protected boolean doRemoveAlias(AliasQuery query, IndexCoordinates index) {
156
131
Assert .notNull (query .getAliasName (), "No alias defined" );
157
132
158
133
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 ());
165
136
}
166
137
167
138
@ Override
168
139
protected List <AliasMetaData > doQueryForAlias (String indexName ) {
169
140
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 ;
173
145
174
- try {
175
146
response = restClient .performRequest (new Request ("GET" , '/' + indexName + "/_alias/*" ));
176
147
aliasResponse = EntityUtils .toString (response .getEntity ());
177
- } catch (Exception e ) {
178
- throw new ElasticsearchException ("Error while getting mapping for indexName : " + indexName , e );
179
- }
180
148
181
- return convertAliasResponse (aliasResponse );
149
+ return convertAliasResponse (aliasResponse );
150
+ });
182
151
}
183
152
184
153
@ Override
@@ -190,26 +159,20 @@ protected Map<String, Object> doGetSettings(String indexName, boolean includeDef
190
159
.indices (indexName ) //
191
160
.includeDefaults (includeDefaults );
192
161
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 ) );
196
165
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 );
201
167
}
202
168
203
169
@ Override
204
170
protected void doRefresh (IndexCoordinates index ) {
205
171
206
172
Assert .notNull (index , "No index defined for refresh()" );
207
173
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 ));
213
176
}
214
177
215
178
// region Helper methods
@@ -225,7 +188,7 @@ private Map<String, Object> convertMappingResponse(String mappingResponse) {
225
188
226
189
return result ;
227
190
} 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 );
229
192
}
230
193
}
231
194
@@ -261,7 +224,7 @@ private List<AliasMetaData> convertAliasResponse(String aliasResponse) {
261
224
}
262
225
return aliasMetaDataList ;
263
226
} 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 );
265
228
}
266
229
}
267
230
// endregion
0 commit comments