[英]Use Python API to conditionally update ElasticSearch document
I am trying to update a document (by appending elements to a list), or create if it does not exists. 我正在尝试更新文档(通过将元素添加到列表中)或创建(如果不存在)。 For example, I want the document with id == Donald_Duck
to add the elements of the list suggestions
, if not present already. 例如,我希望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)
Unfortunately I get a RequestError
: 不幸的是我收到一个RequestError
:
RequestError: TransportError(400, 'mapper_parsing_exception', 'failed to parse')
This is how my mappings looks like: 这是我的映射的样子:
mappings = {
"mappings": {
"test": {
"properties": {
"text": {
"type": "completion",
"analyzer" : "simple",
"search_analyzer" : "simple"
},
}
}
}
}
How can I fix this? 我怎样才能解决这个问题? If I have multiple documents, can I use the same code with the bulk
API? 如果我有多个文档,可以在bulk
API中使用相同的代码吗?
You have some typo in your body: 您的身体有错别字:
body = {
"script": "ctx._source.text += new_suggestions",
"params": { "new_suggestions" : suggestions},
"upsert": {
"text": suggestions
}
}
Plus, are you sure you have enabled the scripting for updates. 另外,您确定已启用脚本进行更新。 Please read https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html 请阅读https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/modules-scripting.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.