3030import org .elasticsearch .index .query .TermQueryBuilder ;
3131import org .elasticsearch .license .LicenseSettings ;
3232import org .elasticsearch .plugins .Plugin ;
33+ import org .elasticsearch .search .SearchHit ;
3334import org .elasticsearch .search .builder .SearchSourceBuilder ;
35+ import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
3436import org .elasticsearch .test .ESSingleNodeTestCase ;
3537import org .elasticsearch .test .InternalSettingsPlugin ;
3638import org .elasticsearch .xcontent .XContentType ;
3739import org .elasticsearch .xpack .core .XPackPlugin ;
3840
3941import java .io .IOException ;
4042import java .time .Instant ;
43+ import java .util .Arrays ;
4144import java .util .Collection ;
45+ import java .util .Comparator ;
4246import java .util .List ;
4347import java .util .UUID ;
4448import java .util .concurrent .ExecutionException ;
@@ -106,13 +110,6 @@ protected Settings nodeSettings() {
106110 .build ();
107111 }
108112
109- public void testStandard () throws Exception {
110- String dataStreamName = "k8s" ;
111- createTemplate (dataStreamName );
112- checkIndexSearchAndRetrieval (dataStreamName , false );
113- }
114-
115-
116113 public void testGetByGeneratedId () throws Exception {
117114 String dataStreamName = "k8s" ;
118115 createTemplate (dataStreamName );
@@ -125,7 +122,7 @@ public void testGetByGeneratedId() throws Exception {
125122 indexRequest .source (
126123 DOC .replace ("$time" , formatInstant (time ))
127124 .replace ("$uuid" , UUID .randomUUID ().toString ())
128- .replace ("$pod" , "pod-" + randomIntBetween ( 0 , 10 ) ),
125+ .replace ("$pod" , "pod-" + j ),
129126 XContentType .JSON
130127 );
131128 bulkRequest .add (indexRequest );
@@ -143,11 +140,13 @@ public void testGetByGeneratedId() throws Exception {
143140 assertThat (searchResponse .getHits ().getTotalHits ().value (), equalTo ((long ) numDocs ));
144141
145142 for (int i = 0 ; i < searchResponse .getHits ().getHits ().length ; i ++) {
146- String id = searchResponse .getHits ().getHits ()[i ].getId ();
143+ SearchHit hit = searchResponse .getHits ().getHits ()[i ];
144+ String id = hit .getId ();
147145 assertThat (id , notNullValue ());
148146
149147 // Check that the _id is gettable:
150- var getResponse = client ().get (new GetRequest (indexName ).id (id )).actionGet ();
148+ var getRequest = new GetRequest (indexName ).id (id );
149+ var getResponse = client ().get (getRequest ).actionGet ();
151150 assertThat (getResponse .isExists (), is (true ));
152151 assertThat (getResponse .getId (), equalTo (id ));
153152 }
@@ -163,11 +162,11 @@ public void testGetByProvidedID() throws Exception {
163162 for (int j = 0 ; j < numDocs ; j ++) {
164163 var indexRequest = new IndexRequest (indexName )
165164 .opType (DocWriteRequest .OpType .INDEX )
166- .id ("some- id-" + j );
165+ .id ("id-" + j );
167166 indexRequest .source (
168167 DOC .replace ("$time" , formatInstant (time ))
169168 .replace ("$uuid" , UUID .randomUUID ().toString ())
170- .replace ("$pod" , "pod-" + randomIntBetween ( 0 , 10 ) ),
169+ .replace ("$pod" , "pod-" + j ),
171170 XContentType .JSON
172171 );
173172 bulkRequest .add (indexRequest );
@@ -188,22 +187,24 @@ public void testGetByProvidedID() throws Exception {
188187 assertThat (searchResponse .getHits ().getTotalHits ().value (), equalTo ((long ) numDocs ));
189188
190189 for (int i = 0 ; i < searchResponse .getHits ().getHits ().length ; i ++) {
191- String id = searchResponse .getHits ().getHits ()[i ].getId ();
190+ SearchHit hit = searchResponse .getHits ().getHits ()[i ];
191+ String id = hit .getId ();
192+ String numPart = id .split ("-" )[1 ];
192193 assertThat (id , notNullValue ());
193-
194- // test get with the IDs from search response
195- var getResponse = client ().get (new GetRequest (indexName ).id (id )).actionGet ();
196- assertThat (getResponse .isExists (), is (true ));
197- assertThat (getResponse .getId (), equalTo (id ));
194+ // check got correct doc in search response
195+ var pod = (String ) hit .getSourceAsMap ().get ("message" );
196+ assertThat (pod , equalTo ("pod-" + numPart ));
198197 }
199198 });
200199
201200 // test get with the provided IDs
202201 for (int i = 0 ; i < numDocs ; i ++) {
203- String id = "some-id-" + i ;
204- var getResponse = client ().get (new GetRequest (indexName ).id (id )).actionGet ();
202+ String id = "id-" + i ;
203+ var getRequest = new GetRequest (indexName ).id (id ).fetchSourceContext (FetchSourceContext .FETCH_SOURCE );
204+ var getResponse = client ().get (getRequest ).actionGet ();
205205 assertThat (getResponse .isExists (), is (true ));
206206 assertThat (getResponse .getId (), equalTo (id ));
207+ assertThat (getResponse .getSourceAsMap ().get ("message" ), equalTo ("pod-" + i ));
207208 }
208209 }
209210
@@ -216,7 +217,7 @@ public void testMatchByProvidedID() throws Exception {
216217 for (int j = 0 ; j < numDocs ; j ++) {
217218 var indexRequest = new IndexRequest (indexName )
218219 .opType (DocWriteRequest .OpType .INDEX )
219- .id ("some- id-" + j );
220+ .id ("id-" + j );
220221 indexRequest .source (
221222 DOC .replace ("$time" , formatInstant (time ))
222223 .replace ("$uuid" , UUID .randomUUID ().toString ())
@@ -231,12 +232,12 @@ public void testMatchByProvidedID() throws Exception {
231232 client ().admin ().indices ().refresh (new RefreshRequest (indexName )).actionGet ();
232233
233234 for (int i = 0 ; i < numDocs ; i ++) {
234- String id = "some- id-" + i ;
235+ String id = "id-" + i ;
235236 var searchRequest = new SearchRequest (indexName );
236237 searchRequest .source (new SearchSourceBuilder ().query (new TermQueryBuilder ("_id" , id )).size (10 ));
237238 var searchResponse = client ().search (searchRequest ).actionGet ();
238239 assertThat (searchResponse .getHits ().getTotalHits ().value (), equalTo ((long ) 1 ));
239- assertThat (searchResponse .getHits ().getAt ( 0 ). field ("message" ), equalTo ("pod-" + i ));
240+ assertThat (searchResponse .getHits ().getHits ()[ 0 ]. getSourceAsMap (). get ("message" ), equalTo ("pod-" + i ));
240241 }
241242 }
242243
0 commit comments