2016-11-15 117 views
5

我有这样重命名字段

{ 
    "_index": "testindex", 
    "_type": "logs", 
    "_id": "1", 
    "_score": 1, 
    "_source": { 
     "field1": "data1", 
     "field2": "data2" 
    } 
} 

文档我需要改变field2Request.field3

{ 
    "_index": "testindex", 
    "_type": "logs", 
    "_id": "1", 
    "_score": 1, 
    "_source": { 
     "field1": "data1", 
     "Request": { 
     "field3": "data2" 
     } 
    } 
} 

为此,首先添加一个字段映射到现存的索引

PUT testindex/_mapping/logs 
{ 
    "properties": 
    { 
     "Request": 
     { 
      "properties": 
      { 
       "field3" : 
       { 
        "type": "string" 
       } 
      } 
     } 
    } 
} 

然后尝试重新索引

POST _reindex 
{ 
    "source": { 
     "index": "testindex" 
    }, 
    "dest": { 
     "index": "testindex1" 
    }, 
    "script": { 
     "inline": "ctx._source.Request.field3 = ctx._source.remove(\"field2\")" 
    } 
} 

错误是

"reason": "failed to run inline script [ctx._source.Request.field3 = ctx._source.remove(\"field2\")] using lang [groovy]", 
"caused_by": { 
    "type": "null_pointer_exception", 
    "reason": "Cannot set property 'field3' on null object" 
} 
+0

谢谢@ saeed-zhiany格式化.. –

回答

13

Request领域还没有在你的文件存在,所以你的脚本需要先创建它:

POST _reindex 
{ 
    "source": { 
     "index": "testindex" 
    }, 
    "dest": { 
     "index": "testindex1" 
    }, 
    "script": { 
     "inline": "ctx._source.Request = [:]; ctx._source.Request.field3 = ctx._source.remove(\"field2\") ]" 
    } 
} 

还是有点短这样的:

POST _reindex 
{ 
    "source": { 
     "index": "testindex" 
    }, 
    "dest": { 
     "index": "testindex1" 
    }, 
    "script": { 
     "inline": "ctx._source.Request = [field3: ctx._source.remove(\"field2\") ]" 
    } 
} 
+0

谢谢瓦尔。这工作。 –

+0

真棒,很高兴它帮助! – Val

+0

还需要什么? – Val