1919
2020package org .elasticsearch .index .reindex .remote ;
2121
22- import org .apache .http .HttpEntity ;
2322import org .apache .http .entity .ByteArrayEntity ;
2423import org .apache .http .entity .ContentType ;
2524import org .apache .http .entity .StringEntity ;
2625import org .apache .lucene .util .BytesRef ;
2726import org .elasticsearch .ElasticsearchException ;
2827import org .elasticsearch .Version ;
2928import org .elasticsearch .action .search .SearchRequest ;
29+ import org .elasticsearch .client .Request ;
3030import org .elasticsearch .common .Strings ;
3131import org .elasticsearch .common .bytes .BytesReference ;
3232import org .elasticsearch .common .unit .TimeValue ;
4040import org .elasticsearch .search .sort .SortBuilder ;
4141
4242import java .io .IOException ;
43- import java .util .HashMap ;
44- import java .util .Map ;
4543
46- import static java .util .Collections .singletonMap ;
4744import static org .elasticsearch .common .unit .TimeValue .timeValueMillis ;
4845
4946/**
5047 * Builds requests for remote version of Elasticsearch. Note that unlike most of the
5148 * 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}
5350 * for {@code 2.0.0-alpha1}. Do not drop support for features from this file just
5451 * because the version constants have been removed.
5552 */
5653final class RemoteRequestBuilders {
5754 private RemoteRequestBuilders () {}
5855
59- static String initialSearchPath (SearchRequest searchRequest ) {
56+ static Request initialSearch (SearchRequest searchRequest , BytesReference query , Version remoteVersion ) {
6057 // It is nasty to build paths with StringBuilder but we'll be careful....
6158 StringBuilder path = new StringBuilder ("/" );
6259 addIndexesOrTypes (path , "Index" , searchRequest .indices ());
6360 addIndexesOrTypes (path , "Type" , searchRequest .types ());
6461 path .append ("_search" );
65- return path .toString ();
66- }
62+ Request request = new Request ("POST" , path .toString ());
6763
68- static Map <String , String > initialSearchParams (SearchRequest searchRequest , Version remoteVersion ) {
69- Map <String , String > params = new HashMap <>();
7064 if (searchRequest .scroll () != null ) {
7165 TimeValue keepAlive = searchRequest .scroll ().keepAlive ();
7266 if (remoteVersion .before (Version .V_5_0_0 )) {
@@ -75,16 +69,16 @@ static Map<String, String> initialSearchParams(SearchRequest searchRequest, Vers
7569 * timeout seems safer than less. */
7670 keepAlive = timeValueMillis ((long ) Math .ceil (keepAlive .millisFrac ()));
7771 }
78- params . put ("scroll" , keepAlive .getStringRep ());
72+ request . addParameter ("scroll" , keepAlive .getStringRep ());
7973 }
80- params . put ("size" , Integer .toString (searchRequest .source ().size ()));
74+ request . addParameter ("size" , Integer .toString (searchRequest .source ().size ()));
8175 if (searchRequest .source ().version () == null || searchRequest .source ().version () == true ) {
8276 /*
8377 * Passing `null` here just add the `version` request parameter
8478 * without any value. This way of requesting the version works
8579 * for all supported versions of Elasticsearch.
8680 */
87- params . put ("version" , null );
81+ request . addParameter ("version" , null );
8882 }
8983 if (searchRequest .source ().sorts () != null ) {
9084 boolean useScan = false ;
@@ -101,13 +95,13 @@ static Map<String, String> initialSearchParams(SearchRequest searchRequest, Vers
10195 }
10296 }
10397 if (useScan ) {
104- params . put ("search_type" , "scan" );
98+ request . addParameter ("search_type" , "scan" );
10599 } else {
106100 StringBuilder sorts = new StringBuilder (sortToUri (searchRequest .source ().sorts ().get (0 )));
107101 for (int i = 1 ; i < searchRequest .source ().sorts ().size (); i ++) {
108102 sorts .append (',' ).append (sortToUri (searchRequest .source ().sorts ().get (i )));
109103 }
110- params . put ("sort" , sorts .toString ());
104+ request . addParameter ("sort" , sorts .toString ());
111105 }
112106 }
113107 if (remoteVersion .before (Version .fromId (2000099 ))) {
@@ -126,20 +120,18 @@ static Map<String, String> initialSearchParams(SearchRequest searchRequest, Vers
126120 fields .append (',' ).append (searchRequest .source ().storedFields ().fieldNames ().get (i ));
127121 }
128122 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 ());
130124 }
131- return params ;
132- }
133125
134- static HttpEntity initialSearchEntity (SearchRequest searchRequest , BytesReference query , Version remoteVersion ) {
135126 // EMPTY is safe here because we're not calling namedObject
136127 try (XContentBuilder entity = JsonXContent .contentBuilder ();
137128 XContentParser queryParser = XContentHelper
138129 .createParser (NamedXContentRegistry .EMPTY , LoggingDeprecationHandler .INSTANCE , query )) {
139130 entity .startObject ();
140131
141132 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
143135 * poorly written queries to escape. Ever. */
144136 entity .copyCurrentStructure (queryParser );
145137 XContentParser .Token shouldBeEof = queryParser .nextToken ();
@@ -160,10 +152,11 @@ static HttpEntity initialSearchEntity(SearchRequest searchRequest, BytesReferenc
160152
161153 entity .endObject ();
162154 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 ) );
164156 } catch (IOException e ) {
165157 throw new ElasticsearchException ("unexpected error building entity" , e );
166158 }
159+ return request ;
167160 }
168161
169162 private static void addIndexesOrTypes (StringBuilder path , String name , String [] indicesOrTypes ) {
@@ -193,45 +186,50 @@ private static String sortToUri(SortBuilder<?> sort) {
193186 throw new IllegalArgumentException ("Unsupported sort [" + sort + "]" );
194187 }
195188
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" );
199191
200- static Map <String , String > scrollParams (TimeValue keepAlive , Version remoteVersion ) {
201192 if (remoteVersion .before (Version .V_5_0_0 )) {
202193 /* Versions of Elasticsearch before 5.0 couldn't parse nanos or micros
203194 * so we toss out that resolution, rounding up so we shouldn't end up
204195 * with 0s. */
205196 keepAlive = timeValueMillis ((long ) Math .ceil (keepAlive .millisFrac ()));
206197 }
207- return singletonMap ("scroll" , keepAlive .getStringRep ());
208- }
198+ request .addParameter ("scroll" , keepAlive .getStringRep ());
209199
210- static HttpEntity scrollEntity (String scroll , Version remoteVersion ) {
211200 if (remoteVersion .before (Version .fromId (2000099 ))) {
212201 // 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 ;
214204 }
205+
215206 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 ));
219211 } catch (IOException e ) {
220212 throw new ElasticsearchException ("failed to build scroll entity" , e );
221213 }
214+ return request ;
222215 }
223216
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+
225220 if (remoteVersion .before (Version .fromId (2000099 ))) {
226221 // 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 ;
228224 }
229225 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 ));
233230 } catch (IOException e ) {
234231 throw new ElasticsearchException ("failed to build clear scroll entity" , e );
235232 }
233+ return request ;
236234 }
237235}
0 commit comments