# Access and match a subfield (nested field) in a Painless script with the reindex API

**URL:** <https://discuss.elastic.co/t/access-and-match-a-subfield-nested-field-in-a-painless-script-with-the-reindex-api/224711>\
**Category:** Elasticsearch\
**Created:** [March 23, 2020, 6:21pm UTC](https://discuss.elastic.co/t/access-and-match-a-subfield-nested-field-in-a-painless-script-with-the-reindex-api/224711 "2020-03-23T18:21:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dsv/32/47173_2.png) [@dsv](https://discuss.elastic.co/u/dsv)\
**Post date:** [March 23, 2020, 6:21pm UTC](https://discuss.elastic.co/t/access-and-match-a-subfield-nested-field-in-a-painless-script-with-the-reindex-api/224711/1 "2020-03-23T18:21:21Z")

</div>

I have to change type (reindex) a field "source.ip" from the text mapping type to the IP type.  
Because some of the "source.ip" values have non IP format (like "bob\_IP").

I've tried:

```auto
POST _reindex
{
    "source": {
        "index": "p-aggtest-2020.02.29"
    },
    "dest": {
        "index": "p-aggtest-2020.02.29-delme"
    },
    "script": {
        "inline": "
             if ( !( ctx._source.source.ip.keyword =~ \/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\/) ) { 
                   ctx._source.source.ip_name = ctx._source.remove(\"source.ip\")
             }"
    }
}

```

But the error is:

```auto
"error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "if ( !( ctx._source.source.ip.keyword =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) ) { ",
          " ^---- HERE"
        ],
        "script" : "if ( !( ctx._source.source.ip.keyword =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) ) { ctx._source.source.ip_name = ctx._source.remove(\"source.ip\") }",
        "lang" : "painless"
      }
    ]

```

I've tried different combinations of `ctx._source.*, params._source*, doc['source'].ip` and readed dosens of docs and examples without success.

I would appreciate any ideas.

Thanks for the attention!

Best regards,

Serg

---

<div class="post-metadata">

**Author:** ![dsv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dsv/32/47173_2.png) [@dsv](https://discuss.elastic.co/u/dsv)\
**Post date:** [March 24, 2020, 7:32am UTC](https://discuss.elastic.co/t/access-and-match-a-subfield-nested-field-in-a-painless-script-with-the-reindex-api/224711/2 "2020-03-24T07:32:28Z")

</div>

It seems i got it.

```auto
POST _reindex
{
    "source": {
        "index": "p-aggtest-2020.02.29"
    },
    "dest": {
        "index": "p-aggtest-2020.02.29-delme"
    },
    "script": {
        "inline": "if ( !( ctx._source['source.ip'] != null && ctx._source['source.ip'] =~ \/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\/) ) { ctx._source['source.ip_name'] = ctx._source.remove(\"source.ip\") }"
    }
}

```

But in the dest index the quantity of docs is less than in the source index:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/4/44cd463d6f01f48d189964ee01c2b79a9d4e0747.png)

I've added to the script `else { ctx.op = 'noop' }` and checked the dest index. All is OK. The new field `source.ip_name` is created and the incorrect field `source.ip` is removed. But I can't understand how i lost the rest documents during reindexing.

---

<div class="post-metadata">

**Author:** ![dsv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dsv/32/47173_2.png) [@dsv](https://discuss.elastic.co/u/dsv)\
**Post date:** [March 24, 2020, 9:44am UTC](https://discuss.elastic.co/t/access-and-match-a-subfield-nested-field-in-a-painless-script-with-the-reindex-api/224711/3 "2020-03-24T09:44:52Z")

</div>

The error was in the mapping conflict in another field. I saw it in the bash script only.  
For thouse who will stuck on the similar problem the full bash script is:

```auto
#!/bin/bash -x
while read index
do
  curl --noproxy '*' -k -u user:password -HContent-Type:application/json -XPOST localhost:9200/_reindex?pretty -d'{
    "source": {
      "index": "'$index'"
    },
    "dest": {
      "index": "'$index'-00001"
    },
    "script": {
       "lang": "painless",
       "inline": " if ( !( ctx._source[\"source.ip\"] != null && ctx._source[\"source.ip\"] =~ \/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\/) ) { ctx._source[\"source.ip_name\"] = ctx._source.remove(\"source.ip\") } if ( !( ctx._source[\"destination.ip\"] != null && ctx._source[\"destination.ip\"] =~ \/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\/) ) { ctx._source[\"destination.ip_name\"] = ctx._source.remove(\"destination.ip\") } "
    }
  }'
done

```

Don't use curl `-k` option unless you are really need it.

Good luck!

---

<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 21, 2020, 9:45am UTC](https://discuss.elastic.co/t/access-and-match-a-subfield-nested-field-in-a-painless-script-with-the-reindex-api/224711/4 "2020-04-21T09:45:04Z")

</div>

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