简体   繁体   中英

Elasticsearch update document field value from interger to string

I have an elastic search index where one of field is mapped with data type of keyword which means field will accept all data types So due to this, I have received string and integer values into same field of elastic search index

Now, I would like to update all integer values to string

Ex: Document

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

So I would like to change all documents data where user_id field is integer then its value to string as below

Expected Document:

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

Can we do it with elastic search update query for all documents / any other solution?

Excuse if any mistakes.

Thanks in advance

You can leverage the Update by Query API to achieve this. Simply run this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM