【问题标题】:Use Python API to conditionally update ElasticSearch document使用 Python API 有条件地更新 ElasticSearch 文档
【发布时间】:2016-10-29 22:17:25
【问题描述】:

我正在尝试更新文档(通过将元素附加到列表中),或者如果它不存在则创建。例如,我希望带有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 一起使用吗?

【问题讨论】:

    标签: python elasticsearch mappings


    【解决方案1】:

    你的身体有些错别字:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 2016-08-11
      相关资源
      最近更新 更多