使用 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/en/elasticsearch/reference/current/modules-scripting.html
我正在尝试更新文档(通过将元素附加到列表),或者如果它不存在则创建。例如,我希望带有 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/en/elasticsearch/reference/current/modules-scripting.html