> For the complete documentation index, see [llms.txt](https://cbelasticsearch.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cbelasticsearch.ortusbooks.com/indices/reindexing.md).

# Reindexing

Sometimes indices get messy. Learn how to reindex your data in CBElasticsearch.

On occasion, due to a mapping or settings change, you will need to reindex data from one index (the "source") to another (the "destination"). You can do this by calling the `reindex` method on CBElasticsearch's `Client` component.

```js
getInstance( "Client@cbelasticsearch" )
    .reindex( "oldIndex", "newIndex" );
```

## Asynchronous Reindexing

If you want the work to be done asynchronusly, you can pass `false` to the `waitForCompletion` flag. When this flag is set to false the method will return a [`Task` instance](https://cbelasticsearch.ortusbooks.com/tasks.md), which can be used to follow up on the completion status of the reindex process.

```js
getInstance( "Client@cbelasticsearch" )
    .reindex(
        source = "oldIndex",
        destination = "newIndex",
        waitForCompletion = false
    );
```

## Additional Reindex Options

If you have more settings or constraints for the reindex action, you can pass a struct containing valid options to `source` and `destination`.

```js
getInstance( "Client@cbelasticsearch" )
    .reindex(
        source = {
            "index": "oldIndex",
            "type": "testdocs",
            "query": {
                "term": {
                    "active": true
                }
            }
        },
        destination = "newIndex"
    );
```

## Transforming Documents via a Reindex Script

You may also pass a script in to the `reindex` method to transform objects as they are being transferred from one index to another:

```js
getInstance( "Client@cbelasticsearch" )
    .reindex(
        source = {
            "index": "oldIndex",
            "type": "testdocs",
            "query": {
                "term": {
                    "active": true
                }
            }
        },
        destination = "newIndex",
        script = {
            "lang" : "painless",
            "source" : "if( ctx._source.foo != null && ctx._source.foo == 'baz' ){ ctx._source.foo = 'bar'; }"
        }
    );
```

Note that a Painless script containing newlines, tabs, or space indentation will throw a parsing error. To work around this limitation, use CBElasticsearch's `Util.formatToPainless( string script )` method to remove newlines and indentation:

```js
getInstance( "Client@cbelasticsearch" )
    .reindex(
        // ...
        script = {
            "lang" : "painless",
            "source" : getInstance( "Util@cbelasticsearch" )
                        .formatToPainless( getReindexScript() )
        }
    );
```

## Handling Reindex Errors

If you `waitForCompletion` and the reindex action fails, a `cbElasticsearch.HyperClient.ReindexFailedException` will be thrown. You can disable the exception by passing `false` to the `throwOnError` parameter:

```js
getInstance( "Client@cbelasticsearch" )
    .reindex(
        source = "oldIndex",
        destination = "newIndex",
        waitForCompletion = false,
        throwOnError = false
    );
```

{% hint style="info" %}
As always, check out the Elasticsearch documentation for more specifics on [what reindexing is and how it works](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cbelasticsearch.ortusbooks.com/indices/reindexing.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
