行动是治愈恐惧的良药,而犹豫、拖延将不断滋养恐惧。

search-template通过文件加载mustache-script的问题

Elasticsearch | 作者 vergilyn | 发布于2019年07月22日 | 阅读数:2085

原因:
  因为需要动态拼接一些条件和使用toJson,如果传递json-string不是很方便阅读和修改。
  官方文档中说可以通过file来加载mustache-script。
 
这是6.4的文档中的说明,但是在 6.4的文档 Pre-registered template 中并未提供file的写法。
TIM图片20190722105039.png

 
然后看到5.6的文档中存在 5.6 Pre-registered template
TIM图片20190722105303.png

 
但是我在6.4中执行错误,错误信息:
GET _search/template
{
"file": "search_template",
"params": {
.....
}
}

{
"error": {
"root_cause": [
{
"type": "x_content_parse_exception",
"reason": "[2:3] [search_template] unknown field [file], parser not found"
}
],
"type": "x_content_parse_exception",
"reason": "[2:3] [search_template] unknown field [file], parser not found"
},
"status": 400
}
1. search_template 确定保存在 es/config/scripts下,扩展名`.mustache`
2. 请求中`file` 加不加扩展名测试过,一样的错误。
 
难道是6.0+取消了这种写法?
 
已邀请:

bellengao - 博客: https://www.jianshu.com/u/e0088e3e2127

赞同来自:

6.0取消了

black_manba - 哈哈哈

赞同来自:

请问楼主,你现在使用哪种方法解决这个问题的?同样遇到这个问题。

vergilyn

赞同来自:

ES v6.4 search-template 
我现在是通过kibana在es创建scripts
```
POST _scripts/<templata-name>
{
  "script": {
    "lang": "mustache",
    "source": ...,  // 标准query DSL语句,或者 query-dsl 对应的json-string
  }
}
```

然后查询的时候通过 template-name查询
```
# id/source 任选其一
GET _render/template
{
  "id": "<template-name>",
  "params": {...}
}
```

要回复问题请先登录注册