2

We need to migrate a number of indexes from ElasticSearch 6.8 to ElasticSearch 7.x. To be able to do this, we now need to go back and fix a large number of documents are the _type field of these documents aren't _doc as required. We fixed this for newer indexes, but some of the older data which we still need has other values in here.

How do we reindex these indexes and also change the _type field?

POST /_reindex
{
    "source": {
        "index": "my-index-2021-11"
    },
    "dest": {
        "index": "my-index-2021-11-n"
    },
    "script": {
      "source": "ctx._type = '_doc';"
    }
}

I saw a post indicating the above might work, but on execution, the value for _type in the next index was still the existing of my-index.

The one option I can think of is to iterate through each document in the index and add it to the new index again which should create the correct _type, but that will take days to complete, so not so keen on doing that.

Paulo
  • 8,690
  • 5
  • 20
  • 34
mieliespoor
  • 945
  • 2
  • 8
  • 23

1 Answers1

3

I think below should work . Please test it out, before running on actual data

{
    "source": {
        "index": "my-index-2021-11"
    },
    "dest": {
        "index": "my-index-2021-11-n",
        "type":"_doc"
    }
}

Docs to help in upgradation

  1. https://www.elastic.co/guide/en/elasticsearch/reference/7.17/reindex-upgrade-inplace.html
jaspreet chahal
  • 8,817
  • 2
  • 11
  • 29
  • 2
    The answer as is isn't the full solution as I did try that before without success, but I got it to work now. Previously, I would've first created the new index by doing `PUT /my-index-2021-11` along with the mapping document. After that, I would've called the reindex. Today, ran the reindex without first doing the PUT call and it worked as expected. – mieliespoor Oct 14 '22 at 10:12
  • Can confirm on 5.x upgraded to 6.8.8, `PUT` to the index with an *inline* mapping that includes the doc type e.g. `{"mappings": "_doc": { ... everything normally in your mappings... }}` works and then you can do the documented `POST` to `_reindex` into it. – sorpigal Mar 11 '23 at 22:36