我有此映射的索引:
curl -XPUT 'http://localhost:9200/origindex/_mapping/page' -d ' { "page" : { "properties" : { "title" : {"type" : "text"}, "body" : {"type" : "text"}, "other": {"type": "text"} } } }'
在新索引中,我想将“ title”复制到“ title1”和“ title2”,将“ body”复制到“ body1”和“ body2”(不考虑“ other”),并将类型从“ page”更改为“ Articles_eng”。新索引具有以下映射:
curl -XPUT 'http://localhost:9200/newindex/_mapping/articles_eng' -d ' { "articles_eng" : { "properties" : { "title1" : { "type" : "text", "analyzer" : "my_analyzer1" }, "title2" : { "type" : "text", "analyzer": "my_analyzer2" }, "body1": { "type" : "text", "analyzer": "my_analyzer1" }, "body2" : { "type" : "text", "analyzer": "my_analyzer2" } } } }'
通过查看此答案和Elasticsearch重新索引文档,我得出了类似以下内容:
curl -XPOST http://localhost:9200/_reindex -d '{ "source": { "index": "origindex", "type": "page", "query": { "match_all": {} }, "_source": [ "title", "body" ] }, "dest": { "index": "newindex" }, "script": { "inline": "ctx._type = \"articles_eng\""; "ctx._title1 = ctx._source._title"; "ctx._title2 = ctx._source._title"; "ctx._body1 = ctx._source._body"; "ctx._body2 = ctx._source._body" } }'
我在脚本行上遇到了麻烦。如果仅执行第一行(更改文档类型),则一切正常。如果我添加其余的行,则会出现错误
“ [reindex]无法解析字段[脚本]”
由…引起
“意外的字符(’;’(代码59)):期望逗号分隔[输入源:org.elasticsearch.transport.netty4.ByteBufStreamInput@37649463;第14行,第50列]中的对象项\ n”
即使我可以使用多条语句来解决问题,也只需在第二行中输入错误即可
“无效的字段已添加到上下文[title1]”}]
谁能帮我吗?看来这不是不可能做到的。
如果仅执行第一行(更改文档类型),则一切正常。如果我添加其余的行,则会出现错误
您不需要将所有内联语句都放在双引号中,而是可以将所有内联脚本语句都用分号(;)分隔并用双引号(")括起来,如下所示:
;
"
"script": { "inline": "ctx._source.title1 = ctx._source.title; ctx._source.title2 = ctx._source.remove(\"title\");ctx._source.body1 = ctx._source.body; ctx._source.body2 = ctx._source.remove(\"body\");ctx._type=\"articles_eng\"" }
您试图以错误的方式访问源字段。元数据字段(如_id, _type, _index ..)应作为ctx._type/ 访问ctx._id,而源字段(如title, body, other您的情况)应作为ctx._source.title/ 访问ctx._source.body。
_id, _type, _index ..
ctx._type
ctx._id
title, body, other
ctx._source.title
ctx._source.body
因此,最后,您的ReIndex查询应如下所示:
POST _reindex { "source": { "index": "origindex", "_source": [ "title", "body" ] }, "dest": { "index": "newindex" }, "script": { "inline": "ctx._source.title1 = ctx._source.title; ctx._source.title2 = ctx._source.remove(\"title\");ctx._source.body1 = ctx._source.body; ctx._source.body2 = ctx._source.remove(\"body\");ctx._type=\"articles_eng\"" } }
希望这可以帮助!