DEV Community

Mallikarjun H T
Mallikarjun H T

Posted on • Updated on

Elastic Search Reindexing

Reindex API

Documents are copied from one location to another.
The document source is extracted from the source index and the documents are indexed in the destination index. You can reindex a selection of the documents or transfer all items to the target index.

Reindexing requires that _source be enabled for all documents in the source.
Before invoking _reindex, the destination should be specified as desired. The settings from the source or its related template are not copied when you reindex.
Mappings, shard counts, replicas, and other parameters must be set up ahead of time.

Querry

POST _reindex
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001"
  }
}
Enter fullscreen mode Exit fullscreen mode

OR

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001"
  }
}'
Enter fullscreen mode Exit fullscreen mode

_reindex with scripts

We can use Painless scripting to run our querry.

POST _reindex
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001"
  },
  "script": {
    "inline": "ctx._source.field = Long.parseLong(ctx._source.field)"
  }
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

Avoide Reindexing!
If reindexing is absolutely necessary, this Querry can be used.
If we need to convert one data type to another, we may use scripts to our benefit.
If you are familiar with pipelines, we can do much more sophisticated tasks with ease.

Resources

ElasticSearch

Top comments (0)