gpt4 book ai didi

elasticsearch - 在 Elasticsearch 中将字段重命名为新索引

转载 作者:行者123 更新时间:2023-11-29 02:52:23 30 4
gpt4 key购买 nike

我有这个映射的索引:

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"
}
}
}
}'

从看this answerElasticsearch reindex docs我想出了这样的事情:

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] failed to parse field [script]"

引起

"Unexpected character (';' (code 59)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@37649463; line: 14, column: 50]"

即使我可以解决多个语句的问题,只输入第二行也会出现错误

"Invalid fields added to context [title1]"}]

谁能帮帮我?看起来这应该不是不可能的。

最佳答案

If I do only the top line (changing the document type), everything works fine. If I add the rest of the lines, I get an error

您不需要将所有内联语句都放在双引号中,而是可以将所有内联脚本语句用分号 (;) 分隔并放在双引号 (") 如下图:

"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\""
}

Even if I can sort out the issue with the multiple statements, putting in just the second line gives me the error

您正在尝试以错误的方式访问源字段。元数据字段(如 _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\""
}
}

希望这对您有所帮助!

关于elasticsearch - 在 Elasticsearch 中将字段重命名为新索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42423899/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com