@@ -54,23 +54,16 @@ GET /_search/template
5454------------------------------------------
5555
5656[float]
57- ===== Passing an array of strings
57+ ===== Converting parameters to JSON
58+
59+ The `{{toJson}}parameter{{/toJson}}` function can be used to convert parameters
60+ like maps and array to their JSON representation:
5861
5962[source,js]
6063------------------------------------------
6164GET /_search/template
6265{
63- "inline": {
64- "query": {
65- "terms": {
66- "status": [
67- "{{#status}}",
68- "{{.}}",
69- "{{/status}}"
70- ]
71- }
72- }
73- },
66+ "inline": "{ \"query\": { \"terms\": { \"status\": {{#toJson}}status{{/toJson}} }}}",
7467 "params": {
7568 "status": [ "pending", "published" ]
7669 }
@@ -82,13 +75,52 @@ which is rendered as:
8275[source,js]
8376------------------------------------------
8477{
85- "query": {
86- "terms": {
87- "status": [ "pending", "published" ]
78+ "query": {
79+ "terms": {
80+ "status": [
81+ "pending",
82+ "published"
83+ ]
84+ }
8885 }
8986}
9087------------------------------------------
9188
89+ A more complex example substitutes an array of JSON objects:
90+
91+ [source,js]
92+ ------------------------------------------
93+ {
94+ "inline": "{\"query\":{\"bool\":{\"must\": {{#toJson}}clauses{{/toJson}} }}}",
95+ "params": {
96+ "clauses": [
97+ { "term": "foo" },
98+ { "term": "bar" }
99+ ]
100+ }
101+ }
102+ ------------------------------------------
103+
104+ which is rendered as:
105+
106+ [source,js]
107+ ------------------------------------------
108+ {
109+ "query" : {
110+ "bool" : {
111+ "must" : [
112+ {
113+ "term" : "foo"
114+ },
115+ {
116+ "term" : "bar"
117+ }
118+ ]
119+ }
120+ }
121+ }
122+ ------------------------------------------
123+
92124
93125[float]
94126===== Concatenating array of values
@@ -223,45 +255,6 @@ for `end`:
223255}
224256------------------------------------------
225257
226- [float]
227- ===== Converting parameters to JSON
228-
229- The `{{toJson}}parameter{{/toJson}}` function can be used to convert parameters
230- like maps and array to their JSON representation:
231-
232- [source,js]
233- ------------------------------------------
234- {
235- "inline": "{\"query\":{\"bool\":{\"must\": {{#toJson}}clauses{{/toJson}} }}}",
236- "params": {
237- "clauses": [
238- { "term": "foo" },
239- { "term": "bar" }
240- ]
241- }
242- }
243- ------------------------------------------
244-
245- which is rendered as:
246-
247- [source,js]
248- ------------------------------------------
249- {
250- "query" : {
251- "bool" : {
252- "must" : [
253- {
254- "term" : "foo"
255- },
256- {
257- "term" : "bar"
258- }
259- ]
260- }
261- }
262- }
263- ------------------------------------------
264-
265258
266259[float]
267260===== Conditional clauses
@@ -330,7 +323,7 @@ We could write the query as:
330323==================================
331324As written above, this template is not valid JSON because it includes the
332325_section_ markers like `{{#line_no}}`. For this reason, the template should
333- either be stored in a file (see <<pre-registered-templates>>) or, when used
326+ either be stored in a file (see <<pre-registered-templates>>) or, when used
334327via the REST API, should be written as a string:
335328
336329[source,js]
@@ -467,7 +460,7 @@ This call will return the rendered template:
467460------------------------------------------
468461<1> `status` array has been populated with values from the `params` object.
469462
470- File and indexed templates can also be rendered by replacing `inline` with
463+ File and indexed templates can also be rendered by replacing `inline` with
471464`file` or `id` respectively. For example, to render a file template
472465
473466[source,js]
0 commit comments