04.search_template
文章目錄
- 1. Search Template簡介
- 2. 樣例Examples
- 1. 存儲一個(gè)template Store a search template
1. Search Template簡介
/_search/template api 允許使用mustache(小胡子)語言在執(zhí)行搜索請求之前預(yù)渲染它們,并使用模板參數(shù)填充現(xiàn)有模板。
The /_search/template endpoint allows to use the mustache language to pre render search requests, before they are executed and fill existing templates with template parameters.
GET _search/template {"source" : {"query": { "match" : { "{{my_field}}" : "{{my_value}}" } },"size" : "{{my_size}}"},"params" : {"my_field" : "message","my_value" : "some message","my_size" : 5} }For more information on how Mustache templating and what kind of templating you can do with it check out the online documentation of the mustache project.
The mustache language is implemented in Elasticsearch as a sandboxed scripting language, hence it obeys settings that may be used to enable or disable scripts per type and context as described in the scripting docs
2. 樣例Examples
1. 存儲一個(gè)template Store a search template
You can store a search template using the stored scripts API.
POST _scripts/<templateid> {"script": {"lang": "mustache","source": {"query": {"match": {"title": "{{query_string}}"}}}} }This template can be retrieved by
獲取template
GET _scripts/<templateid>返回{"script" : {"lang" : "mustache","source" : "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}","options": {"content_type" : "application/json; charset=UTF-8"}},"_id": "<templateid>","found": true }This template can be deleted by
刪除template
DELETE _scripts/<templateid>使用存儲的template來進(jìn)行搜索
GET _search/template {"id": "<templateid>", "params": {"query_string": "search for these words"} }id是stored template的script name
校驗(yàn)一個(gè)search template
Validate a search template
A template can be rendered in a response with given parameters using
GET _render/template {"source": "{ \"query\": { \"terms\": {{#toJson}}statuses{{/toJson}} }}","params": {"statuses" : {"status": [ "pending", "published" ]}} }This call will return the rendered template:
返回渲染后的template
status array has been populated with values from the params object.
Stored templates can also be rendered using
也可以使用store的template進(jìn)行渲染
GET _render/template/<template_name> {"params": {"..."} }Explain
使用explain參數(shù)來看渲染的詳情
Profiling
You can use profile parameter when running a template:
使用profile參數(shù)來進(jìn)行性能分析
Filling in a query string with a single value
使用單個(gè)value來填充query string
GET _search/template {"source": {"query": {"term": {"message": "{{query_string}}"}}},"params": {"query_string": "search for these words"} }將參數(shù)轉(zhuǎn)成json
Converting parameters to JSON
The {{#toJson}}parameter{{/toJson}} function can be used to convert parameters like maps and array to their JSON representation:
which is rendered as:
{"query": {"terms": {"status": ["pending","published"]}} }A more complex example substitutes an array of JSON objects:
GET _search/template {"source": "{\"query\":{\"bool\":{\"must\": {{#toJson}}clauses{{/toJson}} }}}","params": {"clauses": [{ "term": { "user" : "foo" } },{ "term": { "user" : "bar" } }]} }which is rendered as:
{"query" : {"bool" : {"must" : [{"term" : {"user" : "foo"}},{"term" : {"user" : "bar"}}]}} }連接數(shù)組中的元素
Concatenating array of values
The {{#join}}array{{/join}} function can be used to concatenate the values of an array as a comma delimited string:
which is rendered as:
{"query" : {"match" : {"emails" : "username@email.com,lastname@email.com"}} }默認(rèn)使用逗號進(jìn)行連接",", 也可以自定義連接符
The function also accepts a custom delimiter:
which is rendered as:
{"query" : {"range" : {"born" : {"gte" : "2016","lte" : "31/12/2017","format" : "dd/MM/yyyy||yyyy"}}} }默認(rèn)值
Default values
default value is written as {{var}}{{^var}}default{{/var}} for instance:
When params is { “start”: 10, “end”: 15 } this query would be rendered as:
{"range": {"line_no": {"gte": "10","lte": "15"}} }But when params is { “start”: 10 } this query would use the default value for end:
{"range": {"line_no": {"gte": "10","lte": "20"}} }條件語句
Conditional clauses
條件子句不能使用模板的JSON形式表示。而是必須將模板作為字符串傳遞。例如,假設(shè)我們要在行字段上運(yùn)行匹配查詢,并希望按行號進(jìn)行過濾,其中開始和結(jié)束是可選的。
Conditional clauses cannot be expressed using the JSON form of the template. Instead, the template must be passed as a string. For instance, let’s say we wanted to run a match query on the line field, and optionally wanted to filter by line numbers, where start and end are optional.
The params would look like:
{"params": {"text": "words to search for","line_no": { "start": 10,"end": 20}} }The line_no, start, and end parameters are optional.
We could write the query as:
{"query": {"bool": {"must": {"match": {"line": "{{text}}" 1}},"filter": {{{#line_no}} 2"range": {"line_no": {{{#start}} 3"gte": "{{start}}" 4{{#end}},{{/end}} 5{{/start}}{{#end}} 6"lte": "{{end}}" 7{{/end}}}}{{/line_no}}}}} }As written above, this template is not valid JSON because it includes the section markers like {{#line_no}}. For this reason, the template should either be stored in a file (see Store a search template) or, when used via the REST API, should be written as a 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}}}}}}"使用url編碼
Encoding URLs
{{#url}}value{{/url}} 功能可以用來將參數(shù)進(jìn)行url編碼
The {{#url}}value{{/url}} function can be used to encode a string value in a HTML encoding form as defined in by the HTML specification.
As an example, it is useful to encode a URL:
GET _render/template {"source" : {"query" : {"term": {"http_access_log": "{{#url}}{{host}}/{{page}}{{/url}}"}}},"params": {"host": "https://www.elastic.co/","page": "learn"} }The previous query will be rendered as:{"template_output" : {"query" : {"term" : {"http_access_log" : "https%3A%2F%2Fwww.elastic.co%2F%2Flearn"}}} }總結(jié)
以上是生活随笔為你收集整理的04.search_template的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 03.body_search
- 下一篇: 05.multi_search_temp