新增“数据编辑”

This commit is contained in:
2025-10-02 15:49:36 +08:00
parent 657365f9de
commit 8c530ff599
3 changed files with 164 additions and 4 deletions

View File

@@ -40,6 +40,9 @@ def create_index_with_mapping():
else:
print(f"索引 {index_name} 已存在")
def update_document(es, index_name, doc_id=None, updated_doc=None):
"""更新指定ID的文档"""
es.update(index=index_name, id=doc_id, body={"doc": updated_doc})
def get_doc_id(data):
@@ -122,6 +125,49 @@ def delete_by_id(doc_id):
print("删除失败:", str(e))
return False
def update_by_id(doc_id, updated_data):
"""
根据文档ID更新数据
参数:
doc_id (str): 要更新的文档ID
updated_data (dict): 更新的数据内容
返回:
bool: 更新成功返回True失败返回False
"""
try:
# 执行更新操作
es.update(index=index_name, id=doc_id, body={"doc": updated_data})
print(f"文档 {doc_id} 更新成功")
return True
except Exception as e:
print(f"更新失败: {str(e)}")
return False
def get_by_id(doc_id):
"""
根据文档ID获取单个文档
参数:
doc_id (str): 要获取的文档ID
返回:
dict or None: 成功返回文档数据失败返回None
"""
try:
# 执行获取操作
result = es.get(index=index_name, id=doc_id)
if result['found']:
return {
"_id": result['_id'],
**result['_source']
}
return None
except Exception as e:
print(f"获取文档失败: {str(e)}")
return None
def search_by_any_field(keyword):
"""全字段模糊搜索(支持拼写错误)"""
try: