【问题标题】:How do I get passed Elasticsearch Updating or Inserting using Python TypeError?如何使用 Python TypeError 通过 Elasticsearch 更新或插入?
【发布时间】: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


    【解决方案1】:

    终于找到答案了。这是格式问题。我很接近。为了使用 Elasticsearch API,您需要附加 doc 以运行更新。

    对于初始插入,这是可行的

    edit_author1 = {"name": "Sheldon Sid", "myid": 18}
    

    当你想更新时,你需要像这样修改它

    edit_author1 = {"doc":{"name": "Sheldon Sid2", "myid": 18})
    

    注意前面的doc。如果您没有在更新中添加文档并尝试运行命令

    resp = es.update(index="myindex",
                         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;')
    

    【讨论】:

    • Elasticsearch 希望 bodydoc 嵌入
    【解决方案2】:

    index 查询使用了一个相当不必要的字段doc,而update 查询按预期工作,它更新了_source,而不是其中的doc

    对于错误,使用的body 无效。使用

    resp = es.update(index="radsearch", doc_type="default", id=18, 
     body={"doc": edit_author1})
    

    参考:example-usage

    【讨论】:

    • 我已经经历了 10 次,但它没有按预期工作。你没有通过你的回答来看待问题或问题。还是谢谢。
    • 我已经更新了答案以包含要使用的查询,如果有帮助请告诉我
    • 刚刚更新了问题。您给出的答案不起作用并给出“验证失败:1:缺少脚本或文档;”)
    • 我的回答的第一行说,你在索引时使用了不必要的字段doc(这是初始插入)和关于更新查询我错过了doc,因为你已经在使用在第二个查询中。我已经编辑了update 查询
    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2014-04-02
    • 2013-03-06
    • 2018-04-28
    • 2016-12-25
    相关资源
    最近更新 更多