Skip to content

Instantly share code, notes, and snippets.

@MalekBouba
Last active August 24, 2020 10:53
Show Gist options
  • Select an option

  • Save MalekBouba/ed6e0ef5b6c8644bc8732820c262b591 to your computer and use it in GitHub Desktop.

Select an option

Save MalekBouba/ed6e0ef5b6c8644bc8732820c262b591 to your computer and use it in GitHub Desktop.
Safe and clean code snippet to copies documents from one index to another using Elasticsearch Reindex API.

Reindex requires _source to be enabled for all documents in the source index. You must also set up the destination index before calling _reindex. Reindex does not copy the settings from the source index. Mappings, shard counts, replicas, and so on must be configured ahead of time.

Here is a simple example, you just need to change my-index & my-index-aux with your indexes names.

Get 'source' mapping

GET /my-index/_mapping

Create 'aux' index

PUT /my-index-aux/

Give 'aux' a mapping

PUT /my-index-aux/_mapping
{
  "properties": {
    "_id": {
      "type": "keyword"
    },
    "created_at": {
      "type": "date"
    },
    "deleted": {
      "type": "boolean"
    },
    "user_id": {
      "type": "keyword"
    }
  }
}

Check mapping

GET /my-index-aux/_mapping

Reindex old into aux

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

Count 'source' docs

GET /my-index/_count

Count 'aux' docs

GET /my-index-aux/_count

Delete 'source' index

DELETE /my-index

Create 'dest' index

PUT /my-index

Give 'dest' a mapping

PUT /my-index/_mapping
{
  "properties": {
    "_id": {
      "type": "keyword"
    },
    "created_at": {
      "type": "date"
    },
    "deleted": {
      "type": "boolean"
    },
    "user_id": {
      "type": "keyword"
    }
  }
}

Get 'dest' mapping

GET /my-index/_mapping

Reindex 'aux' into 'dest'

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

Count 'dest' docs

GET /my-index/_count

Delete 'aux' index

DELETE /my-index-aux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment