如何使用Python将新的键值对插入到现有的弹性文档中?

问题描述 投票:0回答:1

我尝试了elastic中典型的_update方法,但不起作用。

data_to_send ={
        "new_key1": new_value1,
        "new_key2": new_value2,
        "new_key3": new_value3
      }

response = requests.post(
          url=f"https://{elasticurl}:{port}/{target_index}/_update/{id}", 
          headers={'Content-Type': 'application/json'}, 
          json=data_to_send, 
          verify=False,
          auth = (user,password))

url 经过双重检查,id 是用于标识要更新的文档的文档 ID。这里的关键疑问是,当我们将 data_to_send 发送到 url 时,文档会发生什么,文档是否接受新的键值对?文档没有解决将新的键值对插入现有文档的问题,它只讨论更新现有键的值。

我也尝试过 requests.put、elasticsearch.update 在尝试将新的键值对插入现有文档时,它们似乎都不起作用。我需要某种方法使用弹性中的文档 ID 将新的键值对插入到现有文档中。

json python-3.x elasticsearch python-requests insert-update
1个回答
0
投票

您可以参考this文档,正如所提到的,文档应该在

doc
内传递以进行部分更新。

POST test/type1/1/_update
{
    "doc" : {
        "name" : "new_name"
    }
}

我不是Python专家,但

data_to_send
应该更改如下并且它应该可以工作:

data_to_send ={
   "doc" : {
        "new_key1": new_value1,
        "new_key2": new_value2,
        "new_key3": new_value3
      }
}
© www.soinside.com 2019 - 2024. All rights reserved.