将字段重命名为Elasticsearch中的新索引 [英] Renaming fields to new index in Elasticsearch

查看:1724
本文介绍了将字段重命名为Elasticsearch中的新索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在新索引中,我要将title复制到title1和title2身体到body1和body2(忽略其他),并将类型从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,
分析器:my_analyzer1
},
body2:{
type:text,
analyzer:my_analyzer2
}
}
}
}'

此答案 Elasticsearch 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]解析字段[脚本]


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


即使我可以整理出多个语句的问题,放在第二行给我错误


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


任何人都可以帮我吗?似乎这不应该是不可能的。

解决方案


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


您不需要将所有inline语句放在双引号中您可以将分号分隔的所有内联脚本语句(; )并以双引号括起来()作为如下所示:

 script:{
inline:ctx._source.title1 = ctx._source 。标题; ctx._source.title2 = ctx._source.remove(\title\); ctx._source.body1 = ctx._source.body; ctx._source.body2 = ctx._source.remove(\body\); ctx._type = \articles_eng\
}
/ pre>


即使我可以整理出多个语句的问题,将
放在第二行给我错误


您尝试以错误的方式访问源字段元数据字段(如 _id,_type,_index应该以 ctx._type / ctx._id 作为来源访问应该以 ctx._source.title / <$的形式访问字段(如 title,body,other ) c $ c> ctx._source.body 。



所以最后,你的ReIndex查询应该是这样的:

  POST _reindex 
{
source:{
index:origindex,
_source:[title,body]
},
dest:{
index:newindex
},
script :{
inline:ctx._source.title1 = ctx._sour ce.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\
}
}

希望这有帮助!


I have an index with this mapping:

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

In a new index, I want to copy "title" to "title1" and "title2", and "body" to "body1" and "body2" (disregarding "other"), and change the type from "page" to "articles_eng". The new index has this mapping:

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

From looking at this answer and the Elasticsearch reindex docs I come up with something like this:

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

I'm having trouble with the script lines. 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

"[reindex] failed to parse field [script]"

caused by

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

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

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

Can anyone help me out? It seems like this shouldn't be impossible to do.

解决方案

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

You don't need to put all inline statement in double quotes instead you can put all inline script statements seperated by semi-colon(;) and enclosed in double quotes(") as shown below:

"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

You are trying to access source fields in wrong way. Metadata fields(like _id, _type, _index ..) should be accessed as ctx._type / ctx._id where as source fields(like title, body, other in your case) should be accessed as ctx._source.title/ ctx._source.body .

So finally, your ReIndex query should look like this:

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

Hope this helps!

这篇关于将字段重命名为Elasticsearch中的新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆