【问题标题】:Elasticsearch update document field value from interger to stringElasticsearch 将文档字段值从整数更新为字符串
【发布时间】:2019-11-06 12:02:20
【问题描述】:

我有一个弹性搜索索引,其中一个字段与关键字的数据类型映射 这意味着字段将接受所有数据类型 因此,因此,我已将字符串和整数值接收到弹性搜索索引的同一字段中

现在,我想将所有整数值更新为字符串

例如:文档

{
    "_index": "my-index",
    "_type": "my_index_doc",
    "_id": "PqLbOW4BtJ-51rS9hMsm",
    "_score": null,
    "_source": {
        "user_id": 1019407,
    },
    "sort": [
        1572928717850
    ]
}

所以我想将 user_id 字段为整数的所有文档数据更改为字符串,如下所示

预期文件:

{
    "_index": "my-index",
    "_type": "my_index_doc",
    "_id": "PqLbOW4BtJ-51rS9hMsm",
    "_score": null,
    "_source": {
        "user_id": "1019407",
    },
    "sort": [
        1572928717850
    ]
},

我们可以对所有文档/任何其他解决方案使用弹性搜索更新查询吗?

如有错误请见谅。

提前致谢

【问题讨论】:

    标签: elasticsearch elasticsearch-query


    【解决方案1】:

    您可以利用Update by Query API 来实现此目的。只需运行:

    POST my-index/_update_by_query
    {
      "script": {
        "source": "ctx._source.user_id = Integer.toString(ctx._source.user_id);",
        "lang": "painless"
      },
      "query": {
        "match_all": {}
      }
    }
    

    【讨论】:

    • 酷,很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多