ES使用脚本进行局部更新的排错记录

初学Elasticsearch,在按照《Elasticsearch服务器开发(第2版)》进行学习的过程当中,在P17页中1.4.5 更新文档小节,使用脚本对文档进行局部更新的时候遇到了以下报错:html

  ~ curl -XPOST http://127.0.0.1:9200/blog/article/1/_update -d '{"script": "ctx._source.content=\"new content\""}'
{
	"error":{
		"root_cause":[{
			"type":"remote_transport_exception",
			"reason":"[Lady Mandarin][127.0.0.1:9300][indices:data/write/update[s]]"
			}
		],
		"type":"illegal_argument_exception",
		"reason":"failed to execute script",
		"caused_by":{
			"type":"script_exception",
			"reason":"scripts of type [inline], operation [update] and lang [groovy] are disabled"}},
			"status":400
}%

我根据提示中的缘由"scripts of type [inline], operation [update] and lang [groovy] are disabled"进行了查询,查到了官网文档关于对脚本更新的介绍(介绍连接)。bash

经过粗略查看文档,我发现要解决这个问题,须要在Elasticsearch的配置文件elasticsearch.yml中添加以下配置:服务器

script.engine.groovy.inline.update: oncurl

因为Elasticsearch默认使用的是Groovy语言。Groovy语言一个快速且功能丰富的脚本语言,语法相似于Javascript。它在一个沙盒(sandbox)中运行,以防止恶意用户毁坏Elasticsearch或***服务器。
因为默认Elasticsearch没有打开Groovy的update权限,所以咱们没法经过Groovy进行更新操做,经过上述配置打开权限以后,就能够进行更新操做了。elasticsearch

 ~ curl -XPOST http://127.0.0.1:9200/blog/article/1/_update -d '{"script": "ctx._source.content=\"new content\""}'
{
	"_index":"blog",
	"_type":"article",
	"_id":"1",
	"_version":2,
	"_shards":{
		"total":2,
		"successful":1,
		"failed":0
	}
}%
相关文章
相关标签/搜索