<commit_before>[[docs-reindex]]
== Reindex API

experimental[The reindex API is new and should still be considered experimental.  The API may change in ways that are not backwards compatible]

The most basic form of `_reindex` just copies documents from one index to another.
This will copy documents from the `twitter` index into the `new_twitter` index:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}
--------------------------------------------------
// AUTOSENSE

That will return something like this:

[source,js]
--------------------------------------------------
{
  "took" : 639,
  "updated": 112,
  "batches": 130,
  "version_conflicts": 0,
  "failures" : [ ],
  "created": 12344
}
--------------------------------------------------

Just like <<docs-update-by-query,`_update_by_query`>>, `_reindex` gets a
snapshot of the source index but its target must be a **different** index so
version conflicts are unlikely. The `dest` element can be configured like the
index API to control optimistic concurrency control. Just leaving out
`version_type` (as above) or setting it to `internal` will cause Elasticsearch
to blindly dump documents into the target, overwriting any that happen to have
the same type and id:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "version_type": "internal"
  }
}
--------------------------------------------------
// AUTOSENSE

Setting `version_type` to `external` will cause Elasticsearch to preserve the
`version` from the source, create any documents that are missing, and update
any documents that have an older version in the destination index than they do
in the source index:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "version_type": "external"
  }
}
--------------------------------------------------
// AUTOSENSE

Settings `op_type` to `create` will cause `_reindex` to only create missing
documents in the target index. All existing documents will cause a version
conflict:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "op_type": "create"
  }
}
--------------------------------------------------
// AUTOSENSE

By default version conflicts abort the `_reindex` process but you can just
count them by settings `"conflicts": "proceed"` in the request body:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "conflicts": "proceed",
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "op_type": "create"
  }
}
--------------------------------------------------
// AUTOSENSE

You can limit the documents by adding a type to the `source` or by adding a
query. This will only copy ++tweet++&apos;s made by `kimchy` into `new_twitter`:

[source,js]
--------------------------------------------------
POST /_reindex<commit_msg>Doc: /_reindex: Add a note about the source size parameter<commit_after>[[docs-reindex]]
== Reindex API

experimental[The reindex API is new and should still be considered experimental.  The API may change in ways that are not backwards compatible]

The most basic form of `_reindex` just copies documents from one index to another.
This will copy documents from the `twitter` index into the `new_twitter` index:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  }
}
--------------------------------------------------
// AUTOSENSE

That will return something like this:

[source,js]
--------------------------------------------------
{
  "took" : 639,
  "updated": 112,
  "batches": 130,
  "version_conflicts": 0,
  "failures" : [ ],
  "created": 12344
}
--------------------------------------------------

Just like <<docs-update-by-query,`_update_by_query`>>, `_reindex` gets a
snapshot of the source index but its target must be a **different** index so
version conflicts are unlikely. The `dest` element can be configured like the
index API to control optimistic concurrency control. Just leaving out
`version_type` (as above) or setting it to `internal` will cause Elasticsearch
to blindly dump documents into the target, overwriting any that happen to have
the same type and id:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "version_type": "internal"
  }
}
--------------------------------------------------
// AUTOSENSE

Setting `version_type` to `external` will cause Elasticsearch to preserve the
`version` from the source, create any documents that are missing, and update
any documents that have an older version in the destination index than they do
in the source index:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "version_type": "external"
  }
}
--------------------------------------------------
// AUTOSENSE

Settings `op_type` to `create` will cause `_reindex` to only create missing
documents in the target index. All existing documents will cause a version
conflict:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "op_type": "create"
  }
}
--------------------------------------------------
// AUTOSENSE

By default version conflicts abort the `_reindex` process but you can just
count them by settings `"conflicts": "proceed"` in the request body:

[source,js]
--------------------------------------------------
POST /_reindex
{
  "conflicts": "proceed",
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter",
    "op_type": "create"
  }
}
--------------------------------------------------
// AUTOSENSE

You can increase the default batch size by setting the `size` parameter (which defaults to `1000`) to the `source`. Note that it is different than the `size` parameter available at the root, which limits the number of documents (see below).

[source,js]
--------------------------------------------------
POST /_reindex
{
  "source": {
    "index": "twitter",
    "size": 2000
  },
  "dest": {
    "index": "new_twitter"
  }
}
--------------------------------------------------
// AUTOSENSE

You can limit the documents by adding a type to the `source` or by adding a
query. This will only copy ++tweet++&apos;s made by `kimchy` into `new_twitter`:

[source,js]
--------------------------------------------------
POST /_reindex
{'fraction_non_alphanumeric': 0.2941682189425419, 'fraction_numerical': 0.005444906147012466, 'mean_word_length': 1.744789618560755, 'pattern_counts': {'":': 65, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2474766', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1828, 'n_words': 692}