【发布时间】:2018-05-18 14:36:31
【问题描述】:
我有一个奇怪的问题。如果我将 curl 与我发送到 Elasticsearch 的有效负载一起使用,我没有问题。当我尝试使用 Python 通过 Web 服务将 JSON 传递给 Elastic 时,我得到的结果取决于我设置有效负载的方式
- TypeError: unhashable type: 'dict'
- elasticsearch.exceptions.RequestError: TransportError(400, 'action_request_validation_exception', 'Validation Failed: 1: script or doc is missing;')
为了帮助调试,我将代码取出并内联运行以尝试解决问题。
要创建记录,我运行以下命令。
edit_author1 = {"name": "Sheldon Sid", "myid": 18}
resp = es.index(index="radsearch", doc_type="default", id=18, body={"doc": edit_author1})
这适用于使用 es.create 或 es.index 时初始创建记录,它返回以下内容,就像我从终端使用 curl 一样。
{
"_index": "radsearch",
"_type": "default",
"_id": "18",
"_version": 1,
"found": true,
"_source": {
"doc": {
"name": "Sheldon Sid",
"myid": 18
}
}
}
它按预期工作。当我尝试运行更新时,我有以下内容
resp = es.update(index="radsearch", doc_type="default", id=18, body={'doc': edit_author1})
以上返回
{
"_index": "radsearch",
"_type": "default",
"_id": "18",
"_version": 2,
"found": true,
"_source": {
"doc": {
"name": "Sheldon Sid",
"myid": 18
**},
"myid": 18,
"name": "Sheldon Sid"
}**
}
但是该文档作为一个单独的条目在 doc 下重复出现。如果我通过 curl 运行它,它会更新文档并且不会像上面那样创建第二个条目。
如果我尝试在没有 doc 密钥的情况下运行 create 或 update,我会收到 TypeError: unhashable type: 'dict'
resp = es.update(index="radsearch", doc_type="default", id=18, body={edit_author1})
TypeError: unhashable type: 'dict'
如果我尝试运行更新
resp = es.update(index="radsearch", doc_type="default", id=18, body=edit_author1)
我明白了
elasticsearch.exceptions.RequestError:
TransportError(400, 'action_request_validation_exception',
'Validation Failed: 1: script or doc is missing;')
有什么想法可以解决这个问题吗?
亲切的问候。
【问题讨论】:
标签: python elasticsearch