环境
ubuntu16、jdk1.8、Elasticsearch 7.5.1
背景
使用"upsert"更新一个可能不存在的文档(/website/pageviews/10-->索引:website,类型:pageviews,ID:10):
curl -H "Content-Type:application/json" -XPOST 'http://localhost:9200/website/pageviews/10/_update/?pretty' -d '
{
"script":"ctx._source.views+=1",
"upsert":{
"views":1
}
}'
错误
"type" : "illegal_argument_exception",
"reason" : "Rejecting mapping update to [website] as the final mapping would have more than 1 type: [blog, pageviews]"
原因
依据"reason"明显 看出:出错原因是因为索引website下有两个类型blog、pageviews。因为原本索引website下已经有类型blog,而上述"upsert"语句会在文档不存在时新建该ID=10的文档,其类型为pageviews。ES6.0以上不再允许一个索引包含多于一种类型。