1

I am trying to use upsert to create a new badges record if it doesn't exist and append to the names array if it does. I have tried to follow Elasticsearch upserting and appending to array and the documentation without success. So far I have

es.update(index = '.people', 
            doc_type = 'badges',
            id= match['badgeNumber'], 
            body = {
                    "script": {
                        "inline": "if(ctx._source.names.contains(nm)) {ctx.op = 'none'} else {ctx._source.names += params.nm}", 
                        "lang" : "painless",
                        "params": { 
                            "nm": name 
                                    }
                        }, 
                    "upsert": {
                        "names": name 
                    } 
                    })

The code works fine to add new documents such as:

{
        "_index" : ".people",
        "_type" : "badges",
        "_id" : "12345",
        "_score" : 1.0,
        "_source" : {
          "names" : [
            "John Smith"
          ]
        }
      },
      {
        "_index" : ".people",
        "_type" : "badges",
        "_id" : "7896",
        "_score" : 1.0,
        "_source" : {
          "names" : [
            "Amy Wexler"
          ]
        }
      }

but if I try to update the list:

match = {'badge' = '12345'}
name = 'Johnny Smith'
update_names(name, match)

I get the error:

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 525, in update
    doc_type, id, '_update'), params=params, body=body)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/elasticsearch/transport.py", line 312, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/elasticsearch/connection/http_urllib3.py", line 128, in perform_request
    self._raise_error(response.status, raw_data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'illegal_argument_exception', '[9wu5eiG][127.0.0.1:9300][indices:data/write/update[s]]')
Janos Lenart
  • 25,074
  • 5
  • 73
  • 75
adidier
  • 11
  • 1

0 Answers0