python使用upsert附加到elasticsearch 5.4中的数组

时间:2017-07-24 22:36:59

标签: python elasticsearch

我试图使用upsert创建一个新的徽章记录(如果它不存在)并附加到names数组(如果它存在)。我试图关注Elasticsearch upserting and appending to arraydocumentation但没有成功。到目前为止我已经

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

该代码可以正常添加新文档,例如:

{
        "_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"
          ]
        }
      }

但如果我尝试更新列表:

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

我收到错误:

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]]')

0 个答案:

没有答案