在弹性搜索中重命名字段 [英] Renaming fields in elasticsearch

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

问题描述

我有这样的文件

  {
_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,
请求:{
field3:data2
}
}
}

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

  PUT testindex / _mapping / logs 
{
properties:
{
请求:
{
属性:
{
field3:
{
:string
}
}
}
}
}

然后尝试重新索引

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

错误是

  reason:无法运行内联脚本[ctx._source.Request.field3 = ctx._source.remove(\field2\)]使用lang [groovy],
causes_by:{
type:null_pointer_exception,
reason:无法在空对象上设置属性field3
}


解决方案

您的文档中尚未存在请求字段您的脚本需要先创建它:

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


I have a document like this

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

I need to change the field2 to Request.field3

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

For this, first added a field mapping to existing index

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

Then tried reindexing

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

Error is

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

解决方案

The Request field does not yet exist in your documents, so your script needs to create it first:

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

Or a bit shorter like this:

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

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

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