You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _api-reference/search-apis/index.md
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -38,4 +38,5 @@ These APIs help you test, debug, and optimize your search operations:
38
38
These APIs allow you to work with search templates:
39
39
40
40
-**[Search template]({{site.url}}{{site.baseurl}}/api-reference/search-apis/search-template/)**: Use search templates to run parameterized search queries.
41
-
-**[Multi-search template]({{site.url}}{{site.baseurl}}/api-reference/search-apis/msearch-template/)**: Execute multiple search template requests in a single API call.
41
+
-**[Multi-search template]({{site.url}}{{site.baseurl}}/api-reference/search-apis/search-template/msearch-template/)**: Execute multiple search template requests in a single API call.
42
+
-**[Render template]({{site.url}}{{site.baseurl}}/api-reference/search-apis/search-template/render-template/)**: Previews the final query generated from a search template by substituting parameters without executing the search.
Copy file name to clipboardExpand all lines: _api-reference/search-apis/search-template/index.md
+30-34Lines changed: 30 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,13 @@
2
2
layout: default
3
3
title: Search templates
4
4
parent: Search APIs
5
+
has_children: true
5
6
nav_order: 90
6
7
redirect_from:
7
8
- /opensearch/search-template/
8
9
- /search-plugins/search-template/
9
10
- /api-reference/search-template/
11
+
- /api-reference/search-apis/search-template/
10
12
---
11
13
12
14
# Search templates
@@ -43,13 +45,14 @@ GET _search/template
43
45
}
44
46
}
45
47
```
48
+
{% include copy-curl.html %}
46
49
47
-
This template runs the search on your entire cluster.
48
-
To run this search on a specific index, add the index name to the request:
50
+
This template runs the search on your entire cluster. To run this search on a specific index, add the index name to the request:
49
51
50
52
```json
51
53
GET shakespeare/_search/template
52
54
```
55
+
{% include copy-curl.html %}
53
56
54
57
Specify the `from` and `size` parameters:
55
58
@@ -72,14 +75,16 @@ GET _search/template
72
75
}
73
76
}
74
77
```
78
+
{% include copy-curl.html %}
75
79
76
80
To improve the search experience, you can define defaults so the user doesn’t have to specify every possible parameter. If the parameter is not defined in the `params` section, OpenSearch uses the default value.
77
81
78
82
The syntax for defining the default value for a variable `var` is as follows:
79
83
80
-
```json
84
+
```
81
85
{% raw %}{{var}}{{^var}}default value{{/var}}{% endraw %}
82
86
```
87
+
{% include copy.html %}
83
88
84
89
This command sets the defaults for `from` as 10 and `size` as 10:
85
90
@@ -100,6 +105,7 @@ GET _search/template
100
105
}
101
106
}
102
107
```
108
+
{% include copy-curl.html %}
103
109
104
110
105
111
## Save and execute search templates
@@ -118,7 +124,7 @@ POST _scripts/play_search_template
118
124
"size": "{% raw %}{{size}}{{^size}}10{{/size}}{% endraw %}",
119
125
"query": {
120
126
"match": {
121
-
"play_name": "{{play_name}}"
127
+
"play_name": "{% raw %}{{play_name}}{% endraw %}"
122
128
}
123
129
}
124
130
},
@@ -129,8 +135,7 @@ POST _scripts/play_search_template
129
135
}
130
136
```
131
137
132
-
Now you can reuse the template by referring to its `id` parameter.
133
-
You can reuse this source template for different input values.
138
+
Now you can reuse the template by referring to its `id` parameter. You can reuse this source template for different input values:
134
139
135
140
```json
136
141
GET _search/template
@@ -143,7 +148,9 @@ GET _search/template
143
148
}
144
149
}
145
150
```
146
-
#### Sample output
151
+
{% include copy-curl.html %}
152
+
153
+
## Example response
147
154
148
155
```json
149
156
{
@@ -193,31 +200,9 @@ POST _render/template
193
200
}
194
201
}
195
202
```
203
+
{% include copy-curl.html %}
196
204
197
-
#### Sample output
198
-
199
-
```json
200
-
{
201
-
"template_output": {
202
-
"from": "0",
203
-
"size": "10",
204
-
"query": {
205
-
"match": {
206
-
"play_name": "Henry IV"
207
-
}
208
-
}
209
-
}
210
-
}
211
-
```
212
-
213
-
The following render operations are supported:
214
-
215
-
```json
216
-
GET /_render/template
217
-
POST /_render/template
218
-
GET /_render/template/<id>
219
-
POST /_render/template/<id>
220
-
```
205
+
For more information, see [Render Template API]({{site.url}}{{site.baseurl}}/api-reference/search-apis/search-template/render-template/).
221
206
222
207
## Advanced parameter conversion with search templates
223
208
@@ -231,6 +216,7 @@ Use the section tag in Mustache to represent conditions:
231
216
```json
232
217
{% raw %}{{#var}}var{{/var}}{% endraw %}
233
218
```
219
+
{% include copy.html %}
234
220
235
221
When `var` is a Boolean value, this syntax acts as an `if` condition. The `{% raw %}{{#var}}{% endraw %}` and `{% raw %}{{/var}}{% endraw %}` tags insert the values placed between them only if `var` evaluates to `true`.
236
222
@@ -250,9 +236,9 @@ GET _search/template
250
236
}
251
237
}
252
238
```
239
+
{% include copy-curl.html %}
253
240
254
-
You can also design an `if-else` condition.
255
-
This command sets `size` to `2` if `limit` is `true`. Otherwise, it sets `size` to `10`.
241
+
You can also design an `if-else` condition. This command sets `size` to `2` if `limit` is `true`. Otherwise, it sets `size` to `10`:
256
242
257
243
```json
258
244
GET _search/template
@@ -264,14 +250,16 @@ GET _search/template
264
250
}
265
251
}
266
252
```
253
+
{% include copy-curl.html %}
267
254
268
255
### Loops
269
256
270
-
You can also use the section tag to implement a foreach loop:
257
+
You can also use the section tag to implement a for-each loop:
271
258
272
259
```
273
260
{% raw %}{{#var}}{{.}}{{/var}}{% endraw %}
274
261
```
262
+
{% include copy.html %}
275
263
276
264
When `var` is an array, the search template iterates through it and creates a `terms` query.
Copy file name to clipboardExpand all lines: _api-reference/search-apis/search-template/render-template.md
+13-14Lines changed: 13 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,17 @@
1
1
---
2
2
layout: default
3
3
title: Render template
4
-
parent: Search APIs
5
-
nav_order: 82
4
+
parent: Search templates
5
+
grand_parent: Search APIs
6
+
nav_order: 10
6
7
redirect_from:
7
8
- /api-reference/render-template/
9
+
- /api-reference/search-apis/render-template/
8
10
---
9
11
10
12
# Render template
11
13
12
-
The Render Template API renders a [search template]({{site.url}}{{site.baseurl}}/search-plugins/search-template/)as a search query.
14
+
The Render Template API previews the final query generated from a [search template]({{site.url}}{{site.baseurl}}/search-plugins/search-template/)by substituting parameters without executing the search.
13
15
14
16
## Endpoints
15
17
@@ -22,17 +24,17 @@ POST /_render/template/<id>
22
24
23
25
## Path parameters
24
26
25
-
The Render Template API supports the following optional path parameter.
27
+
The following table lists the available path parameters. All path parameters are optional.
26
28
27
-
| Parameter |Type| Description |
29
+
| Parameter |Data type| Description |
28
30
| :--- | :--- | :--- |
29
31
|`id`| String | The ID of the search template to render. |
30
32
31
33
## Request body fields
32
34
33
-
The following options are supported in the request body of the Render Template API.
35
+
The following table lists the available request body fields.
34
36
35
-
| Parameter | Required |Type| Description |
37
+
| Parameter | Required |Data type| Description |
36
38
| :--- | :--- | :--- | :--- |
37
39
|`id`| Conditional | String | The ID of the search template to render. Is not required if the ID is provided in the path or if an inline template is specified by the `source`. |
38
40
|`params`| No | Object | A list of key-value pairs that replace Mustache variables found in the search template. The key-value pairs must exist in the documents being searched. |
@@ -56,6 +58,7 @@ Both of the following request examples use the search template with the template
56
58
}
57
59
}
58
60
```
61
+
{% include copy.html %}
59
62
60
63
### Render template using template ID
61
64
@@ -70,13 +73,13 @@ POST _render/template
70
73
}
71
74
}
72
75
```
73
-
{% include copy.html %}
76
+
{% include copy-curl.html %}
74
77
75
78
### Render template using `_source`
76
79
77
80
If you don't want to use a saved template, or want to test a template before saving, you can test a template with the `_source` parameter using [Mustache](https://mustache.github.io/mustache.5.html) variables, as shown in the following example:
78
81
79
-
```
82
+
```json
80
83
{
81
84
"source": {
82
85
"from": "{% raw %}{{from}}{{^from}}0{{/from}}{% endraw %}",
@@ -110,8 +113,4 @@ OpenSearch responds with information about the template's output:
0 commit comments