簡體   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