11[[search-template]]
22=== Search Template
33
4- The `/_search/template` endpoint allows to use the mustache language to pre render search requests,
5- before they are executed and fill existing templates with template parameters.
4+ Allows you to use the mustache language to pre render search requests.
65
76[source,js]
87------------------------------------------
@@ -22,19 +21,87 @@ GET _search/template
2221// CONSOLE
2322// TEST[setup:twitter]
2423
24+ [[search-template-api-request]]
25+ ==== {api-request-title}
26+
27+ `GET _search/template`
28+
29+
30+ [[search-template-api-desc]]
31+ ==== {api-description-title}
32+
33+ The `/_search/template` endpoint allows you to use the mustache language to pre-
34+ render search requests, before they are executed and fill existing templates
35+ with template parameters.
36+
2537For more information on how Mustache templating and what kind of templating you
2638can do with it check out the http://mustache.github.io/mustache.5.html[online
2739documentation of the mustache project].
2840
29- NOTE: The mustache language is implemented in Elasticsearch as a sandboxed
30- scripting language, hence it obeys settings that may be used to enable or
31- disable scripts per type and context as described in the
32- <<allowed-script-types-setting, scripting docs>>
41+ NOTE: The mustache language is implemented in {es} as a sandboxed scripting
42+ language, hence it obeys settings that may be used to enable or disable scripts
43+ per type and context as described in the
44+ <<allowed-script-types-setting, scripting docs>>.
45+
46+
47+ [[search-template-api-path-params]]
48+ ==== {api-path-parms-title}
49+
50+ include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
51+
52+
53+ [[search-template-api-query-params]]
54+ ==== {api-query-parms-title}
55+
56+ include::{docdir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
57+
58+ `ccs_minimize_roundtrips`::
59+ (Optional, boolean) If `true`, network round-trips are minimized for
60+ cross-cluster search requests. Defaults to `true`.
61+
62+ include::{docdir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
63+
64+ `explain`::
65+ (Optional, boolean) If `true`, the response includes additional details about
66+ score computation as part of a hit. Defaults to `false`.
67+
68+ `ignore_throttled`::
69+ (Optional, boolean) If `true`, specified concrete, expanded or aliased indices
70+ are not included in the response when throttled. Defaults to `false`.
71+
72+ include::{docdir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
73+
74+ include::{docdir}/rest-api/common-parms.asciidoc[tag=preference]
75+
76+ `profile`::
77+ (Optional, boolean) If `true`, the query execution is profiled. Defaults
78+ to `false`.
79+
80+ `rest_total_hits_as_int`::
81+ (Optional, boolean) If `true`, `hits.total` are rendered as an integer in
82+ the response. Defaults to `false`.
83+
84+ include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
85+
86+ include::{docdir}/rest-api/common-parms.asciidoc[tag=scroll]
87+
88+ include::{docdir}/rest-api/common-parms.asciidoc[tag=search_type]
89+
90+ `typed_keys`::
91+ (Optional, boolean) If `true`, aggregation and suggester names are
92+ prefixed by their respective types in the response. Defaults to `false`.
93+
94+
95+ [[search-template-api-request-body]]
96+ ==== {api-request-body-title}
97+
98+ The API request body must contain the search definition template and its parameters.
99+
100+
101+ [[search-template-api-example]]
102+ ==== {api-response-codes-title}
33103
34- [float]
35- ==== Examples
36104
37- [float]
38105[[pre-registered-templates]]
39106===== Store a search template
40107
@@ -61,8 +128,8 @@ POST _scripts/<templateid>
61128
62129//////////////////////////
63130
64- We want to be sure that the template has been created,
65- because we'll use it later.
131+ The API returns the following result if the template has been successfully
132+ created:
66133
67134[source,js]
68135--------------------------------------------------
@@ -74,7 +141,8 @@ because we'll use it later.
74141
75142//////////////////////////
76143
77- This template can be retrieved by
144+
145+ The template can be retrieved by calling
78146
79147[source,js]
80148------------------------------------------
@@ -83,7 +151,7 @@ GET _scripts/<templateid>
83151// CONSOLE
84152// TEST[continued]
85153
86- which is rendered as :
154+ The API returns the following result :
87155
88156[source,js]
89157------------------------------------------
@@ -101,7 +169,8 @@ which is rendered as:
101169------------------------------------------
102170// TESTRESPONSE
103171
104- This template can be deleted by
172+
173+ This template can be deleted by calling
105174
106175[source,js]
107176------------------------------------------
@@ -110,26 +179,12 @@ DELETE _scripts/<templateid>
110179// CONSOLE
111180// TEST[continued]
112181
113- //////////////////////////
114-
115- We want to be sure that the template has been created,
116- because we'll use it later.
117-
118- [source,js]
119- --------------------------------------------------
120- {
121- "acknowledged" : true
122- }
123- --------------------------------------------------
124- // TESTRESPONSE
125182
126- //////////////////////////
127183
128- [float]
129184[[use-registered-templates]]
130- ===== Use a stored search template
185+ ===== Using a stored search template
131186
132- To use a stored template at search time use :
187+ To use a stored template at search time send the following request :
133188
134189[source,js]
135190------------------------------------------
@@ -145,11 +200,12 @@ GET _search/template
145200// TEST[catch:missing]
146201<1> Name of the stored template script.
147202
148- [float]
203+
149204[[_validating_templates]]
150- ==== Validate a search template
205+ ==== Validating a search template
151206
152- A template can be rendered in a response with given parameters using
207+ A template can be rendered in a response with given parameters by using the
208+ following request:
153209
154210[source,js]
155211------------------------------------------
@@ -165,7 +221,8 @@ GET _render/template
165221------------------------------------------
166222// CONSOLE
167223
168- This call will return the rendered template:
224+
225+ The API returns the rendered template:
169226
170227[source,js]
171228------------------------------------------
@@ -185,7 +242,8 @@ This call will return the rendered template:
185242// TESTRESPONSE
186243<1> `status` array has been populated with values from the `params` object.
187244
188- Stored templates can also be rendered using
245+
246+ Stored templates can also be rendered by calling the following request:
189247
190248[source,js]
191249------------------------------------------
@@ -198,10 +256,10 @@ GET _render/template/<template_name>
198256------------------------------------------
199257// NOTCONSOLE
200258
201- [float ]
202- ===== Explain
259+ [[search-template-explain-parameter] ]
260+ ===== Using the explain parameter
203261
204- You can use `explain` parameter when running a template:
262+ You can use the `explain` parameter when running a template:
205263
206264[source,js]
207265------------------------------------------
@@ -217,10 +275,11 @@ GET _search/template
217275// CONSOLE
218276// TEST[catch:missing]
219277
220- [float]
278+
279+ [[search-template-profile-parameter]]
221280===== Profiling
222281
223- You can use `profile` parameter when running a template:
282+ You can use the `profile` parameter when running a template:
224283
225284[source,js]
226285------------------------------------------
@@ -236,7 +295,8 @@ GET _search/template
236295// CONSOLE
237296// TEST[catch:missing]
238297
239- [float]
298+
299+ [[search-template-query-string-single]]
240300===== Filling in a query string with a single value
241301
242302[source,js]
@@ -258,7 +318,7 @@ GET _search/template
258318// CONSOLE
259319// TEST[setup:twitter]
260320
261- [float ]
321+ [[search-template-converting-to-json] ]
262322===== Converting parameters to JSON
263323
264324The `{{#toJson}}parameter{{/toJson}}` function can be used to convert parameters
@@ -337,7 +397,7 @@ which is rendered as:
337397------------------------------------------
338398// NOTCONSOLE
339399
340- [float ]
400+ [[search-template-concatenate-array] ]
341401===== Concatenating array of values
342402
343403The `{{#join}}array{{/join}}` function can be used to concatenate the
@@ -422,7 +482,7 @@ which is rendered as:
422482------------------------------------------
423483// NOTCONSOLE
424484
425- [float ]
485+ [[search-template-default-values] ]
426486===== Default values
427487
428488A default value is written as `{{var}}{{^var}}default{{/var}}` for instance:
@@ -476,7 +536,7 @@ for `end`:
476536------------------------------------------
477537// NOTCONSOLE
478538
479- [float ]
539+ [[search-template-conditional-clauses] ]
480540===== Conditional clauses
481541
482542Conditional clauses cannot be expressed using the JSON form of the template.
@@ -485,6 +545,7 @@ we wanted to run a `match` query on the `line` field, and optionally wanted
485545to filter by line numbers, where `start` and `end` are optional.
486546
487547The `params` would look like:
548+
488549[source,js]
489550------------------------------------------
490551{
@@ -500,6 +561,7 @@ The `params` would look like:
500561// NOTCONSOLE
501562<1> All three of these elements are optional.
502563
564+
503565We could write the query as:
504566
505567[source,js]
@@ -556,11 +618,12 @@ via the REST API, should be written as a string:
556618==================================
557619
558620
559- [float ]
621+ [[search-template-encode-urls] ]
560622===== Encoding URLs
561623
562624The `{{#url}}value{{/url}}` function can be used to encode a string value
563- in a HTML encoding form as defined in by the http://www.w3.org/TR/html4/[HTML specification].
625+ in a HTML encoding form as defined in by the
626+ http://www.w3.org/TR/html4/[HTML specification].
564627
565628As an example, it is useful to encode a URL:
566629
@@ -583,6 +646,7 @@ GET _render/template
583646------------------------------------------
584647// CONSOLE
585648
649+
586650The previous query will be rendered as:
587651
588652[source,js]
@@ -602,8 +666,19 @@ The previous query will be rendered as:
602666[[multi-search-template]]
603667=== Multi Search Template
604668
605- The multi search template API allows to execute several search template
606- requests within the same API using the `_msearch/template` endpoint.
669+ Allows to execute several search template requests.
670+
671+ [[multi-search-template-api-request]]
672+ ==== {api-request-title}
673+
674+ `GET _msearch/template`
675+
676+
677+ [[multi-search-template-api-desc]]
678+ ==== {api-description-title}
679+
680+ Allows to execute several search template requests within the same API using the
681+ `_msearch/template` endpoint.
607682
608683The format of the request is similar to the <<search-multi-search, Multi
609684Search API>> format:
@@ -617,11 +692,15 @@ body\n
617692--------------------------------------------------
618693// NOTCONSOLE
619694
620- The header part supports the same `index`, `search_type`,
621- `preference`, and `routing` options as the usual Multi Search API.
695+ The header part supports the same `index`, `search_type`, `preference`, and
696+ `routing` options as the Multi Search API.
697+
698+ The body includes a search template body request and supports inline, stored and
699+ file templates.
700+
622701
623- The body includes a search template body request and supports inline,
624- stored and file templates. Here is an example:
702+ [[multi- search- template-api-example]]
703+ ==== {api-examples-title}
625704
626705[source,js]
627706--------------------------------------------------
@@ -643,5 +722,5 @@ $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch/tem
643722The response returns a `responses` array, which includes the search template
644723response for each search template request matching its order in the original
645724multi search template request. If there was a complete failure for that specific
646- search template request, an object with `error` message will be returned in place
647- of the actual search response.
725+ search template request, an object with `error` message will be returned in
726+ place of the actual search response.
0 commit comments