2323import org .elasticsearch .action .bulk .BulkRequestBuilder ;
2424import org .elasticsearch .action .search .SearchRequest ;
2525import org .elasticsearch .common .bytes .BytesArray ;
26- import org .elasticsearch .common .bytes .BytesReference ;
2726import org .elasticsearch .common .xcontent .XContentType ;
2827import org .elasticsearch .common .xcontent .json .JsonXContent ;
2928import org .elasticsearch .plugins .Plugin ;
@@ -152,25 +151,22 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws
152151 public void testIndexedTemplateClient () throws Exception {
153152 assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
154153 .setId ("testTemplate" )
155- .setContent (new BytesArray ("{" +
156- "\" template\" :{" +
157- " \" query\" :{" +
158- " \" match\" :{" +
159- " \" theField\" : \" {{fieldParam}}\" }" +
160- " }" +
161- "}" +
162- "}" ), XContentType .JSON ));
163-
164-
165- assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
166- .setId ("testTemplate" ).setContent (new BytesArray ("{" +
167- "\" template\" :{" +
168- " \" query\" :{" +
169- " \" match\" :{" +
170- " \" theField\" : \" {{fieldParam}}\" }" +
171- " }" +
172- "}" +
173- "}" ), XContentType .JSON ));
154+ .setContent (
155+ new BytesArray (
156+ "{" +
157+ " \" script\" : {" +
158+ " \" lang\" : \" mustache\" ," +
159+ " \" source\" : {" +
160+ " \" query\" : {" +
161+ " \" match\" : {" +
162+ " \" theField\" : \" {{fieldParam}}\" " +
163+ " }" +
164+ " }" +
165+ " }" +
166+ " }" +
167+ "}"
168+ ),
169+ XContentType .JSON ));
174170
175171 GetStoredScriptResponse getResponse = client ().admin ().cluster ()
176172 .prepareGetStoredScript ("testTemplate" ).get ();
@@ -198,41 +194,32 @@ public void testIndexedTemplateClient() throws Exception {
198194
199195 getResponse = client ().admin ().cluster ().prepareGetStoredScript ("testTemplate" ).get ();
200196 assertNull (getResponse .getSource ());
201- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
202197 }
203198
204199 public void testIndexedTemplate () throws Exception {
205- assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
206- .setId ("1a" )
207- .setContent (new BytesArray ("{" +
208- "\" template\" :{" +
209- " \" query\" :{" +
210- " \" match\" :{" +
211- " \" theField\" : \" {{fieldParam}}\" }" +
212- " }" +
213- "}" +
214- "}"
215- ), XContentType .JSON )
200+
201+ String script =
202+ "{" +
203+ " \" script\" : {" +
204+ " \" lang\" : \" mustache\" ," +
205+ " \" source\" : {" +
206+ " \" query\" : {" +
207+ " \" match\" : {" +
208+ " \" theField\" : \" {{fieldParam}}\" " +
209+ " }" +
210+ " }" +
211+ " }" +
212+ " }" +
213+ "}" ;
214+
215+ assertAcked (
216+ client ().admin ().cluster ().preparePutStoredScript ().setId ("1a" ).setContent (new BytesArray (script ), XContentType .JSON )
216217 );
217- assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
218- .setId ("2" )
219- .setContent (new BytesArray ("{" +
220- "\" template\" :{" +
221- " \" query\" :{" +
222- " \" match\" :{" +
223- " \" theField\" : \" {{fieldParam}}\" }" +
224- " }" +
225- "}" +
226- "}" ), XContentType .JSON )
218+ assertAcked (
219+ client ().admin ().cluster ().preparePutStoredScript ().setId ("2" ).setContent (new BytesArray (script ), XContentType .JSON )
227220 );
228- assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
229- .setId ("3" )
230- .setContent (new BytesArray ("{" +
231- "\" template\" :{" +
232- " \" match\" :{" +
233- " \" theField\" : \" {{fieldParam}}\" }" +
234- " }" +
235- "}" ), XContentType .JSON )
221+ assertAcked (
222+ client ().admin ().cluster ().preparePutStoredScript ().setId ("3" ).setContent (new BytesArray (script ), XContentType .JSON )
236223 );
237224
238225 BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ();
@@ -268,7 +255,6 @@ public void testIndexedTemplate() throws Exception {
268255 .setScript ("2" ).setScriptType (ScriptType .STORED ).setScriptParams (templateParams )
269256 .get ();
270257 assertHitCount (searchResponse .getResponse (), 1 );
271- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
272258 }
273259
274260 // Relates to #10397
@@ -282,13 +268,27 @@ public void testIndexedTemplateOverwrite() throws Exception {
282268 client ().admin ().indices ().prepareRefresh ().get ();
283269
284270 int iterations = randomIntBetween (2 , 11 );
271+ String query =
272+ "{" +
273+ " \" script\" : {" +
274+ " \" lang\" : \" mustache\" ," +
275+ " \" source\" : {" +
276+ " \" query\" : {" +
277+ " \" match_phrase_prefix\" : {" +
278+ " \" searchtext\" : {" +
279+ " \" query\" : \" {{P_Keyword1}}\" ," +
280+ " \" slop\" : {{slop}}" +
281+ " }" +
282+ " }" +
283+ " }" +
284+ " }" +
285+ " }" +
286+ "}" ;
285287 for (int i = 1 ; i < iterations ; i ++) {
286288 assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
287289 .setId ("git01" )
288- .setContent (new BytesArray (
289- "{\" template\" :{\" query\" : {\" match_phrase_prefix\" : {\" searchtext\" : {\" query\" : \" {{P_Keyword1}}\" ,"
290- + "\" slop\" : -1}}}}}" ),
291- XContentType .JSON ));
290+ .setContent (new BytesArray (query .replace ("{{slop}}" , Integer .toString (-1 ))), XContentType .JSON )
291+ );
292292
293293 GetStoredScriptResponse getResponse = client ().admin ().cluster ().prepareGetStoredScript ("git01" ).get ();
294294 assertNotNull (getResponse .getSource ());
@@ -304,25 +304,39 @@ public void testIndexedTemplateOverwrite() throws Exception {
304304
305305 assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
306306 .setId ("git01" )
307- .setContent (new BytesArray ("{ \" query\" : { \" match_phrase_prefix \" : { \" searchtext \" : { \" query \" : \" {{P_Keyword1}} \" ," +
308- " \" slop \" : 0}}}}" ), XContentType . JSON ) );
307+ .setContent (new BytesArray (query . replace ( "{{slop}}" , Integer . toString ( 0 ))), XContentType . JSON )
308+ );
309309
310310 SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder (client ())
311311 .setRequest (new SearchRequest ("testindex" ).types ("test" ))
312312 .setScript ("git01" ).setScriptType (ScriptType .STORED ).setScriptParams (templateParams )
313313 .get ();
314314 assertHitCount (searchResponse .getResponse (), 1 );
315315 }
316- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
317316 }
318317
319318 public void testIndexedTemplateWithArray () throws Exception {
320- String multiQuery = "{\" query\" :{\" terms\" :{\" theField\" :[\" {{#fieldParam}}\" ,\" {{.}}\" ,\" {{/fieldParam}}\" ]}}}" ;
319+ String multiQuery =
320+ "{\n " +
321+ " \" script\" : {\n " +
322+ " \" lang\" : \" mustache\" ,\n " +
323+ " \" source\" : {\n " +
324+ " \" query\" : {\n " +
325+ " \" terms\" : {\n " +
326+ " \" theField\" : [\n " +
327+ " \" {{#fieldParam}}\" ,\n " +
328+ " \" {{.}}\" ,\n " +
329+ " \" {{/fieldParam}}\" \n " +
330+ " ]\n " +
331+ " }\n " +
332+ " }\n " +
333+ " }\n " +
334+ " }\n " +
335+ "}" ;
321336 assertAcked (
322337 client ().admin ().cluster ().preparePutStoredScript ()
323338 .setId ("4" )
324- .setContent (BytesReference .bytes (jsonBuilder ().startObject ().field ("template" , multiQuery ).endObject ()),
325- XContentType .JSON )
339+ .setContent (new BytesArray (multiQuery ), XContentType .JSON )
326340 );
327341 BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ();
328342 bulkRequestBuilder .add (client ().prepareIndex ("test" , "type" , "1" ).setSource ("{\" theField\" :\" foo\" }" , XContentType .JSON ));
@@ -342,7 +356,6 @@ public void testIndexedTemplateWithArray() throws Exception {
342356 .setScript ("4" ).setScriptType (ScriptType .STORED ).setScriptParams (arrayTemplateParams )
343357 .get ();
344358 assertHitCount (searchResponse .getResponse (), 5 );
345- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
346359 }
347360
348361}
0 commit comments