Skip to content

Commit 787bb58

Browse files
Refactor search template section (#10147) (#10152)
1 parent e5bfbb9 commit 787bb58

File tree

4 files changed

+49
-51
lines changed

4 files changed

+49
-51
lines changed

_api-reference/search-apis/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ These APIs help you test, debug, and optimize your search operations:
3838
These APIs allow you to work with search templates:
3939

4040
- **[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.

_api-reference/search-apis/search-template.md renamed to _api-reference/search-apis/search-template/index.md

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
layout: default
33
title: Search templates
44
parent: Search APIs
5+
has_children: true
56
nav_order: 90
67
redirect_from:
78
- /opensearch/search-template/
89
- /search-plugins/search-template/
910
- /api-reference/search-template/
11+
- /api-reference/search-apis/search-template/
1012
---
1113

1214
# Search templates
@@ -43,13 +45,14 @@ GET _search/template
4345
}
4446
}
4547
```
48+
{% include copy-curl.html %}
4649

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:
4951

5052
```json
5153
GET shakespeare/_search/template
5254
```
55+
{% include copy-curl.html %}
5356

5457
Specify the `from` and `size` parameters:
5558

@@ -72,14 +75,16 @@ GET _search/template
7275
}
7376
}
7477
```
78+
{% include copy-curl.html %}
7579

7680
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.
7781

7882
The syntax for defining the default value for a variable `var` is as follows:
7983

80-
```json
84+
```
8185
{% raw %}{{var}}{{^var}}default value{{/var}}{% endraw %}
8286
```
87+
{% include copy.html %}
8388

8489
This command sets the defaults for `from` as 10 and `size` as 10:
8590

@@ -100,6 +105,7 @@ GET _search/template
100105
}
101106
}
102107
```
108+
{% include copy-curl.html %}
103109

104110

105111
## Save and execute search templates
@@ -118,7 +124,7 @@ POST _scripts/play_search_template
118124
"size": "{% raw %}{{size}}{{^size}}10{{/size}}{% endraw %}",
119125
"query": {
120126
"match": {
121-
"play_name": "{{play_name}}"
127+
"play_name": "{% raw %}{{play_name}}{% endraw %}"
122128
}
123129
}
124130
},
@@ -129,8 +135,7 @@ POST _scripts/play_search_template
129135
}
130136
```
131137

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:
134139

135140
```json
136141
GET _search/template
@@ -143,7 +148,9 @@ GET _search/template
143148
}
144149
}
145150
```
146-
#### Sample output
151+
{% include copy-curl.html %}
152+
153+
## Example response
147154

148155
```json
149156
{
@@ -193,31 +200,9 @@ POST _render/template
193200
}
194201
}
195202
```
203+
{% include copy-curl.html %}
196204

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/).
221206

222207
## Advanced parameter conversion with search templates
223208

@@ -231,6 +216,7 @@ Use the section tag in Mustache to represent conditions:
231216
```json
232217
{% raw %}{{#var}}var{{/var}}{% endraw %}
233218
```
219+
{% include copy.html %}
234220

235221
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`.
236222

@@ -250,9 +236,9 @@ GET _search/template
250236
}
251237
}
252238
```
239+
{% include copy-curl.html %}
253240

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`:
256242

257243
```json
258244
GET _search/template
@@ -264,14 +250,16 @@ GET _search/template
264250
}
265251
}
266252
```
253+
{% include copy-curl.html %}
267254

268255
### Loops
269256

270-
You can also use the section tag to implement a for each loop:
257+
You can also use the section tag to implement a for-each loop:
271258

272259
```
273260
{% raw %}{{#var}}{{.}}{{/var}}{% endraw %}
274261
```
262+
{% include copy.html %}
275263

276264
When `var` is an array, the search template iterates through it and creates a `terms` query.
277265

@@ -287,6 +275,7 @@ GET _search/template
287275
}
288276
}
289277
```
278+
{% include copy-curl.html %}
290279

291280
This template is rendered as:
292281

@@ -328,6 +317,7 @@ GET _search/template
328317
}
329318
}
330319
```
320+
{% include copy-curl.html %}
331321

332322
Renders as:
333323

@@ -360,6 +350,7 @@ GET _search/template
360350
}
361351
}
362352
```
353+
{% include copy-curl.html %}
363354

364355
Renders as:
365356

@@ -406,22 +397,27 @@ GET _msearch/template
406397
{"id":"play_search_template","params":{"play_name":"Henry IV"}}
407398
```
408399

400+
For more information, see [Multi-search Template API]({{site.url}}{{site.baseurl}}/api-reference/search-apis/msearch-template/).
401+
409402
## Manage search templates
410403

411404
To list all scripts, run the following command:
412405

413406
```json
414407
GET _cluster/state/metadata?pretty&filter_path=**.stored_scripts
415408
```
409+
{% include copy-curl.html %}
416410

417411
To retrieve a specific search template, run the following command:
418412

419413
```json
420414
GET _scripts/<name_of_search_template>
421415
```
416+
{% include copy-curl.html %}
422417

423418
To delete a search template, run the following command:
424419

425420
```json
426421
DELETE _scripts/<name_of_search_template>
427422
```
423+
{% include copy-curl.html %}

_api-reference/search-apis/msearch-template.md renamed to _api-reference/search-apis/search-template/msearch-template.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
22
layout: default
33
title: Multi-search template
4-
parent: Search APIs
5-
nav_order: 100
4+
parent: Search templates
5+
grand_parent: Search APIs
6+
nav_order: 20
67
redirect_from:
78
- /api-reference/msearch-template/
9+
- /api-reference/search-apis/msearch-template/
810
---
911

1012
# Multi-search template

_api-reference/search-apis/render-template.md renamed to _api-reference/search-apis/search-template/render-template.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
---
22
layout: default
33
title: Render template
4-
parent: Search APIs
5-
nav_order: 82
4+
parent: Search templates
5+
grand_parent: Search APIs
6+
nav_order: 10
67
redirect_from:
78
- /api-reference/render-template/
9+
- /api-reference/search-apis/render-template/
810
---
911

1012
# Render template
1113

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.
1315

1416
## Endpoints
1517

@@ -22,17 +24,17 @@ POST /_render/template/<id>
2224

2325
## Path parameters
2426

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.
2628

27-
| Parameter | Type | Description |
29+
| Parameter | Data type | Description |
2830
| :--- | :--- | :--- |
2931
| `id` | String | The ID of the search template to render. |
3032

3133
## Request body fields
3234

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.
3436

35-
| Parameter | Required | Type | Description |
37+
| Parameter | Required | Data type | Description |
3638
| :--- | :--- | :--- | :--- |
3739
| `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`. |
3840
| `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
5658
}
5759
}
5860
```
61+
{% include copy.html %}
5962

6063
### Render template using template ID
6164

@@ -70,13 +73,13 @@ POST _render/template
7073
}
7174
}
7275
```
73-
{% include copy.html %}
76+
{% include copy-curl.html %}
7477

7578
### Render template using `_source`
7679

7780
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:
7881

79-
```
82+
```json
8083
{
8184
"source": {
8285
"from": "{% raw %}{{from}}{{^from}}0{{/from}}{% endraw %}",
@@ -110,8 +113,4 @@ OpenSearch responds with information about the template's output:
110113
}
111114
}
112115
}
113-
```
114-
115-
116-
117-
116+
```

0 commit comments

Comments
 (0)