23
23
import org .elasticsearch .action .bulk .BulkRequestBuilder ;
24
24
import org .elasticsearch .action .search .SearchRequest ;
25
25
import org .elasticsearch .common .bytes .BytesArray ;
26
- import org .elasticsearch .common .bytes .BytesReference ;
27
26
import org .elasticsearch .common .xcontent .XContentType ;
28
27
import org .elasticsearch .common .xcontent .json .JsonXContent ;
29
28
import org .elasticsearch .plugins .Plugin ;
@@ -152,25 +151,22 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws
152
151
public void testIndexedTemplateClient () throws Exception {
153
152
assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
154
153
.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 ));
174
170
175
171
GetStoredScriptResponse getResponse = client ().admin ().cluster ()
176
172
.prepareGetStoredScript ("testTemplate" ).get ();
@@ -198,41 +194,32 @@ public void testIndexedTemplateClient() throws Exception {
198
194
199
195
getResponse = client ().admin ().cluster ().prepareGetStoredScript ("testTemplate" ).get ();
200
196
assertNull (getResponse .getSource ());
201
- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
202
197
}
203
198
204
199
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 )
216
217
);
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 )
227
220
);
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 )
236
223
);
237
224
238
225
BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ();
@@ -268,7 +255,6 @@ public void testIndexedTemplate() throws Exception {
268
255
.setScript ("2" ).setScriptType (ScriptType .STORED ).setScriptParams (templateParams )
269
256
.get ();
270
257
assertHitCount (searchResponse .getResponse (), 1 );
271
- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
272
258
}
273
259
274
260
// Relates to #10397
@@ -282,13 +268,27 @@ public void testIndexedTemplateOverwrite() throws Exception {
282
268
client ().admin ().indices ().prepareRefresh ().get ();
283
269
284
270
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
+ "}" ;
285
287
for (int i = 1 ; i < iterations ; i ++) {
286
288
assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
287
289
.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
+ );
292
292
293
293
GetStoredScriptResponse getResponse = client ().admin ().cluster ().prepareGetStoredScript ("git01" ).get ();
294
294
assertNotNull (getResponse .getSource ());
@@ -304,25 +304,39 @@ public void testIndexedTemplateOverwrite() throws Exception {
304
304
305
305
assertAcked (client ().admin ().cluster ().preparePutStoredScript ()
306
306
.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
+ );
309
309
310
310
SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder (client ())
311
311
.setRequest (new SearchRequest ("testindex" ).types ("test" ))
312
312
.setScript ("git01" ).setScriptType (ScriptType .STORED ).setScriptParams (templateParams )
313
313
.get ();
314
314
assertHitCount (searchResponse .getResponse (), 1 );
315
315
}
316
- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
317
316
}
318
317
319
318
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
+ "}" ;
321
336
assertAcked (
322
337
client ().admin ().cluster ().preparePutStoredScript ()
323
338
.setId ("4" )
324
- .setContent (BytesReference .bytes (jsonBuilder ().startObject ().field ("template" , multiQuery ).endObject ()),
325
- XContentType .JSON )
339
+ .setContent (new BytesArray (multiQuery ), XContentType .JSON )
326
340
);
327
341
BulkRequestBuilder bulkRequestBuilder = client ().prepareBulk ();
328
342
bulkRequestBuilder .add (client ().prepareIndex ("test" , "type" , "1" ).setSource ("{\" theField\" :\" foo\" }" , XContentType .JSON ));
@@ -342,7 +356,6 @@ public void testIndexedTemplateWithArray() throws Exception {
342
356
.setScript ("4" ).setScriptType (ScriptType .STORED ).setScriptParams (arrayTemplateParams )
343
357
.get ();
344
358
assertHitCount (searchResponse .getResponse (), 5 );
345
- assertWarnings ("the template context is now deprecated. Specify templates in a \" script\" element." );
346
359
}
347
360
348
361
}
0 commit comments