0
votes

I have a problem of re-indexing only part of the index data in elastic search.

Using PHP elastic search client.

Let's say I have an index myindex with types type1 and type2.

Now some of the fields in type2 changes and I need to change the mapping.

After reading their docs, I thought that I could get away by creating a new type and creating an alias, but unfortunately it seems that you cannot have alias for types, it's only applicable for index.

Then, I thought to create a new temporary mapping for newtype2, copy data from type2 to newtype2 and delete type2.

But now I cannot find any way to rename a type.

3

3 Answers

1
votes

I would like to extend marcostvz's answer.

If you really want the existing index to have type name, even though it can not be done directly, you can do that with help of some other index and reindexing.

  1. Create temporary index.
  2. Copy original index with old type to temporary index with new type.
  3. Recreate (delete and put) the original index.
  4. Copy temporary index to original index.
  5. Delete temporary index.

While running the code, I run into problem that my data got lost in the process. Adding a little break between the commands (sleep 5 to be exact) solved the problem.

curl -XPUT localhost:9200/index_tmp &&
curl -XPOST localhost:9200/_reindex -H 'Content-Type: application/json' -d '
{
  "source": {
    "index": "index",
    "type": "OldType"
  },
  "dest": {
    "index": "index_tmp",
    "type": "NewType"
  }
}' &&
curl -XDELETE localhost:9200/index &&
curl -XPUT localhost:9200/index &&
curl -XPOST localhost:9200/_reindex -H 'Content-Type: application/json' -d '
{
  "source": {
    "index": "index_tmp"
  },
  "dest": {
    "index": "index"
  }
}' &&
curl -XDELETE localhost:9200/index_tmp
0
votes

The closest way to rename a type would be using a new index (with the new type):

  • Create the new index (with the new type, I am using a template)
  • Reindex using the new type
  • Delete the old index
  • Create an alias (to be able to use as the old naming)

Example renaming flb_type to _doc:

    export INDEX=log-2019.03.12 &&
    curl -XPUT localhost:9200/$INDEX-v2 &&
    curl -XPOST localhost:9200/_reindex -H 'Content-Type: application/json' -d '
    {
      "source": {
        "index": "'$INDEX'",
        "type": "flb_type"
      },
      "dest": {
        "index": "'$INDEX'-v2",
        "type": "_doc"
      }
    }' &&
    curl -XDELETE localhost:9200/$INDEX &&
    curl -XPOST localhost:9200/_aliases -H 'Content-Type: application/json' -d '
    {
      "actions": [
        {
          "add":
          {
            "alias": "'$INDEX'",
            "index": "'$INDEX'-v2"
          }
        }
      ]
    }'