1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.libraryelastic.util;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
39
40 import fr.paris.lutece.plugins.libraryelastic.business.bulk.BulkRequest;
41 import fr.paris.lutece.plugins.libraryelastic.business.search.SearchRequest;
42 import fr.paris.lutece.plugins.libraryelastic.business.suggest.AbstractSuggestRequest;
43 import fr.paris.lutece.util.httpaccess.HttpAccessException;
44 import fr.paris.lutece.util.httpaccess.InvalidResponseStatus;
45
46 import org.apache.commons.lang3.StringUtils;
47 import org.apache.hc.core5.http.HttpStatus;
48
49
50
51
52 public class Elastic
53 {
54 private static ObjectMapper _mapper = new ObjectMapper( ).registerModule( new JavaTimeModule( ) );
55 private ElasticConnexion _connexion;
56 private String _strServerUrl;
57
58
59
60
61
62
63
64 public Elastic( String strServerUrl )
65 {
66 _strServerUrl = strServerUrl;
67 _connexion = new ElasticConnexion( );
68 }
69
70
71
72
73
74
75
76
77
78
79
80 public Elastic( String strServerUrl, String strServerLogin, String strServerPwd )
81 {
82 _strServerUrl = strServerUrl;
83 _connexion = new ElasticConnexion( strServerLogin, strServerPwd );
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 public String create( String strIndex, Object object ) throws ElasticClientException
98 {
99 return create( strIndex, StringUtils.EMPTY, object );
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public String create( String strIndex, String strId, Object object ) throws ElasticClientException
116 {
117 String strResponse = StringUtils.EMPTY;
118 try
119 {
120 String strJSON;
121
122 if ( object instanceof String )
123 {
124 strJSON = (String) object;
125 }
126 else
127 {
128 strJSON = _mapper.writeValueAsString( object );
129 }
130
131 String strURI = getURI( strIndex ) + "_doc" + Constants.URL_PATH_SEPARATOR + strId;
132 strResponse = _connexion.POST( strURI, strJSON );
133 }
134 catch( JsonProcessingException | HttpAccessException ex )
135 {
136 throw new ElasticClientException( "ElasticLibrary : Error creating object : " + ex.getMessage( ), ex );
137 }
138 return strResponse;
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public String createByBulk( String strIndex, BulkRequest bulkRequest ) throws ElasticClientException
152 {
153 String strResponse = StringUtils.EMPTY;
154 try
155 {
156 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_BULK;
157 String strBulkBody = bulkRequest.getBulkBody( _mapper );
158 strResponse = _connexion.POST( strURI, strBulkBody );
159 }
160 catch( JsonProcessingException | HttpAccessException ex )
161 {
162 throw new ElasticClientException( "ElasticLibrary : Error processing bulking request : " + ex.getMessage( ), ex );
163 }
164 return strResponse;
165 }
166
167
168
169
170
171
172
173
174
175
176 public String deleteIndex( String strIndex ) throws ElasticClientException
177 {
178 String strResponse = StringUtils.EMPTY;
179 try
180 {
181 String strURI = getURI( strIndex );
182 strResponse = _connexion.DELETE( strURI );
183 }
184 catch( HttpAccessException ex )
185 {
186 throw new ElasticClientException( "ElasticLibrary : Error deleting index : " + ex.getMessage( ), ex );
187 }
188 return strResponse;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202 public String deleteDocument( String strIndex, String strId ) throws ElasticClientException
203 {
204 String strResponse = StringUtils.EMPTY;
205 try
206 {
207 String strURI = getURI( strIndex ) + "_doc" + Constants.URL_PATH_SEPARATOR + strId;
208 strResponse = _connexion.DELETE( strURI );
209 }
210 catch( HttpAccessException ex )
211 {
212 throw new ElasticClientException( "ElasticLibrary : Error deleting document : " + ex.getMessage( ), ex );
213 }
214 return strResponse;
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228 public String deleteByQuery( String strIndex, String strQuery ) throws ElasticClientException
229 {
230 String strResponse = StringUtils.EMPTY;
231 try
232 {
233 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_DELETE_BY_QUERY;
234 strResponse = _connexion.POST( strURI, strQuery );
235 }
236 catch( HttpAccessException ex )
237 {
238 throw new ElasticClientException( "ElasticLibrary : Error deleting by query : " + ex.getMessage( ), ex );
239 }
240 return strResponse;
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 public String partialUpdate( String strIndex, String strId, Object object ) throws ElasticClientException
258 {
259 String strResponse = StringUtils.EMPTY;
260 try
261 {
262 String strJSON;
263 if ( object instanceof String )
264 {
265 strJSON = (String) object;
266 }
267 else
268 {
269 strJSON = _mapper.writeValueAsString( object );
270 }
271
272 String json = buildJsonToPartialUpdate( strJSON );
273
274 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_UPDATE + Constants.URL_PATH_SEPARATOR + strId;
275 strResponse = _connexion.POST( strURI, json );
276 }
277 catch( JsonProcessingException | HttpAccessException ex )
278 {
279 throw new ElasticClientException( "ElasticLibrary : Error updating by query : " + ex.getMessage( ), ex );
280 }
281 return strResponse;
282 }
283
284
285
286
287
288
289
290
291
292
293 public boolean isExists( String strIndex ) throws ElasticClientException
294 {
295 try
296 {
297 String strURI = getURI( strIndex );
298 _connexion.GET( strURI );
299 }
300 catch( InvalidResponseStatus ex )
301 {
302 if ( ex.getResponseStatus( ) == HttpStatus.SC_NOT_FOUND )
303 {
304 return false;
305 }
306 throw new ElasticClientException( "ElasticLibrary : Error getting index : " + ex.getMessage( ), ex );
307 }
308 catch( HttpAccessException ex )
309 {
310 throw new ElasticClientException( "ElasticLibrary : Error getting index : " + ex.getMessage( ), ex );
311 }
312 return true;
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326 public String search( String strIndex, SearchRequest search ) throws ElasticClientException
327 {
328 String strResponse = StringUtils.EMPTY;
329 try
330 {
331 String strJSON = _mapper.writeValueAsString( search.mapToNode( ) );
332 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_SEARCH;
333 strResponse = _connexion.POST( strURI, strJSON );
334 }
335 catch( JsonProcessingException | HttpAccessException ex )
336 {
337 throw new ElasticClientException( "ElasticLibrary : Error searching object : " + ex.getMessage( ), ex );
338 }
339 return strResponse;
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353 public String search( String strIndex, String searchRequest ) throws ElasticClientException
354 {
355 String strResponse = StringUtils.EMPTY;
356 try
357 {
358 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_SEARCH;
359 strResponse = _connexion.POST( strURI, searchRequest );
360 }
361 catch( HttpAccessException ex )
362 {
363 throw new ElasticClientException( "ElasticLibrary : Error searching object : " + ex.getMessage( ), ex );
364 }
365 return strResponse;
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379
380 public String suggest( String strIndex, AbstractSuggestRequest suggest ) throws ElasticClientException
381 {
382 String strResponse = StringUtils.EMPTY;
383 try
384 {
385 SearchRequestryelastic/business/search/SearchRequest.html#SearchRequest">SearchRequest search = new SearchRequest( );
386 search.setSize( 0 );
387 search.setSearchQuery( suggest );
388 String strJSON = _mapper.writeValueAsString( search.mapToNode( ) );
389 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_SEARCH;
390 strResponse = _connexion.POST( strURI, strJSON );
391 }
392 catch( JsonProcessingException | HttpAccessException ex )
393 {
394 throw new ElasticClientException( "ElasticLibrary : Error suggesting object : " + ex.getMessage( ), ex );
395 }
396 return strResponse;
397 }
398
399
400
401
402
403
404
405
406
407
408
409
410
411 public String suggest( String strIndex, String strJSON ) throws ElasticClientException
412 {
413 String strResponse = StringUtils.EMPTY;
414 try
415 {
416 String strURI = getURI( strIndex ) + Constants.PATH_QUERY_SEARCH;
417 strResponse = _connexion.POST( strURI, strJSON );
418 }
419 catch( HttpAccessException ex )
420 {
421 throw new ElasticClientException( "ElasticLibrary : Error suggesting object : " + ex.getMessage( ), ex );
422 }
423 return strResponse;
424 }
425
426
427
428
429
430
431
432
433 public String createMappings( String strIndex, String strJsonMappings ) throws ElasticClientException
434 {
435 String strResponse = StringUtils.EMPTY;
436 try
437 {
438 String strURI = getURI( strIndex );
439 strResponse = _connexion.PUT( strURI, strJsonMappings );
440 }
441 catch( HttpAccessException ex )
442 {
443 throw new ElasticClientException( "ElasticLibrary : Error creating mappings : " + ex.getMessage( ), ex );
444 }
445 return strResponse;
446
447 }
448
449
450
451
452
453
454
455
456 private String getURI( String strIndex )
457 {
458 String strURI = _strServerUrl;
459 strURI = ( strURI.endsWith( Constants.URL_PATH_SEPARATOR ) ) ? strURI : strURI + Constants.URL_PATH_SEPARATOR;
460 if ( StringUtils.isNotEmpty( strIndex ) )
461 {
462 strURI = ( ( strIndex.endsWith( Constants.URL_PATH_SEPARATOR ) ) ? strURI + strIndex : strURI + strIndex + Constants.URL_PATH_SEPARATOR );
463 }
464
465 return strURI;
466 }
467
468
469
470
471
472
473
474
475 private String buildJsonToPartialUpdate( String strJson )
476 {
477
478 StringBuilder sbuilder = new StringBuilder( );
479 sbuilder.append( "{ \"doc\" : " );
480 sbuilder.append( strJson );
481 sbuilder.append( "}" );
482
483 return sbuilder.toString( );
484 }
485 }