繁体   English   中英

使用Python API有条件地更新ElasticSearch文档

[英]Use Python API to conditionally update ElasticSearch document

我正在尝试更新文档(通过将元素添加到列表中)或创建(如果不存在)。 例如,我希望id == Donald_Duck的文档添加列表suggestions的元素(如果尚不存在)。

name = 'Donald_Duck'
suggestions = ['Donald', 'Donald duck', 'Duck', 'Duck Avanger']
body = {
           "script" : "ctx._source.text += new_suggetions",
           "params" : { "new_suggestions" : suggestions},
            "upsert" : suggestions
        }


es.update(index=INDEX_NAME, doc_type='test', id=name, body=body)

不幸的是我收到一个RequestError

RequestError: TransportError(400, 'mapper_parsing_exception', 'failed to parse')  

这是我的映射的样子:

mappings = {
  "mappings": {
    "test": { 
      "properties": { 
        "text":    { 
                    "type": "completion", 
                    "analyzer" : "simple",
                    "search_analyzer" : "simple" 
        }, 
      }
    }
  }
}

我怎样才能解决这个问题? 如果我有多个文档,可以在bulk API中使用相同的代码吗?

您的身体有错别字:

body = {
    "script": "ctx._source.text += new_suggestions",
    "params": { "new_suggestions" : suggestions},
    "upsert": {
        "text": suggestions
    }
}

另外,您确定已启用脚本进行更新。 请阅读https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/modules-scripting.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM