1919
2020package org .elasticsearch .index .reindex .remote ;
2121
22- import org .apache .http .HttpEntity ;
2322import org .apache .http .HttpHost ;
24- import org .apache .http .entity .ContentType ;
25- import org .apache .http .entity .StringEntity ;
2623import org .apache .http .util .EntityUtils ;
24+ import org .elasticsearch .client .Request ;
2725import org .elasticsearch .client .Response ;
28- import org .elasticsearch .client .ResponseException ;
2926import org .elasticsearch .client .RestClient ;
3027import org .elasticsearch .common .Booleans ;
3128import org .elasticsearch .test .rest .ESRestTestCase ;
3229
3330import java .io .IOException ;
34- import java .util .Map ;
35- import java .util .TreeMap ;
3631
37- import static java .util .Collections .singletonMap ;
3832import static org .hamcrest .Matchers .containsString ;
3933
4034public class ReindexFromOldRemoteIT extends ESRestTestCase {
35+ /**
36+ * Number of documents to test when reindexing from an old version.
37+ */
38+ private static final int DOCS = 5 ;
39+
4140 private void oldEsTestCase (String portPropertyName , String requestsPerSecond ) throws IOException {
4241 boolean enabled = Booleans .parseBoolean (System .getProperty ("tests.fromOld" ));
4342 assumeTrue ("test is disabled, probably because this is windows" , enabled );
4443
4544 int oldEsPort = Integer .parseInt (System .getProperty (portPropertyName ));
4645 try (RestClient oldEs = RestClient .builder (new HttpHost ("127.0.0.1" , oldEsPort )).build ()) {
4746 try {
48- HttpEntity entity = new StringEntity ("{\" settings\" :{\" number_of_shards\" : 1}}" , ContentType .APPLICATION_JSON );
49- oldEs .performRequest ("PUT" , "/test" , singletonMap ("refresh" , "true" ), entity );
50-
51- entity = new StringEntity ("{\" test\" :\" test\" }" , ContentType .APPLICATION_JSON );
52- oldEs .performRequest ("PUT" , "/test/doc/testdoc1" , singletonMap ("refresh" , "true" ), entity );
53- oldEs .performRequest ("PUT" , "/test/doc/testdoc2" , singletonMap ("refresh" , "true" ), entity );
54- oldEs .performRequest ("PUT" , "/test/doc/testdoc3" , singletonMap ("refresh" , "true" ), entity );
55- oldEs .performRequest ("PUT" , "/test/doc/testdoc4" , singletonMap ("refresh" , "true" ), entity );
56- oldEs .performRequest ("PUT" , "/test/doc/testdoc5" , singletonMap ("refresh" , "true" ), entity );
47+ Request createIndex = new Request ("PUT" , "/test" );
48+ createIndex .setJsonEntity ("{\" settings\" :{\" number_of_shards\" : 1}}" );
49+ oldEs .performRequest (createIndex );
50+
51+ for (int i = 0 ; i < DOCS ; i ++) {
52+ Request doc = new Request ("PUT" , "/test/doc/testdoc" + i );
53+ doc .addParameter ("refresh" , "true" );
54+ doc .setJsonEntity ("{\" test\" :\" test\" }" );
55+ oldEs .performRequest (doc );
56+ }
5757
58- entity = new StringEntity (
58+ Request reindex = new Request ("POST" , "/_reindex" );
59+ reindex .setJsonEntity (
5960 "{\n "
6061 + " \" source\" :{\n "
6162 + " \" index\" : \" test\" ,\n "
@@ -67,36 +68,23 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th
6768 + " \" dest\" : {\n "
6869 + " \" index\" : \" test\" \n "
6970 + " }\n "
70- + "}" ,
71- ContentType .APPLICATION_JSON );
72- Map <String , String > params = new TreeMap <>();
73- params .put ("refresh" , "true" );
74- params .put ("pretty" , "true" );
71+ + "}" );
72+ reindex .addParameter ("refresh" , "true" );
73+ reindex .addParameter ("pretty" , "true" );
7574 if (requestsPerSecond != null ) {
76- params . put ("requests_per_second" , requestsPerSecond );
75+ reindex . addParameter ("requests_per_second" , requestsPerSecond );
7776 }
78- client ().performRequest ("POST" , "/_reindex" , params , entity );
77+ client ().performRequest (reindex );
7978
80- Response response = client ().performRequest ("POST" , "test/_search" , singletonMap ("pretty" , "true" ));
79+ Request search = new Request ("POST" , "/test/_search" );
80+ search .addParameter ("pretty" , "true" );
81+ Response response = client ().performRequest (search );
8182 String result = EntityUtils .toString (response .getEntity ());
82- assertThat (result , containsString ("\" _id\" : \" testdoc1\" " ));
83- } finally {
84- try {
85- oldEs .performRequest ("DELETE" , "/test" );
86- } catch (ResponseException e ) {
87- /* Try not to throw ResponseException for as it'll eat the
88- * real exception. This is because the rest client throws
89- * exceptions in a "funny" way that isn't compatible with
90- * `suppressed`. In the case of 404s we'll just log something
91- * and move on because that just means that a previous
92- * failure caused the index not to be created. */
93- if (e .getResponse ().getStatusLine ().getStatusCode () == 404 ) {
94- logger .warn ("old index not deleted because it doesn't exist" );
95- } else {
96- logger .error ("failed to remove old index" , e );
97- fail ("failed to remove old index, see log" );
98- }
83+ for (int i = 0 ; i < DOCS ; i ++) {
84+ assertThat (result , containsString ("\" _id\" : \" testdoc" + i + "\" " ));
9985 }
86+ } finally {
87+ oldEs .performRequest (new Request ("DELETE" , "/test" ));
10088 }
10189 }
10290 }
0 commit comments