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



我有一个索引的索引:

curl -XPUT 'http://localhost:9200/origindex/_mapping/page' -d '
   {
    "page" : {
        "properties" : {
            "title" : {"type" : "text"},
            "body" : {"type" : "text"},
            "other": {"type": "text"}
        }
     }
   }'

在新索引中,我想将"标题"复制为" title1"one_answers" title2"," body"到" body1"one_answers" body2"(无视"其他"),然后从"页面"更改类型" to" 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 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"                                                                                                   
    }                                                                                              
}'

我在脚本行上遇到了麻烦。如果我只做顶行(更改文档类型),则一切正常。如果添加其余的行,我会收到一个错误

" [REINDEX]无法解析字段[脚本]

引起

"意外字符(';'(代码59)):期望逗号分开 对象条目 n在[源: org.elasticsearch.transport.netty4.bytebufstreaminput@37649463;线: 14,列:50]"

即使我可以用多个语句解决问题,仅列入第二行才会给我错误

"无效字段添加到上下文[title1]}]

有人可以帮我吗?似乎这不可能做。

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

您不需要将所有内联语句放入双引号中,而是可以将所有内联脚本语句分为semi-colon(;)分开,并以双引号(")封装,如下所示:

"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访问。

因此,最后,您的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""
  }
}

希望这会有所帮助!

最新更新