2

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

1 Answer 1

3

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": {}
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.