19
19
20
20
package org .elasticsearch .index .reindex .remote ;
21
21
22
- import org .apache .http .HttpEntity ;
23
22
import org .apache .http .entity .ByteArrayEntity ;
24
23
import org .apache .http .entity .ContentType ;
25
24
import org .apache .http .entity .StringEntity ;
26
25
import org .apache .lucene .util .BytesRef ;
27
26
import org .elasticsearch .ElasticsearchException ;
28
27
import org .elasticsearch .Version ;
29
28
import org .elasticsearch .action .search .SearchRequest ;
29
+ import org .elasticsearch .client .Request ;
30
30
import org .elasticsearch .common .Strings ;
31
31
import org .elasticsearch .common .bytes .BytesReference ;
32
32
import org .elasticsearch .common .unit .TimeValue ;
40
40
import org .elasticsearch .search .sort .SortBuilder ;
41
41
42
42
import java .io .IOException ;
43
- import java .util .HashMap ;
44
- import java .util .Map ;
45
43
46
- import static java .util .Collections .singletonMap ;
47
44
import static org .elasticsearch .common .unit .TimeValue .timeValueMillis ;
48
45
49
46
/**
50
47
* Builds requests for remote version of Elasticsearch. Note that unlike most of the
51
48
* rest of Elasticsearch this file needs to be compatible with very old versions of
52
- * Elasticsearch. Thus is often uses identifiers for versions like {@code 2000099}
49
+ * Elasticsearch. Thus it often uses identifiers for versions like {@code 2000099}
53
50
* for {@code 2.0.0-alpha1}. Do not drop support for features from this file just
54
51
* because the version constants have been removed.
55
52
*/
56
53
final class RemoteRequestBuilders {
57
54
private RemoteRequestBuilders () {}
58
55
59
- static String initialSearchPath (SearchRequest searchRequest ) {
56
+ static Request initialSearch (SearchRequest searchRequest , BytesReference query , Version remoteVersion ) {
60
57
// It is nasty to build paths with StringBuilder but we'll be careful....
61
58
StringBuilder path = new StringBuilder ("/" );
62
59
addIndexesOrTypes (path , "Index" , searchRequest .indices ());
63
60
addIndexesOrTypes (path , "Type" , searchRequest .types ());
64
61
path .append ("_search" );
65
- return path .toString ();
66
- }
62
+ Request request = new Request ("POST" , path .toString ());
67
63
68
- static Map <String , String > initialSearchParams (SearchRequest searchRequest , Version remoteVersion ) {
69
- Map <String , String > params = new HashMap <>();
70
64
if (searchRequest .scroll () != null ) {
71
65
TimeValue keepAlive = searchRequest .scroll ().keepAlive ();
72
66
if (remoteVersion .before (Version .V_5_0_0 )) {
@@ -75,16 +69,16 @@ static Map<String, String> initialSearchParams(SearchRequest searchRequest, Vers
75
69
* timeout seems safer than less. */
76
70
keepAlive = timeValueMillis ((long ) Math .ceil (keepAlive .millisFrac ()));
77
71
}
78
- params . put ("scroll" , keepAlive .getStringRep ());
72
+ request . addParameter ("scroll" , keepAlive .getStringRep ());
79
73
}
80
- params . put ("size" , Integer .toString (searchRequest .source ().size ()));
74
+ request . addParameter ("size" , Integer .toString (searchRequest .source ().size ()));
81
75
if (searchRequest .source ().version () == null || searchRequest .source ().version () == true ) {
82
76
/*
83
77
* Passing `null` here just add the `version` request parameter
84
78
* without any value. This way of requesting the version works
85
79
* for all supported versions of Elasticsearch.
86
80
*/
87
- params . put ("version" , null );
81
+ request . addParameter ("version" , null );
88
82
}
89
83
if (searchRequest .source ().sorts () != null ) {
90
84
boolean useScan = false ;
@@ -101,13 +95,13 @@ static Map<String, String> initialSearchParams(SearchRequest searchRequest, Vers
101
95
}
102
96
}
103
97
if (useScan ) {
104
- params . put ("search_type" , "scan" );
98
+ request . addParameter ("search_type" , "scan" );
105
99
} else {
106
100
StringBuilder sorts = new StringBuilder (sortToUri (searchRequest .source ().sorts ().get (0 )));
107
101
for (int i = 1 ; i < searchRequest .source ().sorts ().size (); i ++) {
108
102
sorts .append (',' ).append (sortToUri (searchRequest .source ().sorts ().get (i )));
109
103
}
110
- params . put ("sort" , sorts .toString ());
104
+ request . addParameter ("sort" , sorts .toString ());
111
105
}
112
106
}
113
107
if (remoteVersion .before (Version .fromId (2000099 ))) {
@@ -126,20 +120,18 @@ static Map<String, String> initialSearchParams(SearchRequest searchRequest, Vers
126
120
fields .append (',' ).append (searchRequest .source ().storedFields ().fieldNames ().get (i ));
127
121
}
128
122
String storedFieldsParamName = remoteVersion .before (Version .V_5_0_0_alpha4 ) ? "fields" : "stored_fields" ;
129
- params . put (storedFieldsParamName , fields .toString ());
123
+ request . addParameter (storedFieldsParamName , fields .toString ());
130
124
}
131
- return params ;
132
- }
133
125
134
- static HttpEntity initialSearchEntity (SearchRequest searchRequest , BytesReference query , Version remoteVersion ) {
135
126
// EMPTY is safe here because we're not calling namedObject
136
127
try (XContentBuilder entity = JsonXContent .contentBuilder ();
137
128
XContentParser queryParser = XContentHelper
138
129
.createParser (NamedXContentRegistry .EMPTY , LoggingDeprecationHandler .INSTANCE , query )) {
139
130
entity .startObject ();
140
131
141
132
entity .field ("query" ); {
142
- /* We're intentionally a bit paranoid here - copying the query as xcontent rather than writing a raw field. We don't want
133
+ /* We're intentionally a bit paranoid here - copying the query
134
+ * as xcontent rather than writing a raw field. We don't want
143
135
* poorly written queries to escape. Ever. */
144
136
entity .copyCurrentStructure (queryParser );
145
137
XContentParser .Token shouldBeEof = queryParser .nextToken ();
@@ -160,10 +152,11 @@ static HttpEntity initialSearchEntity(SearchRequest searchRequest, BytesReferenc
160
152
161
153
entity .endObject ();
162
154
BytesRef bytes = BytesReference .bytes (entity ).toBytesRef ();
163
- return new ByteArrayEntity (bytes .bytes , bytes .offset , bytes .length , ContentType .APPLICATION_JSON );
155
+ request . setEntity ( new ByteArrayEntity (bytes .bytes , bytes .offset , bytes .length , ContentType .APPLICATION_JSON ) );
164
156
} catch (IOException e ) {
165
157
throw new ElasticsearchException ("unexpected error building entity" , e );
166
158
}
159
+ return request ;
167
160
}
168
161
169
162
private static void addIndexesOrTypes (StringBuilder path , String name , String [] indicesOrTypes ) {
@@ -193,45 +186,50 @@ private static String sortToUri(SortBuilder<?> sort) {
193
186
throw new IllegalArgumentException ("Unsupported sort [" + sort + "]" );
194
187
}
195
188
196
- static String scrollPath () {
197
- return "/_search/scroll" ;
198
- }
189
+ static Request scroll (String scroll , TimeValue keepAlive , Version remoteVersion ) {
190
+ Request request = new Request ("POST" , "/_search/scroll" );
199
191
200
- static Map <String , String > scrollParams (TimeValue keepAlive , Version remoteVersion ) {
201
192
if (remoteVersion .before (Version .V_5_0_0 )) {
202
193
/* Versions of Elasticsearch before 5.0 couldn't parse nanos or micros
203
194
* so we toss out that resolution, rounding up so we shouldn't end up
204
195
* with 0s. */
205
196
keepAlive = timeValueMillis ((long ) Math .ceil (keepAlive .millisFrac ()));
206
197
}
207
- return singletonMap ("scroll" , keepAlive .getStringRep ());
208
- }
198
+ request .addParameter ("scroll" , keepAlive .getStringRep ());
209
199
210
- static HttpEntity scrollEntity (String scroll , Version remoteVersion ) {
211
200
if (remoteVersion .before (Version .fromId (2000099 ))) {
212
201
// Versions before 2.0.0 extract the plain scroll_id from the body
213
- return new StringEntity (scroll , ContentType .TEXT_PLAIN );
202
+ request .setEntity (new StringEntity (scroll , ContentType .TEXT_PLAIN ));
203
+ return request ;
214
204
}
205
+
215
206
try (XContentBuilder entity = JsonXContent .contentBuilder ()) {
216
- return new StringEntity (Strings .toString (entity .startObject ()
217
- .field ("scroll_id" , scroll )
218
- .endObject ()), ContentType .APPLICATION_JSON );
207
+ entity .startObject ()
208
+ .field ("scroll_id" , scroll )
209
+ .endObject ();
210
+ request .setEntity (new StringEntity (Strings .toString (entity ), ContentType .APPLICATION_JSON ));
219
211
} catch (IOException e ) {
220
212
throw new ElasticsearchException ("failed to build scroll entity" , e );
221
213
}
214
+ return request ;
222
215
}
223
216
224
- static HttpEntity clearScrollEntity (String scroll , Version remoteVersion ) {
217
+ static Request clearScroll (String scroll , Version remoteVersion ) {
218
+ Request request = new Request ("DELETE" , "/_search/scroll" );
219
+
225
220
if (remoteVersion .before (Version .fromId (2000099 ))) {
226
221
// Versions before 2.0.0 extract the plain scroll_id from the body
227
- return new StringEntity (scroll , ContentType .TEXT_PLAIN );
222
+ request .setEntity (new StringEntity (scroll , ContentType .TEXT_PLAIN ));
223
+ return request ;
228
224
}
229
225
try (XContentBuilder entity = JsonXContent .contentBuilder ()) {
230
- return new StringEntity (Strings .toString (entity .startObject ()
231
- .array ("scroll_id" , scroll )
232
- .endObject ()), ContentType .APPLICATION_JSON );
226
+ entity .startObject ()
227
+ .array ("scroll_id" , scroll )
228
+ .endObject ();
229
+ request .setEntity (new StringEntity (Strings .toString (entity ), ContentType .APPLICATION_JSON ));
233
230
} catch (IOException e ) {
234
231
throw new ElasticsearchException ("failed to build clear scroll entity" , e );
235
232
}
233
+ return request ;
236
234
}
237
235
}
0 commit comments