当前位置: 首页 > 面试题库 >

在Elasticsearch中重命名字段

窦彦君
2023-03-14
问题内容

我有这样的文件

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

问题答案:

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


 类似资料:
  • 问题内容: 我有此映射的索引: 在新索引中,我想将“ title”复制到“ title1”和“ title2”,将“ body”复制到“ body1”和“ body2”(不考虑“ other”),并将类型从“ page”更改为“ Articles_eng”。新索引具有以下映射: 通过查看此答案和Elasticsearch重新索引文档,我得出了类似以下内容: 我在脚本行上遇到了麻烦。如果仅执行第一行

  • 问题内容: 无论如何,我可以在现有的Elasticsearch映射中重命名元素而不必添加新元素?如果是这样,为了避免破坏现有映射,最好的方法是什么? 例如从fieldCamelcase到fieldCamelCase 问题答案: 您可以通过创建一个Ingest管道来做到这一点,该管道包含一个Rename Processor 和Reindex API 。 请注意,您需要运行Elasticsearch

  • 在elasticsearch的索引中有两个字段field1和field2 我需要重命名字段1到字段2,我也想重命名字段2到字段1 这就是我当前映射的样子,我已经为此映射索引了许多文档 我需要下面的地图 是否有任何可能,我可以重命名字段和做重新索引? 我使用的是elasticsearch 7.4版

  • 问题内容: 我正在使用带有NEST的C#.NET应用程序来创建索引。 我创建了一个Elasticsearch索引,客户可以查询该索引,称为index_1。然后,我使用应用程序的不同实例构建索引的另一个版本,并将其称为index_1_temp。 我将index_1_temp重命名为index_1然后删除原始index_1的最安全方法是什么? 我知道ES具有别名,但是我不确定如何将其用于此任务 编辑:

  • 我想在sqlite中重命名列。我为一些列创建了两个带空格的单词标题,这些标题稍后会产生问题(例如而不是。 以前,这似乎是不可能的链接。但是几个月前的一个版本似乎包含了选项链接。 然而,这似乎不起作用。 查询将生成以下错误消息: 我为列名添加了引号,以防出现空格问题: 但也会出现同样的错误。 此解决方案提示重命名是可能的。但只重命名表(这很好),而不重命名列。

  • 我正在进行一个多模块Maven3项目的重构和维护过程,我需要重命名一些工件的groupId和artifactId。 有办法重命名groupId和artifactid吗? 我知道我可以使用sed之类的查找和替换工具,但我想知道除了groupId和artifactid之外,是否还有类似versions-maven-plugin(例如:)的mojo。