翻译-Elasticsearch Search Tempalte

  1. 查询模板

/_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维度进行设置)。设置更详细的描述参考

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/modules-scripting-security.html#allowed-script-types-setting

2.模板例子

2.1例子:单个查询字符串

GET /_search/template

{

  "source": {

    "query": {

      "term": {

        "waybillNo": "{{query_string}}"

      }

    }

  },

  "params": {

    "query_string": "M00003277240002"

  }

}

执行查询后他会寻找当前cluster下所有的indexwaybillNo满足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": { https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/1.png

            "start": 10, https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/2.png

            "end":   20  https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/3.png

        }

    }

}

我们写的查询:

{

  "query": {

    "bool": {

      "must": {

        "match": {

          "line": "{{text}}" https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/1.png

        }

      },

      "filter": {

        {{#line_no}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/2.png

          "range": {

            "line_no": {

              {{#start}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/3.png

                "gte": "{{start}}" https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/4.png

                {{#end}},{{/end}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/5.png

              {{/start}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/6.png

              {{#end}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/7.png

                "lte": "{{end}}" https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/8.png

              {{/end}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/9.png

            }

          }

        {{/line_no}} https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/10.png

      }

    }

  }

}

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/1.png

填充参数 text

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/2.png https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/10.png

包含range查询仅在line_no被赋值

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/3.png https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/6.png

包含gte语句仅在 line_no.start被赋值

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/4.png

赋值参数line_no.start

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/5.png

在gte语句后加逗号,仅在line_no.start 和 line_no.end 都存在

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/7.png https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/9.png

包含lte语句仅在line_no.end被赋值

https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/8.png

赋值参数 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>", https://www.elastic.co/guide/en/elasticsearch/reference/6.2/images/icons/callouts/1.png

    "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

}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值