# Converting a string field to an IP field

**URL:** https://discuss.elastic.co/t/converting-a-string-field-to-an-ip-field/78733
**Category:** Elasticsearch
**Created:** [March 15, 2017, 3:26pm UTC](https://discuss.elastic.co/t/converting-a-string-field-to-an-ip-field/78733 "2017-03-15T15:26:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Cylindric](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cylindric/32/5660_2.png) [@Cylindric](https://discuss.elastic.co/u/Cylindric)
#### Post date: [March 15, 2017, 3:26pm UTC](https://discuss.elastic.co/t/converting-a-string-field-to-an-ip-field/78733/1 "2017-03-15T15:26:24Z")

</div>

I have a bunch of log data that contains three IP fields, but these have been shipped as a string.  
I have created a new index template that defines these fields as IP now, and new data is coming in nicely, because my app passes in `null` for fields that don't have a valid IP, but if I `_reindex` my existing data, I get errors due to the empty strings that are currently in there.

Is there any way of using _painless_ scripts or something to catch invalid addresses on the way through and replacing them with nulls, instead of just abandoning the reindex?

The closest I could find was something like this:

```
POST _reindex
{
  "source": {
    "index": "twitter"
  },
  "dest": {
    "index": "new_twitter"
  },
  "script": {
    "inline": "if (ctx._source.ip == '') {ctx._source.ip = null}",
    "lang": "painless"
  }
}

```

How would I run that snippet on three separate fields?

---

<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 12, 2017, 3:26pm UTC](https://discuss.elastic.co/t/converting-a-string-field-to-an-ip-field/78733/2 "2017-04-12T15:26:28Z")

</div>

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