Elasticsearch 6.* 升级 7.* 历险记
具体情况是这样的
跟着课程里面学习 EleasticSearch 版本是 6.*, 但是自己服务器本着新就是生产力的原则(手贱)安装了最新的(截止目前) 7.2 版本,导致满心欢喜执行创建索引操作时,一堆 error、warning(流下了悔恨的泪水),这里记录下如何解决开发 & 线上 ES 版本差异导致的代码不兼容问题
首先通过以下的 warning 信息找到了和自己遇到了同样人生难题的难友们, 不过注意到这里其实是安装 kibana 遇到的,我是创建索引。具体 GitHub 地址在这里
https://github.com/elastic/kibana/issues/3...
[illegal_argument_exception] The mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true.
通过自己丰富的知识从回答中遍历到了一个可能的正确答案(主要是回答的人明显带有 author 标签,大概不会骗人吧),就是如下这个
Either the template needs to be changed to remove the mapping type _doc or the command needs to include ?include_type_name=true, preferably the latter to be forward-compatible.
大意就是 ES 7. 中要要么移除 doc type 或者在使用的时候需要加上 ?includetypename=true, 因为 ?include_type_name=true 这一项在 ES 6. 和 ES 7. 的默认设置不一样。并且建议采用后一种方式(也就是加上 include_type_name=true),因为是向前兼容的(就是 ES 6. 和 7.* 都可以正常使用)
本着听人劝吃饱饭的原则,我选择了第二种。但问题是自己的索引创建是通过代码创建的,暂时不清楚 include_type_name 需要加到什么地方。通过看 PHP ES Client 相关的代码了解到在创建的时候是可以附加上 parameters 的,就将原有的
'index' => $aliasName.'_0',
'body' => [
// 调用索引类的 getSettings() 方法获取索引设置
'settings' => $indexClass::getSettings(),
'mappings' => [
'_doc' => [
// 调用索引类的 getProperties() 方法获取索引字段
'properties' => $indexClass::getProperties(),
],
],
'aliases' => [
// 同时创建别名
$aliasName => new \stdClass(),
],
],
改为了
'index' => $aliasName.'_0',
'body' => [
// 调用索引类的 getSettings() 方法获取索引设置
'settings' => $indexClass::getSettings(),
'mappings' => [
'_doc' => [
// 调用索引类的 getProperties() 方法获取索引字段
'properties' => $indexClass::getProperties(),
],
],
'aliases' => [
// 同时创建别名
$aliasName => new \stdClass(),
],
],
'include_type_name' => true
再次执行,成功了(流下了激动的眼泪水)~
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: