# Convert the long datatype field to string

**URL:** <https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692>\
**Category:** Elasticsearch\
**Created:** [May 14, 2018, 8:39am UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692 "2018-05-14T08:39:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajesnal](https://avatars.discourse-cdn.com/v4/letter/r/df788c/32.png) [@rajesnal](https://discuss.elastic.co/u/rajesnal)\
**Post date:** [May 14, 2018, 8:39am UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692/1 "2018-05-14T08:39:08Z")

</div>

I am looking for an example, to convert the data type of field in the document from long to string.  
could you anyone please help me on this.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 14, 2018, 9:06am UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692/2 "2018-05-14T09:06:32Z")

</div>

You need to reindex your data, as the internal data structure used to store a long differs from the one storing a string.

Check out the [Reindex API](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-reindex.html), which can help you to execute the reindexing, once you have changed your mapping properly in your new index.

---

<div class="post-metadata">

**Author:** ![rajesnal](https://avatars.discourse-cdn.com/v4/letter/r/df788c/32.png) [@rajesnal](https://discuss.elastic.co/u/rajesnal)\
**Post date:** [May 14, 2018, 9:39am UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692/3 "2018-05-14T09:39:44Z")

</div>

First of all thanks for the reply.

I am OK, to do the reindex. But I am not to specify the datatype change in the new index, i have tried with below example. it was not working. still the new index has the data type long only.

```
POST _reindex?pretty
{
  "source": {
    "index": "index1.1526279922.1"
  },
  "dest": {
    "index": "index1.string.1"
  },
  "script": {
    "inline": "ctx._source.inode = String.valueOf(ctx._source.inode)"
  }
}

```

Please correct me, if i am doing anything wrong here.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 14, 2018, 11:40am UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692/4 "2018-05-14T11:40:20Z")

</div>

You need to create the proper mapping before you start indexing documents. See our [mapping documentation](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/mapping.html).

See [reindex API docs](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/docs-reindex.html) also state this in the first paragraph.

---

<div class="post-metadata">

**Author:** ![rajesnal](https://avatars.discourse-cdn.com/v4/letter/r/df788c/32.png) [@rajesnal](https://discuss.elastic.co/u/rajesnal)\
**Post date:** [May 28, 2018, 1:25pm UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692/5 "2018-05-28T13:25:42Z")

</div>

Finally, I got the solution. Thank you very much for your support.

Below is the same.

```
POST _reindex?pretty
{
  "source": {
    "index": "index1.1526279922.1"
  },
  "dest": {
    "index": "index1.string.1"
  },
  "script": {
    "source": "ctx._source.inode = '' + ctx._source.inode"
  }
}
```

---

<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:** [June 25, 2018, 1:27pm UTC](https://discuss.elastic.co/t/convert-the-long-datatype-field-to-string/131692/6 "2018-06-25T13:27:53Z")

</div>

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