PythonElasticSearch更新,出现脚本异常

xxb16uws  于 2023-03-07  发布在  ElasticSearch
关注(0)|答案(1)|浏览(117)

我尝试使用python ES客户端将新对象追加到文档数组中(请参阅下面的代码片段)。但是我收到了RequestError(400,"非法参数异常","执行脚本失败")
Python代码:

def index_doc(doc_id, index):
    try:
        body = {
            "script": {
                "source": "ctx._source.array_1.add(params.data)",
                "lang": "painless",
                "params": {
                    "data": {
                        "field1": "abc",
                        "field2": 3
                    }
                }
            }
        }

        client.update(
            index=index,
            id=doc_id,
            routing=doc_id,
            body=body
        )
    except Exception as e:
        print("exception - ", doc_id, e)

也尝试了"脚本"参数,得到相同的RequestError(400,"非法参数异常","无法执行脚本")

def index_doc(doc_id, index):
    try:
        script = {
            "source": "ctx._source.array_1.add(params.data)",
            "lang": "painless",
            "params": {
                "data": {
                    "field1": "abc",
                    "field2": 3
                }
            }
        }

        client.update(
            index=index,
            id=doc_id,
            routing=doc_id,
            script=script
        )
    except Exception as e:
        print("exception - ", doc_id, e)

我用postman命令测试了相同的update payload,没有任何问题,新对象可以插入到文档数组中。

curl --location --request POST 'https://<endpoint>/<index_name>/_update/<doc_id>?routing=<doc_id>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <token>' \
--data-raw '{
    "script": {
        "source": "ctx._source.array_1.add(params.data)",
        "lang": "painless",
        "params": {
            "data": {
                "field1": "abc",
                "field2": 3
            }
        }
    }
}'

那么,有没有人能分享一下可能是罪魁祸首的想法?我正在使用ES 7. 17。提前感谢。

    • 在此处更新引用通告日志:**
client.update(
exception -  <doc_id> RequestError(400, 'illegal_argument_exception', 'failed to execute script')
Traceback (most recent call last):
  File "/<file_path>", line 137, in index_doc
    client.update(
  File "/Library/Python/3.9/site-packages/elasticsearch/client/utils.py", line 347, in _wrapped
    return func(*args, params=params, headers=headers, **kwargs)
  File "/Library/Python/3.9/site-packages/elasticsearch/client/__init__.py", line 2092, in update
    return self.transport.perform_request(
  File "/Library/Python/3.9/site-packages/elasticsearch/transport.py", line 466, in perform_request
    raise e
  File "/Library/Python/3.9/site-packages/elasticsearch/transport.py", line 427, in perform_request
    status, headers_response, data = connection.perform_request(
  File "/Library/Python/3.9/site-packages/elasticsearch/connection/http_urllib3.py", line 291, in perform_request
    self._raise_error(response.status, raw_data)
  File "/Library/Python/3.9/site-packages/elasticsearch/connection/base.py", line 328, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', 'failed to execute script')
lzfw57am

lzfw57am1#

根据官方文档(8.x),您不需要指定body,而是直接指定脚本:

client.update(
            index=index,
            id=doc_id,
            routing=doc_id,
            script=script            <------
        )

相关问题