# Reindex with adding subdocuments (filebeat index 5.6 to 6.6 migration)

**URL:** https://discuss.elastic.co/t/reindex-with-adding-subdocuments-filebeat-index-5-6-to-6-6-migration/174582
**Category:** Elasticsearch
**Created:** [March 29, 2019, 5:13pm UTC](https://discuss.elastic.co/t/reindex-with-adding-subdocuments-filebeat-index-5-6-to-6-6-migration/174582 "2019-03-29T17:13:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Hubert\_Lyczek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubert_lyczek/32/43111_2.png) [@Hubert\_Lyczek](https://discuss.elastic.co/u/Hubert_Lyczek)
#### Post date: [March 29, 2019, 5:13pm UTC](https://discuss.elastic.co/t/reindex-with-adding-subdocuments-filebeat-index-5-6-to-6-6-migration/174582/1 "2019-03-29T17:13:50Z")

</div>

Hello,

I'm testing upgrade from **5.6** to **6.6** currently. After upgrading ES, Kibana, Logstash and Beats I noticed that documents produced by beats are slightly different. That's ok, as all is as described in the documentation.

Example (old index):

```
{
  "host": "dev",
  "input_type": "log"
  ...
}

```

(new index):

```
{
  "host": {
    "name": "dev"
  },
  "input": {
    "type": "log"
  }
  ...
}

```

**My problem:**  
I want to migrate old indices to a new structure. I will create new indexes anyway, as they will have a different mapping. I thought of using reindex functionality. Reindex API looks promising as it contains example how to rename fields during reindexing, but it allows adding new fields and modifying existing fields.

So something like this:

```
curl -i -H "Content-Type: application/json" --data '
{
  "source": {
    "index": "old"
  },
  "dest": {
    "index": "new",
    "type": "doc"
  },
  "script": {
    "source": "ctx._source.host.name = ctx._source.remove(\"host\"); ctx._source.index.type = ctx._source.remove(\"index_type\")"
  }
}' -X POST http://localhost:9200/_reindex

```

will produce errors. Problems are of course:

- host already exists and it is a String (do not have name field)
- index does not exist, and causes null\_pointer\_exception

I haven't find a good explanation of how to use context, what is contains, whether there is something like _\_destination_.

**Question:**  
Is it posible to achieve index reshape as described at the beginning?  
if not, how to do this?

Thanks in advance.  
Hubert

**PS**  
This looks like a common problem, and this should be easy to solve. Am I missing something?

---

<div class="post-metadata">

### Author: ![Hubert\_Lyczek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubert_lyczek/32/43111_2.png) [@Hubert\_Lyczek](https://discuss.elastic.co/u/Hubert_Lyczek)
#### Post date: [April 1, 2019, 12:40pm UTC](https://discuss.elastic.co/t/reindex-with-adding-subdocuments-filebeat-index-5-6-to-6-6-migration/174582/2 "2019-04-01T12:40:04Z")

</div>

This the solution to this problem:

POST \_reindex

```
{        
      "source": {
        "index": "test_old"
      },                                                     
      "dest": {
        "index": "test_new",
        "type": "doc"
      },
      "script": {
        "source": "ctx._source.host = [\"name\": ctx._source.remove(\"host\")]; ctx._source.input = [\"type\": ctx._source.remove(\"input_type\")];"
      }

```

}

The main key is to use Painless Map syntax.  
Now everything works fine.

Hubert

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 29, 2019, 12:40pm UTC](https://discuss.elastic.co/t/reindex-with-adding-subdocuments-filebeat-index-5-6-to-6-6-migration/174582/3 "2019-04-29T12:40:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
