Skip to content

Commit 4820d49

Browse files
committed
Mustache: Add util functions to render JSON and join array values
This pull request adds two util functions to the Mustache templating engine: - {{#toJson}}my_map{{/toJson}} to render a Map parameter as a JSON string - {{#join}}my_iterable{{/join}} to render any iterable (including arrays) as a comma separated list of values like `1, 2, 3`. It's also possible de change the default delimiter (comma) to something else. closes #18970
1 parent b97ea99 commit 4820d49

File tree

7 files changed

+635
-116
lines changed

7 files changed

+635
-116
lines changed

docs/reference/search/search-template.asciidoc

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,89 @@ which is rendered as:
8989
}
9090
------------------------------------------
9191

92+
93+
[float]
94+
===== Concatenating array of values
95+
96+
The `{{#join}}array{{/join}}` function can be used to concatenate the
97+
values of an array as a comma delimited string:
98+
99+
[source,js]
100+
------------------------------------------
101+
GET /_search/template
102+
{
103+
"inline": {
104+
"query": {
105+
"match": {
106+
"emails": "{{#join}}emails{{/join}}"
107+
}
108+
}
109+
},
110+
"params": {
111+
112+
}
113+
}
114+
------------------------------------------
115+
116+
which is rendered as:
117+
118+
[source,js]
119+
------------------------------------------
120+
{
121+
"query" : {
122+
"match" : {
123+
124+
}
125+
}
126+
}
127+
------------------------------------------
128+
129+
The function also accepts a custom delimiter:
130+
131+
[source,js]
132+
------------------------------------------
133+
GET /_search/template
134+
{
135+
"inline": {
136+
"query": {
137+
"range": {
138+
"born": {
139+
"gte" : "{{date.min}}",
140+
"lte" : "{{date.max}}",
141+
"format": "{{#join delimiter='||'}}date.formats{{/join delimiter='||'}}"
142+
}
143+
}
144+
}
145+
},
146+
"params": {
147+
"date": {
148+
"min": "2016",
149+
"max": "31/12/2017",
150+
"formats": ["dd/MM/yyyy", "yyyy"]
151+
}
152+
}
153+
}
154+
------------------------------------------
155+
156+
which is rendered as:
157+
158+
[source,js]
159+
------------------------------------------
160+
{
161+
"query" : {
162+
"range" : {
163+
"born" : {
164+
"gte" : "2016",
165+
"lte" : "31/12/2017",
166+
"format" : "dd/MM/yyyy||yyyy"
167+
}
168+
}
169+
}
170+
}
171+
172+
------------------------------------------
173+
174+
92175
[float]
93176
===== Default values
94177

@@ -140,6 +223,46 @@ for `end`:
140223
}
141224
------------------------------------------
142225

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+
265+
143266
[float]
144267
===== Conditional clauses
145268

0 commit comments

Comments
 (0)