- 查询模板
/_search/template 结尾的请求允许使用mustache(胡子) 语言来提前处理查询请求。在查询执行之前会给对应的模板添加对应的参数。
GET _search/template
{
"source" : {
"query": { "match" : { "{{my_field}}" : "{{my_value}}" } },
"size" : "{{my_size}}"
},
"params" : {
"my_field" : "message",
"my_value" : "some message",
"my_size" : 5
}
}
curl -X GET "localhost:9200/_search/template" -H 'Content-Type: application/json' -d'
{
"source" : {
"query": { "match" : { "{{my_field}}" : "{{my_value}}" } },
"size" : "{{my_size}}"
},
"params" : {
"my_field" : "message",
"my_value" : "some message",
"my_size" : 5
}
}
'
对于更多的关于mustache 模板和能做什么样的模板请参考:
http://mustache.github.io/mustache.5.html
注意:mustache 语言在Elasticsearch中作为沙箱脚本语言实现。因此它遵守setting,这些设置可以用来启用或者禁用脚本(以type维度进行设置)。设置更详细的描述参考
2.模板例子
2.1例子:单个查询字符串
GET /_search/template
{
"source": {
"query": {
"term": {
"waybillNo": "{{query_string}}"
}
}
},
"params": {
"query_string": "M00003277240002"
}
}
执行查询后他会寻找当前cluster下所有的index找waybillNo满足query_string 字符串的数据。
2.2例子:转化参数为json
GET _search/template
{
"source": "{\"query\":{\"bool\":{\"must\": {{#toJson}}clauses{{/toJson}} }}}",
"params": {
"clauses": [
{ "term": { "waybillNo": "M00003277240002"} },
{ "term": { "hostAddress": "192.168.207.55" } }
]
}
}
类似于执行
{
"query" : {
"bool" : {
"must" : [
{
"term" : {
"waybillNo": "M00003277240002"
}
},
{
"term" : {
"hostAddress": "192.168.207.55"
}
}
]
}
}
}
2.3例子:连接数组
GET _search/template
{
"source": {
"query": {
"range": {
"createTime": {
"gte" : "{{date.min}}",
"lte" : "{{date.max}}",
"format": "{{#join delimiter='||'}}date.formats{{/join delimiter='||'}}"
}
}
}
},
"params": {
"date": {
"min": "2019-06-19 16:00:00",
"max": "2019-06-19 23:59:59",
"formats": ["yyyy-MM-dd HH:MM:SS"]
}
}
}
GET _render/template
{
"source": {
"query": {
"terms": { "waybillNo": ["{{#join}}waybillNo{{/join}}"] }
}
},
"params": {
"waybillNo": [ "M00003277240001" ,"M00003277240002"]
}
}
结果:
{
"template_output": {
"query": {
"terms": {
"waybillNo": [
"M00003277240001,M00003277240002"
]
}
}
}
}
2.4例子:缺省值
GET _search/template
{
"source": {
"query": {
"range": {
"lastLong": {
"gte": "{{start}}",
"lte": "{{end}}{{^end}}20{{/end}}"
}
}
}
},
"params": { "start":20,"end":1000}
}
GET _render/template
{
"source": {
"query": {
"range": {
"lastLong": {
"gte": "{{start}}",
"lte": "{{end}}{{^end}}20{{/end}}"
}
}
}
},
"params": { "start":20}
}
结果:
{
"template_output": {
"query": {
"range": {
"lastLong": {
"gte": "20",
"lte": "20"
}
}
}
}
}
2.5例子:条件语句
(这块描述的不是很清晰)
条件从句不能以JSON的形式进行表达。模板应该作为string进行传递。举个例子,执行一个match查询在line字段上面同时加入可选的过滤条件line no.使用start和end变量。
那么这个参数,他们都是可选的:
{
"params": {
"text": "words to search for",
"line_no": {
"start": 10,
"end": 20
}
}
}
我们写的查询:
{
"query": {
"bool": {
"must": {
"match": {
"line": "{{text}}"
}
},
"filter": {
{{#line_no}}
"range": {
"line_no": {
{{#start}}
"gte": "{{start}}"
{{#end}},{{/end}}
{{/start}}
{{#end}}
"lte": "{{end}}"
{{/end}}
}
}
{{/line_no}}
}
}
}
}
填充参数 text | |
包含range查询仅在line_no被赋值 | |
包含gte语句仅在 line_no.start被赋值 | |
赋值参数line_no.start | |
在gte语句后加逗号,仅在line_no.start 和 line_no.end 都存在 | |
包含lte语句仅在line_no.end被赋值 | |
赋值参数 line_no.end |
注意:如上面所写。这个template不是合格的JSON因为它包含段标签如{{#line_no}}。因为这样的原因模板应该呗保存在文件里或者通过rest Api来使用,应该写成string:
"source": "{\"query\":{\"bool\":{\"must\":{\"match\":{\"line\":\"{{text}}\"}},\"filter\":{{{#line_no}}\"range\":{\"line_no\":{{{#start}}\"gte\":\"{{start}}\"{{#end}},{{/end}}{{/start}}{{#end}}\"lte\":\"{{end}}\"{{/end}}}}{{/line_no}}}}}}"
2.6例子:提前注册的模板
可以使用保存脚本的API注册查询模板
POST _scripts/<templatename>
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match": {
"title": "{{query_string}}"
}
}
}
}
}
获取模板
GET _scripts/<templatename>
删除模板
DELETE _scripts/<templatename>
使用一个保存的模板
GET _search/template
{
"id": "<templateName>",
"params": {
"query_string": "search for these words"
}
}
2.7例子:验证模板
模板可以和它的参数一起被渲染返回
GET _render/template
{
"source": "{ \"query\": { \"terms\": {{#toJson}}statuses{{/toJson}} }}",
"params": {
"statuses" : {
"status": [ "pending", "published" ]
}
}
}
2.8例子:模板解释
使用explain参数来执行一个模板
GET _search/template
{
"source": {
"query": {
"range": {
"createTime": {
"gte" : "{{date.min}}",
"lte" : "{{date.max}}",
"format": "{{#join delimiter='||'}}date.formats{{/join delimiter='||'}}"
}
}
}
},
"params": {
"date": {
"min": "2019-06-19 16:00:00",
"max": "2019-06-19 23:59:59",
"formats": ["yyyy-MM-dd HH:MM:SS"]
}
},
"explain": true
}
特殊信息:
"_explanation": {
"value": 1,
"description": "createTime:[2019-06-19 16:00:00 TO 2019-06-19 23:59:59]",
"details": []
}
2.9例子:轮廓
当执行模板查询的时候可使用profile参数
GET _search/template
{
"id": "my_template",
"params": {
"status": [ "pending", "published" ]
},
"profile": true
}