如何使用 python 编辑保存在 Elasticsearch 中的文档

How to Edit document saved in Elasticsearch using python

我是 elasticsearch 的新手,我正在尝试使用 python 执行 CRUD 操作。我已经创建了一个索引,并且能够将文档保存在 Elasticsearch 中。但是,当我尝试更新文档时,整个数据都被覆盖了。例如,在创建文档时有 3 个字段:

data = {
 "typeId":"someValue",
 "typeStatus":"someValue",
 "typeLists":"someValue",
 "createdDate","someValue"
}

以上内容在保存文档时被保存。在编辑上述文档时,createdDate 被删除。下面是更新文档。

data = {
 "typeId":"someValue",
 "typeStatus":"someValueEdited",
 "typeLists":"someValue",
 "updatedDate","someValue"
}

有没有办法 save/edit 文档而不丢失字段?下面是代码。

import urllib3
        
        
saveContext = '_doc'
updateContext = '_update'
httpClient = urllib3.PoolManager()
response = httpClient.request('PUT', 
                          elasticsearchURL, 
                          headers={'headersValue'},
                          body=json.dumps(items))

上下文值附加到 ES URL。

是的,你可以实现你想要的,只是不一样URL。

您可以像现在一样创建文档,只需点击 following endpoint

PUT index/_doc/<id>
{
  "typeId": "someValue",
  "typeStatus": "someValue",
  "typeLists": "someValue",
  "createdDate": "someValue"
}

然后,当你想部分更新你的文档时,你需要稍微敲一下different endpoint(主体略有不同),即

POST index/_doc/<id>/_update
{
   "doc" : {
     "typeId": "someValue",
     "typeStatus": "someValueEdited",
     "typeLists": "someValue",
     "updatedDate": "someValue"
   }
}

那么最终结果将是:

{
  "typeId": "someValue",
  "typeStatus": "someValueEdited",
  "typeLists": "someValue",
  "createdDate": "someValue",
  "updatedDate": "someValue"
}

另请注意,更新文档时,您无需传递值未更改的字段(即 typeIdtypeLists