1
1
[[search-template]]
2
2
=== Search Template
3
3
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.
6
5
7
6
[source,js]
8
7
------------------------------------------
@@ -22,19 +21,87 @@ GET _search/template
22
21
// CONSOLE
23
22
// TEST[setup:twitter]
24
23
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
+
25
37
For more information on how Mustache templating and what kind of templating you
26
38
can do with it check out the http://mustache.github.io/mustache.5.html[online
27
39
documentation of the mustache project].
28
40
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}
33
103
34
- [float]
35
- ==== Examples
36
104
37
- [float]
38
105
[[pre-registered-templates]]
39
106
===== Store a search template
40
107
@@ -61,8 +128,8 @@ POST _scripts/<templateid>
61
128
62
129
//////////////////////////
63
130
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:
66
133
67
134
[source,js]
68
135
--------------------------------------------------
@@ -74,7 +141,8 @@ because we'll use it later.
74
141
75
142
//////////////////////////
76
143
77
- This template can be retrieved by
144
+
145
+ The template can be retrieved by calling
78
146
79
147
[source,js]
80
148
------------------------------------------
@@ -83,7 +151,7 @@ GET _scripts/<templateid>
83
151
// CONSOLE
84
152
// TEST[continued]
85
153
86
- which is rendered as :
154
+ The API returns the following result :
87
155
88
156
[source,js]
89
157
------------------------------------------
@@ -101,7 +169,8 @@ which is rendered as:
101
169
------------------------------------------
102
170
// TESTRESPONSE
103
171
104
- This template can be deleted by
172
+
173
+ This template can be deleted by calling
105
174
106
175
[source,js]
107
176
------------------------------------------
@@ -110,26 +179,12 @@ DELETE _scripts/<templateid>
110
179
// CONSOLE
111
180
// TEST[continued]
112
181
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
125
182
126
- //////////////////////////
127
183
128
- [float]
129
184
[[use-registered-templates]]
130
- ===== Use a stored search template
185
+ ===== Using a stored search template
131
186
132
- To use a stored template at search time use :
187
+ To use a stored template at search time send the following request :
133
188
134
189
[source,js]
135
190
------------------------------------------
@@ -145,11 +200,12 @@ GET _search/template
145
200
// TEST[catch:missing]
146
201
<1> Name of the stored template script.
147
202
148
- [float]
203
+
149
204
[[_validating_templates]]
150
- ==== Validate a search template
205
+ ==== Validating a search template
151
206
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:
153
209
154
210
[source,js]
155
211
------------------------------------------
@@ -165,7 +221,8 @@ GET _render/template
165
221
------------------------------------------
166
222
// CONSOLE
167
223
168
- This call will return the rendered template:
224
+
225
+ The API returns the rendered template:
169
226
170
227
[source,js]
171
228
------------------------------------------
@@ -185,7 +242,8 @@ This call will return the rendered template:
185
242
// TESTRESPONSE
186
243
<1> `status` array has been populated with values from the `params` object.
187
244
188
- Stored templates can also be rendered using
245
+
246
+ Stored templates can also be rendered by calling the following request:
189
247
190
248
[source,js]
191
249
------------------------------------------
@@ -198,10 +256,10 @@ GET _render/template/<template_name>
198
256
------------------------------------------
199
257
// NOTCONSOLE
200
258
201
- [float ]
202
- ===== Explain
259
+ [[search-template-explain-parameter] ]
260
+ ===== Using the explain parameter
203
261
204
- You can use `explain` parameter when running a template:
262
+ You can use the `explain` parameter when running a template:
205
263
206
264
[source,js]
207
265
------------------------------------------
@@ -217,10 +275,11 @@ GET _search/template
217
275
// CONSOLE
218
276
// TEST[catch:missing]
219
277
220
- [float]
278
+
279
+ [[search-template-profile-parameter]]
221
280
===== Profiling
222
281
223
- You can use `profile` parameter when running a template:
282
+ You can use the `profile` parameter when running a template:
224
283
225
284
[source,js]
226
285
------------------------------------------
@@ -236,7 +295,8 @@ GET _search/template
236
295
// CONSOLE
237
296
// TEST[catch:missing]
238
297
239
- [float]
298
+
299
+ [[search-template-query-string-single]]
240
300
===== Filling in a query string with a single value
241
301
242
302
[source,js]
@@ -258,7 +318,7 @@ GET _search/template
258
318
// CONSOLE
259
319
// TEST[setup:twitter]
260
320
261
- [float ]
321
+ [[search-template-converting-to-json] ]
262
322
===== Converting parameters to JSON
263
323
264
324
The `{{#toJson}}parameter{{/toJson}}` function can be used to convert parameters
@@ -337,7 +397,7 @@ which is rendered as:
337
397
------------------------------------------
338
398
// NOTCONSOLE
339
399
340
- [float ]
400
+ [[search-template-concatenate-array] ]
341
401
===== Concatenating array of values
342
402
343
403
The `{{#join}}array{{/join}}` function can be used to concatenate the
@@ -422,7 +482,7 @@ which is rendered as:
422
482
------------------------------------------
423
483
// NOTCONSOLE
424
484
425
- [float ]
485
+ [[search-template-default-values] ]
426
486
===== Default values
427
487
428
488
A default value is written as `{{var}}{{^var}}default{{/var}}` for instance:
@@ -476,7 +536,7 @@ for `end`:
476
536
------------------------------------------
477
537
// NOTCONSOLE
478
538
479
- [float ]
539
+ [[search-template-conditional-clauses] ]
480
540
===== Conditional clauses
481
541
482
542
Conditional 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
485
545
to filter by line numbers, where `start` and `end` are optional.
486
546
487
547
The `params` would look like:
548
+
488
549
[source,js]
489
550
------------------------------------------
490
551
{
@@ -500,6 +561,7 @@ The `params` would look like:
500
561
// NOTCONSOLE
501
562
<1> All three of these elements are optional.
502
563
564
+
503
565
We could write the query as:
504
566
505
567
[source,js]
@@ -556,11 +618,12 @@ via the REST API, should be written as a string:
556
618
==================================
557
619
558
620
559
- [float ]
621
+ [[search-template-encode-urls] ]
560
622
===== Encoding URLs
561
623
562
624
The `{{#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].
564
627
565
628
As an example, it is useful to encode a URL:
566
629
@@ -583,6 +646,7 @@ GET _render/template
583
646
------------------------------------------
584
647
// CONSOLE
585
648
649
+
586
650
The previous query will be rendered as:
587
651
588
652
[source,js]
@@ -602,8 +666,19 @@ The previous query will be rendered as:
602
666
[[multi-search-template]]
603
667
=== Multi Search Template
604
668
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.
607
682
608
683
The format of the request is similar to the <<search-multi-search, Multi
609
684
Search API>> format:
@@ -617,11 +692,15 @@ body\n
617
692
--------------------------------------------------
618
693
// NOTCONSOLE
619
694
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
+
622
701
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}
625
704
626
705
[source,js]
627
706
--------------------------------------------------
@@ -643,5 +722,5 @@ $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch/tem
643
722
The response returns a `responses` array, which includes the search template
644
723
response for each search template request matching its order in the original
645
724
multi 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