在朱林编著的《elasticsearch技术解析与实战》一书中第164页至166页----搜索模版这一部分,书中例举了三种使用模板的方式,在这里要着重提一下最后一种,书中原文:可以使用Mustache语言作为搜索请求的预处理,它提供了模板,然后通过键值对来替换模板中的变量。把脚本存储在本地磁盘中,默认的位置为:elasticsearch\config\scripts,通过引用脚本名称进行使用。
紧接着说:在操作前,先要把脚本存储在文件中,文件名为template_test.mustache,文件中的内容为:
{我把template_test.mustache放到了elasticsearch\config\scripts文件夹下面后,书中又说elasticsearch对文件读取有个时间,刚建好后,不能生效,做验证的时候可以重启进行生效。于是我重启了后跟着书上的例子做验证:"template": { "query": { "match": { "eventName": "{{query_string}}" } } } }
请求:POST http://127.0.0.1:9200/secisland/_search/
{
"query": {
"template": {
"id": "template_test",
"params": {
"query_string": "linux"
}
}
}
}
结果报错了:{
- "status": 400
请求:POST http://127.0.0.1:9200/secisland/_search/
{
"query": {
"template": {
"file": "template_test",
"params": {
"query_string": "linux"
}
}
}
}
这本书几乎全部是在浏览器的head插件(类似于postman)中进行操作的,只有两三页介绍Java API;而且翻译的痕迹非常明显,实战性不强,书中错误挺多的。