0

lets say that I have index that its mapping look like this:

curl -XPUT 'http://localhost:9200/oldindex/_mapping/book' -d '
   {
    "book" : {
        "properties" : {
            "title" : {"type" : "text"},
            "words" : {"type" : "text"},
            "pages": {"type": "int"}
        }
     }
   }'

I want to create a new index from the old index, but now I want the "words" type field to be "keyword" instead of "text":

curl -XPUT 'http://localhost:9200/oldindex/_mapping/book' -d '
       {
        "book" : {
            "properties" : {
                "title" : {"type" : "text"},
                "words" : {"type" : "keyword"},
                "pages": {"type": "int"}
            }
         }
       }'

How can I do that? can I use "Reindex API" or there is a better solution?

2
  • Which ES version? Commented Jan 16, 2018 at 22:51
  • elastic version: 5.6.4 Commented Jan 17, 2018 at 9:08

1 Answer 1

2

Assuming you don't want another field like wordsKeyword on the index, the Reindex API would probably be best. Move your data to a temp index, nuke the index and recreate it (with an updated mapping), then reindex the temp index data back into the new index.
Step by step this is:

  1. Create new index with new mapping.
  2. Copy the data from the old index to the new index via _reindex.
  3. Delete the old index.
  4. Recreate the old index with the new mapping.
  5. Copy the data from the new index to the old index via _reindex.
  6. Delete the new index. -- The "old index" (well technically a new one with the same name) will now have the updated mapping and data. Total of five API calls (two _reindexes to move the data over to new mapping, then to move it back)

If you don't mind having another field and don't want to reindex, you could look into using the _update_by_query API to literally copy over the value into the newly mapped keyword field via a script, thereby deprecating the text one.

Sign up to request clarification or add additional context in comments.

3 Comments

So let me re-explain your answer: 1. Create a new Index with the new mapping 2. Copy the data from the old index. Can I do it with one "Reindex" query?
1. Create new index with new mapping. 2. Copy the data from the old index to the new index via _reindex. 3. Delete the old index. 4. Recreate the old index with the new mapping. 5. Copy the data from the new index to the old index via _reindex. 6. Delete the new index. -- The "old index" (well technically a new one with the same name) will now have the updated mapping and data. Total of five API calls (two _reindexes to move the data over to new mapping, then to move it back)
PLease write this comment as an answer so I can V it

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.