# Create structure in painless update script

**URL:** <https://discuss.elastic.co/t/create-structure-in-painless-update-script/207865>\
**Category:** Logstash\
**Created:** [November 14, 2019, 10:50am UTC](https://discuss.elastic.co/t/create-structure-in-painless-update-script/207865 "2019-11-14T10:50:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MMH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmh/32/54449_2.png) [@MMH](https://discuss.elastic.co/u/MMH)\
**Post date:** [November 14, 2019, 10:50am UTC](https://discuss.elastic.co/t/create-structure-in-painless-update-script/207865/1 "2019-11-14T10:50:12Z")

</div>

I am using Elasticsearch 6.5.  
I want to use an update API with the painless script. Let's say, that my target document has an `address` structure inside of it. Update script may look something like this:

```auto
ctx._source.address.street = params.src.address.street;   
ctx._source.address.city = params.src.address.city;   
ctx._source.address.postal_code = params.src.address.postal_code; 

```

Which usually runs great. But there are documents, that do not have an `address` structure. In these cases update script fails with `null_pointer_exception` error - then I am addressing inner field of not existing structure.

So basically my questions are:

1. How to detect in painless, that document does not have an `address` structure?
2. How to create this inner structure and set values to it?

To answer questions that may arise: making the whole document flat (just fields without inner structure) is not an option in my case. Also, I cannot modify documents to ensure, that everyone has an `address` structure, before an update script.

---

<div class="post-metadata">

**Author:** ![MMH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmh/32/54449_2.png) [@MMH](https://discuss.elastic.co/u/MMH)\
**Post date:** [November 14, 2019, 1:26pm UTC](https://discuss.elastic.co/t/create-structure-in-painless-update-script/207865/2 "2019-11-14T13:26:41Z")

</div>

Ok,  
I managed to detect if `address` structure exist with:

```auto
if (ctx._source.address != null) { (...) }

```

But still I have no idea how to create this structure.

I tried the following:

```auto
POST test_idx1/_doc/1/_update
{
    "script": """ 
        if (ctx._source.address != null) {
            ctx._source.address.street = params.src.street;
            ctx._source.address.city = params.src.city;
            ctx._source.address.postal_code = params.src.postal_code;
        } else {
            ctx._source.address = params.src;
        }
        """,
    "params": {
    "src": 
        {
            "street" : "test str",
            "city": "London",
            "postal_code": "01-234"
        }
    }
}

```

But in the result the `address` field is created, but has `null` value.

---

<div class="post-metadata">

**Author:** ![MMH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmh/32/54449_2.png) [@MMH](https://discuss.elastic.co/u/MMH)\
**Post date:** [November 14, 2019, 2:53pm UTC](https://discuss.elastic.co/t/create-structure-in-painless-update-script/207865/3 "2019-11-14T14:53:10Z")

</div>

Ok, I cannot delete the thread, so I will post the solution.  
The syntax was wrong. The correct one is below:

```auto
POST test_idx1/_doc/1/_update
{
    "script": {
    "source" : """ 
        if (ctx._source.address != null) {
            ctx._source.address.street = params.src.street;
            ctx._source.address.city = params.src.city;
            ctx._source.address.postal_code = params.src.postal_code;
        } else {
            ctx._source.address = params.src;
        }
        """,
    "params": {
    "src": 
        {
            "street" : "test str",
            "city": "London",
            "postal_code": "01-234"
        }
    }
  }
}

```

---

<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:** [December 12, 2019, 2:53pm UTC](https://discuss.elastic.co/t/create-structure-in-painless-update-script/207865/4 "2019-12-12T14:53:14Z")

</div>

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