将字段重命名为 Elasticsearch 中的新索引

ksl*_*net 5 elasticsearch

我有一个带有此映射的索引:

curl -XPUT 'http://localhost:9200/origindex/_mapping/page' -d '
   {
    "page" : {
        "properties" : {
            "title" : {"type" : "text"},
            "body" : {"type" : "text"},
            "other": {"type": "text"}
        }
     }
   }'
Run Code Online (Sandbox Code Playgroud)

在新索引中,我想将“title”复制到“title1”和“title2”,将“body”复制到“body1”和“body2”(不考虑“other”),并将类型从“page”更改为“文章_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" 
            }                                                   
        }                                                                                      
    }                                                                                          
}'                                                                                              
Run Code Online (Sandbox Code Playgroud)

通过查看这个答案Elasticsearch reindex 文档,我想出了这样的事情:

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"                                                                                                   
    }                                                                                              
}'
Run Code Online (Sandbox Code Playgroud)

我的脚本行有问题。如果我只做第一行(更改文档类型),一切正常。如果我添加其余的行,我会收到一个错误

“[reindex] 未能解析字段 [脚本]”

造成的

“意外字符(';'(代码 59)):期望逗号分隔对象条目\n [来源:org.elasticsearch.transport.netty4.ByteBufStreamInput@37649463;行:14,列:50]”

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

“添加到上下文 [title1] 中的无效字段”}]

谁能帮我吗?这似乎不是不可能做到的。

avr*_*avr 5

如果我只做第一行(更改文档类型),一切正常。如果我添加其余的行,我会收到一个错误

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

"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\""
}
Run Code Online (Sandbox Code Playgroud)

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

您正试图以错误的方式访问源字段。元数据字段(如_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\""
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!