基本步骤
- 确定文档标识:通过索引名称、类型(在Elasticsearch 7.0+ 类型已被弃用,可忽略)和文档ID来定位要更新的文档。
- 构建更新请求:定义需要更新的字段及其新值。
- 发送更新请求:使用Elasticsearch客户端将请求发送到Elasticsearch集群。
常用参数
- index:指定文档所在的索引名称。
- id:指定要更新的文档的唯一标识符。
- doc:包含要更新的字段和新值的JSON对象。
示例代码(以Python和Elasticsearch-py客户端为例)
from elasticsearch import Elasticsearch
# 连接到Elasticsearch集群
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
# 定义索引名称、文档ID和更新内容
index_name = 'your_index'
document_id = 'your_document_id'
update_data = {
"doc": {
"field_to_update": "new_value"
}
}
# 发送更新请求
response = es.update(index=index_name, id=document_id, body=update_data)
print(response)