Update field with array of objects

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
}

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" } 
  }
}

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