4545import co .elastic .clients .elasticsearch .transform .PutTransformResponse ;
4646import co .elastic .clients .json .JsonData ;
4747import co .elastic .clients .json .JsonpMappingException ;
48- import co .elastic .clients .json .jackson .JacksonJsonpMapper ;
49- import co .elastic .clients .transport .ElasticsearchTransport ;
5048import co .elastic .clients .transport .TransportException ;
5149import co .elastic .clients .transport .endpoints .BinaryResponse ;
52- import co .elastic .clients .transport .rest_client .RestClientTransport ;
5350import co .elastic .clients .util .BinaryData ;
5451import co .elastic .clients .util .ContentType ;
5552import com .fasterxml .jackson .core .type .TypeReference ;
5653import com .fasterxml .jackson .databind .JsonNode ;
5754import com .fasterxml .jackson .databind .ObjectMapper ;
5855import com .fasterxml .jackson .databind .node .ObjectNode ;
59- import org .apache .http .HttpHost ;
60- import org .apache .http .auth .AuthScope ;
61- import org .apache .http .auth .UsernamePasswordCredentials ;
62- import org .apache .http .client .CredentialsProvider ;
63- import org .apache .http .impl .client .BasicCredentialsProvider ;
6456import org .apache .logging .log4j .LogManager ;
6557import org .apache .logging .log4j .Logger ;
66- import org .elasticsearch .client .RestClient ;
6758import org .junit .jupiter .api .*;
6859import org .testcontainers .elasticsearch .ElasticsearchContainer ;
6960import org .testcontainers .utility .DockerImageName ;
9182class EsClientIT {
9283
9384 private static final Logger logger = LogManager .getLogger ();
94- private static RestClient restClient = null ;
9585 private static ElasticsearchClient client = null ;
9686 private static ElasticsearchAsyncClient asyncClient = null ;
9787 private static final String PASSWORD = "changeme" ;
@@ -126,41 +116,33 @@ static void startOptionallyTestContainers() throws IOException {
126116
127117 @ AfterAll
128118 static void elasticsearchClient () throws IOException {
129- if (restClient != null ) {
130- restClient .close ();
119+ if (client != null ) {
120+ client .close ();
121+ }
122+ if (asyncClient != null ) {
123+ asyncClient .close ();
131124 }
132- }
133-
134- static private ElasticsearchTransport getElasticsearchTransport (final String elasticsearchServiceAddress , final byte [] certificate ) {
135- logger .debug ("Trying to connect to {} {}." , elasticsearchServiceAddress ,
136- certificate == null ? "with no ssl checks" : "using the provided SSL certificate" );
137- final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
138- credentialsProvider .setCredentials (AuthScope .ANY ,
139- new UsernamePasswordCredentials ("elastic" , PASSWORD ));
140-
141- // Create the low-level client
142- restClient = RestClient .builder (HttpHost .create (elasticsearchServiceAddress ))
143- .setHttpClientConfigCallback (hcb -> hcb
144- .setDefaultCredentialsProvider (credentialsProvider )
145- .setSSLContext (certificate != null ?
146- createContextFromCaCert (certificate ) : createTrustAllCertsContext ())
147- ).build ();
148-
149- // Create the transport with a Jackson mapper
150- return new RestClientTransport (restClient , new JacksonJsonpMapper ());
151125 }
152126
153127 static private ElasticsearchClient getClient (final String elasticsearchServiceAddress , final byte [] certificate ) throws Exception {
154128 // Create the API client
155- final ElasticsearchClient client = new ElasticsearchClient (getElasticsearchTransport (elasticsearchServiceAddress , certificate ));
129+ final ElasticsearchClient client = ElasticsearchClient .of (b -> b
130+ .host (elasticsearchServiceAddress )
131+ .sslContext (certificate != null ? createContextFromCaCert (certificate ) : createTrustAllCertsContext ())
132+ .usernameAndPassword ("elastic" , PASSWORD )
133+ );
156134 final InfoResponse info = client .info ();
157135 logger .info ("Client connected to a cluster running version {} at {}." , info .version ().number (), elasticsearchServiceAddress );
158136 return client ;
159137 }
160138
161139 static private ElasticsearchAsyncClient getAsyncClient (final String elasticsearchServiceAddress , final byte [] certificate ) throws Exception {
162140 // Create the API client
163- final ElasticsearchAsyncClient client = new ElasticsearchAsyncClient (getElasticsearchTransport (elasticsearchServiceAddress , certificate ));
141+ final ElasticsearchAsyncClient client = ElasticsearchAsyncClient .of (b -> b
142+ .host (elasticsearchServiceAddress )
143+ .sslContext (certificate != null ? createContextFromCaCert (certificate ) : createTrustAllCertsContext ())
144+ .usernameAndPassword ("elastic" , PASSWORD )
145+ );
164146 final InfoResponse info = client .info ().get ();
165147 logger .info ("Async Client connected to a cluster running version {} at {}." , info .version ().number (), elasticsearchServiceAddress );
166148 return client ;
@@ -665,7 +647,7 @@ void searchWithTimeout() throws IOException, ExecutionException, InterruptedExce
665647 void catApi () throws IOException {
666648 final ThreadPoolResponse threadPool = client .cat ().threadPool ();
667649 assertNotNull (threadPool );
668- for (final ThreadPoolRecord record : threadPool .valueBody ()) {
650+ for (final ThreadPoolRecord record : threadPool .threadPools ()) {
669651 logger .debug ("threadPool = {}" , record );
670652 assertNotNull (record .nodeName ());
671653 assertNotNull (record .name ());
@@ -675,15 +657,15 @@ void catApi() throws IOException {
675657 }
676658 final IndicesResponse indices = client .cat ().indices ();
677659 assertNotNull (indices );
678- for (final IndicesRecord record : indices .valueBody ()) {
660+ for (final IndicesRecord record : indices .indices ()) {
679661 logger .debug ("index = {}" , record );
680662 assertNotNull (record .index ());
681663 assertNotNull (record .docsCount ());
682664 assertNotNull (record .docsDeleted ());
683665 }
684666 final ShardsResponse shards = client .cat ().shards ();
685667 assertNotNull (shards );
686- for (final ShardsRecord record : shards .valueBody ()) {
668+ for (final ShardsRecord record : shards .shards ()) {
687669 logger .debug ("shard = {}" , record );
688670 assertNotNull (record .index ());
689671 assertNotNull (record .state ());
@@ -702,8 +684,8 @@ void ingestPipelines() throws IOException {
702684 .id ("my-pipeline" )
703685 .processors (p -> p
704686 .script (s -> s
705- .source ( "ctx.foo = 'bar'" )
706- .lang ( "painless" )
687+ .lang ( ScriptLanguage . Painless )
688+ .source ( src -> src . scriptString ( "ctx.foo = 'bar'" ) )
707689 )
708690 )
709691 );
@@ -742,7 +724,7 @@ void sourceRequest() throws IOException {
742724 client .index (ir -> ir .index (indexName ).id ("1" ).withJson (new StringReader ("{\" foo\" :\" bar\" }" )));
743725 client .indices ().refresh (rr -> rr .index (indexName ));
744726 final GetSourceResponse <ObjectNode > source = client .getSource (gsr -> gsr .index (indexName ).id ("1" ), ObjectNode .class );
745- assertEquals ("{\" foo\" :\" bar\" }" , source .valueBody ().toString ());
727+ assertEquals ("{\" foo\" :\" bar\" }" , source .source ().toString ());
746728 }
747729
748730 @ Test
@@ -771,7 +753,7 @@ void updateDocument() throws IOException {
771753 client .update (ur -> ur .index (indexName ).id ("1" ).script (
772754 s -> s
773755 .lang (ScriptLanguage .Painless )
774- .source ("ctx._source.show_count += 1" )
756+ .source (src -> src . scriptString ( "ctx._source.show_count += 1" ) )
775757 ), ObjectNode .class );
776758 final GetResponse <ObjectNode > response = client .get (gr -> gr .index (indexName ).id ("1" ), ObjectNode .class );
777759 assumeNotNull (response .source ());
@@ -867,8 +849,8 @@ void elser() throws IOException {
867849 .query ("How to avoid muscle soreness after running?" )
868850 )), ObjectNode .class );
869851 });
870- assertEquals ("[elser-v2-test] is not an inference service model or a deployed ml model " , exception .error ().reason ());
871- assertEquals (404 , exception .status ());
852+ assertEquals ("current license is non-compliant for [ inference] " , exception .error ().reason ());
853+ assertEquals (403 , exception .status ());
872854 }
873855
874856 @ Test
@@ -963,16 +945,25 @@ void esql() throws IOException, SQLException {
963945 {
964946 // Using the Raw ES|QL API
965947 try (final BinaryResponse response = client .esql ().query (q -> q .query (query )); InputStream is = response .content ()) {
966- // The response object is {"took":5,"columns":[{"name":"name","type":"text"}],"values":[["David"]]}
948+ // The response object is {
949+ // "took" : 4,
950+ // "is_partial" : false,
951+ // "columns" : [ {
952+ // "name" : "name",
953+ // "type" : "text"
954+ // } ],
955+ // "values" : [ [ "David" ] ]
956+ //}
967957 final ObjectMapper mapper = new ObjectMapper ();
968958 final JsonNode node = mapper .readTree (is );
969959 assertNotNull (node );
970- assertEquals (3 , node .size ());
960+ assertEquals (4 , node .size ());
971961 assertEquals (1 , node .get ("columns" ).size ());
972962 assertEquals ("name" , node .get ("columns" ).get (0 ).get ("name" ).asText ());
973963 assertEquals (1 , node .get ("values" ).size ());
974964 assertEquals ("David" , node .get ("values" ).get (0 ).get (0 ).asText ());
975965 assertTrue (node .get ("took" ).asInt () > 0 );
966+ assertFalse (node .get ("is_partial" ).asBoolean ());
976967 }
977968 }
978969
@@ -1036,9 +1027,9 @@ void withAliases() throws IOException {
10361027 assertTrue (client .indices ().existsAlias (ga -> ga .name (indexName + "_alias" )).value ());
10371028
10381029 // Check we have one alias on indexName
1039- assertEquals (1 , client .indices ().getAlias (ga -> ga .index (indexName )).result ().get (indexName ).aliases ().size ());
1030+ assertEquals (1 , client .indices ().getAlias (ga -> ga .index (indexName )).aliases ().get (indexName ).aliases ().size ());
10401031 // Check we have no alias on indexName-v2
1041- assertEquals (0 , client .indices ().getAlias (ga -> ga .index (indexName + "-v2" )).result ().get (indexName + "-v2" ).aliases ().size ());
1032+ assertEquals (0 , client .indices ().getAlias (ga -> ga .index (indexName + "-v2" )).aliases ().get (indexName + "-v2" ).aliases ().size ());
10421033
10431034 // Switch the alias indexName_alias from indexName to indexName-v2
10441035 client .indices ().updateAliases (ua -> ua
@@ -1047,9 +1038,9 @@ void withAliases() throws IOException {
10471038 );
10481039
10491040 // Check we have no alias on indexName
1050- assertEquals (0 , client .indices ().getAlias (ga -> ga .index (indexName )).result ().get (indexName ).aliases ().size ());
1041+ assertEquals (0 , client .indices ().getAlias (ga -> ga .index (indexName )).aliases ().get (indexName ).aliases ().size ());
10511042 // Check we have one alias on indexName-v2
1052- assertEquals (1 , client .indices ().getAlias (ga -> ga .index (indexName + "-v2" )).result ().get (indexName + "-v2" ).aliases ().size ());
1043+ assertEquals (1 , client .indices ().getAlias (ga -> ga .index (indexName + "-v2" )).aliases ().get (indexName + "-v2" ).aliases ().size ());
10531044
10541045 // Check the alias existence by its name
10551046 assertTrue (client .indices ().existsAlias (ga -> ga .name (indexName + "_alias" )).value ());
0 commit comments