gpt4 book ai didi

elasticsearch - 重命名 Elasticsearch 中的字段

转载 作者:行者123 更新时间:2023-11-29 02:46:59 24 4
gpt4 key购买 nike

我有这样一个文档

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

我需要将 field2 更改为 Request.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"
}

最佳答案

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

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

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