Redistribute Elastic Search Index 



1. Disable allocation:

PUT _cluster/settings
{
    "transient": {
        "cluster.routing.allocation.enable": "none"
    }
}


Disabled Allocation to NONE Output generated by the command above
{
  "acknowledged": true,
  "persistent": {},
  "transient": {
    "cluster": {
      "routing": {
        "allocation": {
          "enable": "none"
        }
      }
    }
  }
}

2. Cancel the allocation for the replica

POST _cluster/reroute
{
    "commands": [
        {
            "cancel": {
                "index": "test2024.05.01-000134", "shard": 0,
                "node": "instance-0000000003"
            }
        }
    ]
}

The index is NOW in UNASSIGNED State Output generated by the command above POST _cluster/reroute
              {
                "state": "UNASSIGNED",
                "primary": false,
                "node": null,
                "relocating_node": null,
                "shard": 0,
                "index": "test2024.05.01-000134",
                "recovery_source": {
                  "type": "PEER"
                },

You can see the replica with the GET _cat/shards in Unassigned

test2024.05.01-000134                                    0 p STARTED       0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000134                                    0 r UNASSIGNED                           


3. Move the primary shard

POST /_cluster/reroute
{
    "commands": [
        {
            "move": {
                "index": "test2024.05.01-000134",
                "shard": 0,
                "from_node": "instance-0000000002",
                "to_node": "instance-0000000003"
            }
        }
    ]
}


Output REALLOCATION state after executing command above 
      "test2024.05.01-000134": {
          "shards": {
            "0": [
              {
                "state": "RELOCATING",
                "primary": true,
                "node": "JMzF2z1mRXkrml4Q",
                "relocating_node": "Xn35Qqb7Q-d-asC6g",
                "shard": 0,
                "index": "test2024.05.01-000134",
                "expected_shard_size_in_bytes": 225,
                "allocation_id": {
                  "id": "GFrWYdYyQcWvXDgsk9zihg",
                  "relocation_id": "R1SAcz_M581yEF5og"
                },
                "relocation_failure_info": {
                  "failed_attempts": 0
                }

Now the index was REALLOCATED In the instance-00000003, the index will be put in STARTED state

test2024.05.01-000134                                    0 p STARTED       0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000134                                    0 r UNASSIGNED                           


4. Enable the automatic allocation again


PUT _cluster/settings
{
    "transient": {
        "cluster.routing.allocation.enable": null
    }
}


Output 

{
  "acknowledged": true,
  "persistent": {},
  "transient": {}
}



Final Result we had redistribute index between instances

test2024.05.01-000134                                    0 p STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000134                                    0 r STARTED    0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000135                                    0 p STARTED    0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000135                                    0 r STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000136                                    0 p STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000136                                    0 r STARTED    0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000137                                    0 p STARTED    0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000137                                    0 r STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000138                                    0 p STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000138                                    0 r STARTED    0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000139                                    0 p STARTED    0    225b 10.1.7.5  instance-0000000002
test2024.05.01-000139                                    0 r STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000140                                    0 p STARTED    0    225b 10.1.12.4 instance-0000000003
test2024.05.01-000140                                    0 r STARTED    0    225b 10.1.7.5  instance-0000000002




If you need  check replica state, you can use when you want to see Replica or Primary STATE

GET /_cat/shards/test2024.04.12-000010?v

Comments