19
19
20
20
package org .elasticsearch .index .reindex .remote ;
21
21
22
- import org .apache .http .HttpEntity ;
23
22
import org .apache .http .HttpHost ;
24
- import org .apache .http .entity .ContentType ;
25
- import org .apache .http .entity .StringEntity ;
26
23
import org .apache .http .util .EntityUtils ;
24
+ import org .elasticsearch .client .Request ;
27
25
import org .elasticsearch .client .Response ;
28
- import org .elasticsearch .client .ResponseException ;
29
26
import org .elasticsearch .client .RestClient ;
30
27
import org .elasticsearch .common .Booleans ;
31
28
import org .elasticsearch .test .rest .ESRestTestCase ;
32
29
33
30
import java .io .IOException ;
34
- import java .util .Map ;
35
- import java .util .TreeMap ;
36
31
37
- import static java .util .Collections .singletonMap ;
38
32
import static org .hamcrest .Matchers .containsString ;
39
33
40
34
public 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
+
41
40
private void oldEsTestCase (String portPropertyName , String requestsPerSecond ) throws IOException {
42
41
boolean enabled = Booleans .parseBoolean (System .getProperty ("tests.fromOld" ));
43
42
assumeTrue ("test is disabled, probably because this is windows" , enabled );
44
43
45
44
int oldEsPort = Integer .parseInt (System .getProperty (portPropertyName ));
46
45
try (RestClient oldEs = RestClient .builder (new HttpHost ("127.0.0.1" , oldEsPort )).build ()) {
47
46
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
+ }
57
57
58
- entity = new StringEntity (
58
+ Request reindex = new Request ("POST" , "/_reindex" );
59
+ reindex .setJsonEntity (
59
60
"{\n "
60
61
+ " \" source\" :{\n "
61
62
+ " \" index\" : \" test\" ,\n "
@@ -67,36 +68,23 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th
67
68
+ " \" dest\" : {\n "
68
69
+ " \" index\" : \" test\" \n "
69
70
+ " }\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" );
75
74
if (requestsPerSecond != null ) {
76
- params . put ("requests_per_second" , requestsPerSecond );
75
+ reindex . addParameter ("requests_per_second" , requestsPerSecond );
77
76
}
78
- client ().performRequest ("POST" , "/_reindex" , params , entity );
77
+ client ().performRequest (reindex );
79
78
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 );
81
82
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 + "\" " ));
99
85
}
86
+ } finally {
87
+ oldEs .performRequest (new Request ("DELETE" , "/test" ));
100
88
}
101
89
}
102
90
}
0 commit comments