1

我正在尝试更新文档(通过将元素附加到列表中),或者如果它不存在则创建。例如,我希望文档id == Donald_Duck添加 list 的元素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" 
        }, 
      }
    }
  }
}

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

4

1 回答 1

2

你的身体有一些错字:

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

于 2016-06-27T19:48:34.330 回答