日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

04.search_template

發(fā)布時(shí)間:2024/2/28 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 04.search_template 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 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

{"template_output": {"query": {"terms": {"status": [ "pending","published"]}}} }

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ù)來看渲染的詳情

GET _search/template {"id": "my_template","params": {"status": [ "pending", "published" ]},"explain": true }

Profiling
You can use profile parameter when running a template:
使用profile參數(shù)來進(jìn)行性能分析

GET _search/template {"id": "my_template","params": {"status": [ "pending", "published" ]},"profile": true }

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:

GET _search/template {"source": "{ \"query\": { \"terms\": {{#toJson}}statuses{{/toJson}} }}","params": {"statuses" : {"status": [ "pending", "published" ]}} }

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:

GET _search/template {"source": {"query": {"match": {"emails": "{{#join}}emails{{/join}}"}}},"params": {"emails": [ "username@email.com", "lastname@email.com" ]} }

which is rendered as:

{"query" : {"match" : {"emails" : "username@email.com,lastname@email.com"}} }

默認(rèn)使用逗號進(jìn)行連接",", 也可以自定義連接符
The function also accepts a custom delimiter:

GET _search/template {"source": {"query": {"range": {"born": {"gte" : "{{date.min}}","lte" : "{{date.max}}","format": "{{#join delimiter='||'}}date.formats{{/join delimiter='||'}}"}}}},"params": {"date": {"min": "2016","max": "31/12/2017","formats": ["dd/MM/yyyy", "yyyy"]}} }

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:

{"source": {"query": {"range": {"line_no": {"gte": "{{start}}","lte": "{{end}}{{^end}}20{{/end}}"}}}},"params": { ... } }

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}}}}} }
  • 填充param text, Fill in the value of param text
  • 檢測到line_no參數(shù)則包含range filter, Include the range filter only if line_no is specified
  • 檢測到line_no.start 則包含gte 條件, Include the gte clause only if line_no.start is specified
  • 填充line_no.start 的value, Fill in the value of param line_no.start
  • 在檢測到line_no.start AND line_no.end的時(shí)候填充一個(gè)逗號",", Add a comma after the gte clause only if line_no.start AND line_no.end are specified
  • 檢測到line_no.end 的時(shí)候包含lte條件,Include the lte clause only if line_no.end is specified
  • 填充line_no.end的value, Fill in the value of param line_no.end
  • 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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。