Elastic search

Index Versioning

Overview

Indexes can be versioned. These versions will handle breaking data changes. To facilitate this the repositories will use an alias. This alias allows us to couple the most recent version of the version on an elastic level.

The mechanism used for this is called reindex .

Backup the current index

To kick of our changes, we will first reindex the existing index. We will add a version number on the end. In this example we will call this version 1.

To reindex use the following request:

POST _reindex
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001"
  }
}

Additionally we want to remove fields we don’t want in our new index. This will allow us to create a clean index when needed. To exclude fields we can use the following syntax. By using the script we can remove fields we don’t want in the new index

POST /_reindex
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-index-000001"
  },
  "conflicts": "proceed",
  "script" : {
    "source": "ctx._source.data.remove('properties.review_meta')"
  }

}

Create an alias

We will create an alias. This alias can be used in the application. We can change this alias when we need changed the index underneath. This will prevent the need to update the application on a data change.

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "my-data-stream",
        "alias": "my-alias"
      }
    }
  ]
}