# Update field with array of objects

**URL:** <https://discuss.elastic.co/t/update-field-with-array-of-objects/126157>\
**Category:** Elasticsearch\
**Created:** [March 29, 2018, 8:42pm UTC](https://discuss.elastic.co/t/update-field-with-array-of-objects/126157 "2018-03-29T20:42:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![net](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@net](https://discuss.elastic.co/u/net)\
**Post date:** [March 29, 2018, 8:42pm UTC](https://discuss.elastic.co/t/update-field-with-array-of-objects/126157/1 "2018-03-29T20:42:49Z")

</div>

Hi,

I'm using the update api to try to update a field. This field has the mapping (below) that works and has allowed for values that look like this:

[{'customer': 'Joe', 'group': 'blah', 'name': 'abc'}, {'customer': 'Joe', 'group': 'blah', 'name': 'abc'}]

The issue I'm having is that when I try to update the field with the value above I get the error below. It looks like it doesn't like the { but I'm not sure how I can fix that. Any help would be appreciated. Thanks.

```
"products" : {
  "type" : "nested",
  "properties" : {
    "customer" : {
      "type":"string",
      "index":"not_analyzed"
    },
    "group" : {
      "type":"string",
      "index":"not_analyzed"
    },
    "name" : {
      "type":"string",
      "index":"not_analyzed"
    }
  }
}

/store/abc/_update_by_query

{
  "script": {
    "inline": "ctx._source.products = [{'customer': 'Joe', 'group': 'blah', 'name': 'abc'}, {'customer': 'Joe', 'group': 'blah', 'name': 'abc'}]",
        "lang": "painless"
  },
  "query": {
    "term" : { "id" : "23423" } 
  }
}

error:

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "compile error",
                "script_stack": [
                    "... x._source.products = [{'customer': 'Joe', 'group ...",
                    " ^---- HERE"
                ],
                "script": "ctx._source.products = [{'customer': 'Joe', 'group': 'blah', 'name': 'abc'}, {'customer': 'Joe', 'group': 'blah', 'name': 'abc'}]",
                "lang": "painless"
            }
        ],
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
            "... x._source.products = [{'customer': 'Joe', 'tag_g ...",
            " ^---- HERE"
        ],
        "script": "ctx._source.products = [{'customer': 'Joe', 'group': 'blah', 'name': 'abc'}, {'viewers': 'Joe', 'group': 'blah', 'name': 'abc'}]",
        "lang": "painless",
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "invalid sequence of tokens near ['{'].",
            "caused_by": {
                "type": "no_viable_alt_exception",
                "reason": null
            }
        }
    },
    "status": 500
}
```

---

<div class="post-metadata">

**Author:** ![net](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@net](https://discuss.elastic.co/u/net)\
**Post date:** [March 29, 2018, 8:52pm UTC](https://discuss.elastic.co/t/update-field-with-array-of-objects/126157/2 "2018-03-29T20:52:19Z")

</div>

> [@net](#):
>
> {  
> "script": {  
> "inline": "ctx.\_source.products = [{'customer': 'Joe', 'group': 'blah', 'name': 'abc'}, {'customer': 'Joe', 'group': 'blah', 'name': 'abc'}]",  
> "lang": "painless"  
> },  
> "query": {  
> "term" : { "id" : "23423" }  
> }  
> }

Okay, I figured it out. I read that maps are defined like this in painless def person = ['name': 'Joe', 'age': 63];

So I replaced the { } to and it worked (see code below). Now I have an array of maps.

```
{
  "script": {
    "inline": "ctx._source.products = [['customer': 'Joe', 'group': 'blah', 'name': 'abc'], ['customer': 'Joe', 'group': 'blah', 'name': 'abc']]",
        "lang": "painless"
  },
  "query": {
    "term" : { "id" : "23423" } 
  }
}

```

---

<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 26, 2018, 8:53pm UTC](https://discuss.elastic.co/t/update-field-with-array-of-objects/126157/3 "2018-04-26T20:53:12Z")

</div>

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