# How to reindex and add new field?

**URL:** <https://discuss.elastic.co/t/how-to-reindex-and-add-new-field/167660>\
**Category:** Elasticsearch\
**Created:** [February 8, 2019, 3:44pm UTC](https://discuss.elastic.co/t/how-to-reindex-and-add-new-field/167660 "2019-02-08T15:44:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)\
**Post date:** [February 8, 2019, 3:44pm UTC](https://discuss.elastic.co/t/how-to-reindex-and-add-new-field/167660/1 "2019-02-08T15:44:09Z")

</div>

Hi,

Currently I have two versions of a field in different indexes. Here I use `session.id` and there I use sessionId.

I will make them equal in logstash for new values, but how can I change the old data via reindex?  
I want to create an additional field `sessionId` and this field needs to have the value of `session.id`

I tried

```
  "script": {
    "source": "ctx._source['sessionId'] = ctx._source['[session][id]']",
    "lang": "painless"
  }

```

and

```
  "script": {
    "source": "ctx._source['sessionId'] = ctx._source['session.id']",
    "lang": "painless"
  }

```

but alwayss sessionId is null.

What is the correct syntax?

---

<div class="post-metadata">

**Author:** ![Shaoranlaos](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Shaoranlaos](https://discuss.elastic.co/u/Shaoranlaos)\
**Post date:** [February 11, 2019, 2:28pm UTC](https://discuss.elastic.co/t/how-to-reindex-and-add-new-field/167660/2 "2019-02-11T14:28:20Z")

</div>

i had to do something similar due to the change from filebeat mapping side and here is the script that i used  
but i have it the other way around (a string must be written in an object)

```auto
"script": {
    "source": "if (ctx._source.host instanceof String) {String host=ctx._source.host;ctx._source.host=['name':host];}"
  }

```

you could try to access your sessionid with

```auto
ctx._source.session.id

```

or

```auto
ctx._source['session']['id']

```

---

<div class="post-metadata">

**Author:** ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)\
**Post date:** [February 11, 2019, 7:55pm UTC](https://discuss.elastic.co/t/how-to-reindex-and-add-new-field/167660/3 "2019-02-11T19:55:39Z")

</div>

great, thanks for your explanation. For me it doesn't currently matter which direction I use - at least for the upcoming showcase.

I used followig to write the value of sessionId to session.id:

```
 "script": {
    "source": "if (ctx._source.sessionId instanceof String) {String id=ctx._source.sessionId;ctx._source['session']=['id':id];}",
    "lang": "painless"
  }

```

Thanks a lot.

---

<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:** [March 11, 2019, 8:03pm UTC](https://discuss.elastic.co/t/how-to-reindex-and-add-new-field/167660/4 "2019-03-11T20:03:33Z")

</div>

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