11package org .apn .elasticsearch ;
22
3- import java .io .Closeable ;
4- import java .io .IOException ;
5- import java .text .MessageFormat ;
6- import java .util .Objects ;
7-
83import org .apache .commons .logging .Log ;
94import org .apache .commons .logging .LogFactory ;
105import org .apache .http .HttpHost ;
1712import org .elasticsearch .client .Response ;
1813import org .elasticsearch .client .RestClient ;
1914
15+ import java .io .Closeable ;
16+ import java .io .IOException ;
17+ import java .text .MessageFormat ;
18+ import java .util .Objects ;
19+
2020/**
2121 * The Class deals with Elasticsearch common usecases via
2222 * {@link RestClient}</code>.
23- *
24- * @author amit.nema
2523 *
24+ * @author amit.nema
2625 */
2726public class RestLowLevelClientApp implements Closeable {
2827
29- private static final Log LOGGER = LogFactory .getLog (RestLowLevelClientApp .class );
30- private final RestClient client ;
31- private final String index ;
32- private final String type ;
28+ private static final Log LOGGER = LogFactory .getLog (RestLowLevelClientApp .class );
29+ private final RestClient client ;
30+
31+ public RestLowLevelClientApp (final HttpHost ... hosts ) {
32+ client = RestClient .builder (hosts ).build ();
33+ }
3334
34- public RestLowLevelClientApp ( final HttpHost [] hosts , final String index , final String type ) {
35- this . index = index ;
36- this . type = type ;
37- client = RestClient . builder ( hosts ). build ();
38- }
35+ @ Override
36+ public void close () throws IOException {
37+ if ( Objects . nonNull ( client ))
38+ client . close ();
39+ }
3940
40- @ Override
41- public void close () throws IOException {
42- if (Objects .nonNull (client ))
43- client .close ();
44- }
41+ public void findById (final String index , final String id ) throws IOException {
42+ final Request req = new Request (HttpGet .METHOD_NAME , String .join ("/" , index , "_search" ));
43+ req .addParameter ("pretty" , "false" );
44+ req .setJsonEntity ("{\" query\" :{\" match\" :{\" " + "_id" + "\" :\" " + id + "\" }}}" );
45+ final Response resp = client .performRequest (req );
46+ LOGGER .info ("***** source *****\n " + resp .getRequestLine () + "\n ***** ***** *****" );
47+ final String responseBody = EntityUtils .toString (resp .getEntity ());
48+ LOGGER .info (responseBody );
49+ }
4550
46- public void findById ( final String id ) throws IOException {
47- final Request req = new Request (HttpGet .METHOD_NAME , String . join ( "/" , index , type , "_search" ) );
48- req .addParameter ("pretty" , "false" );
49- req .setJsonEntity ("{\" query\" :{ \" match \" :{ \" " + "_id" + "\" : \" " + id + "\" }} }" );
50- final Response resp = client .performRequest (req );
51- LOGGER .info ("***** source *****\n " + resp .getRequestLine () + "\n ***** ***** *****" );
52- final String responseBody = EntityUtils .toString (resp .getEntity ());
53- LOGGER .info (responseBody );
54- }
51+ public void findByIdSQL ( final String index , final String id ) throws IOException {
52+ final Request req = new Request (HttpGet .METHOD_NAME , "_sql" );
53+ req .addParameter ("pretty" , "false" );
54+ req .setJsonEntity ("{\" query\" :\" select * from " + index + " where ProductID = " + id + "\" }" );
55+ final Response resp = client .performRequest (req );
56+ LOGGER .info ("***** source *****\n " + resp .getRequestLine () + "\n ***** ***** *****" );
57+ final String responseBody = EntityUtils .toString (resp .getEntity ());
58+ LOGGER .info (responseBody );
59+ }
5560
56- public boolean indexExists () throws IOException {
57- final Request req = new Request (HttpHead .METHOD_NAME , MessageFormat .format ("/{0}" , index ));
58- final Response response = client .performRequest (req );
59- return response .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ;
60- }
61+ public boolean indexExists (final String index ) throws IOException {
62+ final Request req = new Request (HttpHead .METHOD_NAME , MessageFormat .format ("/{0}" , index ));
63+ final Response response = client .performRequest (req );
64+ return response .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ;
65+ }
6166
62- public boolean refreshIndex () throws IOException {
63- final Request req = new Request (HttpPost .METHOD_NAME , String .join ("/" , index , type , "_refresh" ));
64- req .setJsonEntity ("{}" );
65- final Response response = client .performRequest (req );
66- return response .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ;
67- }
67+ public boolean refreshIndex (final String index ) throws IOException {
68+ final Request req = new Request (HttpPost .METHOD_NAME , String .join ("/" , index , "_refresh" ));
69+ req .setJsonEntity ("{}" );
70+ final Response response = client .performRequest (req );
71+ return response .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ;
72+ }
6873}
0 commit comments