当前位置: 首页 > news >正文

elasticsearch增删改查索引结构示例 - 详解

elasticsearch增删改查索引结构示例 - 详解

elasticsearch是经常用到的文档索引工具,使用方便快捷。

之前介绍了如何创建和查询索引结构

https://blog.csdn.net/liliang199/article/details/154641939

这里进一步示例在创建索引后,如何增加、删除、以及修改数据。

所用示例代码参考和修改自网络资料。

1 es索引管理

这里简单示例如何创建、和修改索引。

1.1 创建连接

首先连接es,示例如下

from elasticsearch.helpers import bulk
import elasticsearch
class ElasticSearchClient(object):@staticmethoddef get_es_servers():es_host = "http://localhost:9200"es_client = elasticsearch.Elasticsearch(hosts=es_host)return es_client
es_client = ElasticSearchClient().get_es_servers()
print(es_client.info())

输出如下

{'name': 'a2e27d00bb95', 'cluster_name': 'docker-cluster', 'cluster_uuid': 'fXhGBstXTKmI3dd0JBq_mw', 'version': {'number': '8.11.3', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '64cf052f3b56b1fd4449f5454cb88aca7e739d9a', 'build_date': '2023-12-08T11:33:53.634979452Z', 'build_snapshot': False, 'lucene_version': '9.8.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}

1.2 索引创建

创建名称为es_index_test的索引,包含document_id, title字段。

index = "es_index_test"
mapping = {"properties": {"document_id": {"type": "text"},"title": {"type": "text"},}
}
print(es_client.indices.exists(index=index))
res = es_client.indices.create(index=index,mappings=mapping
)
print(res)

输出如下

False
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'es_index_test'}

1.3 索引修改

这里在不重建索引前提下,新增content字段,示例如下。

# 添加新字段 "new_field" 为 keyword 类型
es_client.indices.put_mapping(index=index,body={"properties": {"content": {"type": "text"}}}
)
# 查看修改后的索引
res2 = es_client.indices.get(index=index)
print(res2)

输出示例如下

{'es_index_test': {'aliases': {}, 'mappings': {'properties': {'content': {'type': 'text'}, 'document_id': {'type': 'text'}, 'title': {'type': 'text'}}}, 'settings': {'index': {'routing': {'allocation': {'include': {'_tier_preference': 'data_content'}}}, 'number_of_shards': '1', 'provided_name': 'es_index_test', 'creation_date': '1764842498013', 'number_of_replicas': '1', 'uuid': 'OP22_lHLQvCidVxzPyApyA', 'version': {'created': '8500003'}}}}}

1.4 删除索引

这里示例如何删除索引

if es_client.indices.exists(index=index):print('test_index索引存在,即将删除')es_client.indices.delete(index=index)
else:print('test_index索引不存在!')

2 es数据操作

这里示例如何导入、修改和删除索引的具体数据项。

2.1 单条导入

单条数据导入示例如下

obj1 = {"document_id": "news_1","title": u"The Ten Best Science Books of 2025","content": u"In 2025, our science reporters followed the first confirmed glimpse of a colossal squid and a rare look at dinosaur blood vessels. We watched the odds of a future asteroid impact climb to higher-than-normal levels—then drop back down to zero. We parsed headlines on a blood test to detect cancer and a beloved pair of coyotes in New York City’s Central Park. Throughout it all, many of us read extended works of science nonfiction, pulling back the curtain on tuberculosis, evolution and the Arctic....",}
obj2 = {"document_id": "news_2","title": u"The 7 Most Groundbreaking NASA Discoveries of 2025","content": u"In 2025, NASA faced unprecedented uncertainty as it grappled with sweeping layoffs, looming budget cuts, and leadership switch-ups. Despite all of that, the agency somehow still managed to do some seriously astonishing science.....",}
_id1 = 1
es_client.index(index=index, body=obj1, id=_id1)
_id2 = 2
es_client.index(index=index, body=obj2, id=_id2)

输出如下

ObjectApiResponse({'_index': 'es_index_test', '_id': '2', '_version': 4, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 8, '_primary_term': 1})

2.2 批量导入

批量插入数据示例如下

from elasticsearch.helpers import bulk
def add_date_bulk(es_client, index, row_obj_list):"""批量插入ES"""load_data = []i = 1bulk_num = 2000  # 2000条为一批for row_obj in row_obj_list:action = {"_index": index,"_id": row_obj.get('_id', 'None'),"_source": {'document_id': row_obj.get('document_id', None),'title': row_obj.get('title', None),'content': row_obj.get('content', None),}}load_data.append(action)i += 1# 批量处理if len(load_data) == bulk_num:print('插入', i / bulk_num, '批数据')print(len(load_data))success, failed = bulk(es_client, load_data, index=index, raise_on_error=True)del load_data[0:len(load_data)]print(success, failed)if len(load_data) > 0:success, failed = bulk(es_client, load_data, index=index, raise_on_error=True)del load_data[0:len(load_data)]print(success, failed)
write_obj = {"_id": 1,"document_id": 1,"title": u"Elasticsearch 完全指南:原理、优势与应用场景","content": u"Elasticsearch 是一个基于 Apache Lucene 构建的开源、分布式、RESTful 搜索和分析引擎。它是 Elastic Stack(ELK Stack)的核心组件,由 Elastic 公司开发和维护。",}
row_obj_list = [write_obj]
for i in range(2, 2200):temp_obj = write_obj.copy()temp_obj["_id"] = itemp_obj["document_id"] = str(i)row_obj_list.append(temp_obj)
add_date_bulk(es_client, index, row_obj_list)

输出如下

插入 1.0005 批数据
2000
2000 []
199 []

2.3 数据修改

单条数据修改更细示例如下,需要先获取到id,然后依据id更新对应的文档。

def update_by_id(es_client, index, row_obj):"""根据给定的_id,更新ES文档:return:"""_id = row_obj.get("_id", 1)row_obj.pop("_id")es_client.update(index=index, body={"doc": row_obj}, id=_id)
row_obj = {"_id": 1,"document_id": 6,"title": u"20个必知的PyTorch概念简单解释,带你快速入门","content": u"PyTorch是当今最重要且最受欢迎的深度学习框架之一。它基于Meta的Lua语言Torch库构建,并于2017年开源。自发布以来,该库已被用于构建几乎所有重要的现代AI创新,从特斯拉的自动驾驶汽车到OpenAI的ChatGPT。本文将从基础出发,系统阐述20个最重要的概念,以深化对PyTorch的理解。",}
update_by_id(es_client, index, row_obj)

2.4 数据删除

依据id删除文档示例如下。

先获取到id,然后依据id删除对应的文档。

def delete_by_id(es_client, index, _id):"""根据给定的id,删除文档:return:"""es_client.delete(index=index, id=_id)
_id = 8
delete_by_id(es_client, index, _id)

reference

---

elasticsearch创建和查询索引结构示例

https://blog.csdn.net/liliang199/article/details/154641939

ElasticSearch 数据增删改实现 

https://www.cnblogs.com/sandea/p/9467315.html

http://www.jsqmd.com/news/205700/

相关文章:

  • 2025年杭州精装修大平层设计公司权威推荐:精装修全案设计/精装房改造/精装修全屋定制源头服务商精选 - 品牌推荐官
  • 【深度学习】YOLO实战之模型训练
  • AI Agent 时代全攻略:大模型+智能体,编程开发者的最强外挂,收藏这一篇就够了!
  • Twitter Shorts 的封面图设计吸引点击技巧是什么?
  • 机器人关节多维力试验机/传动系统总成效率试验机/制动系统总成效率试验机/传动机构运动工况模拟试验机哪个品牌更强?有没有资深采购能给点推荐? - 品牌推荐大师
  • 2026年1000元支付宝立减金回收多少,各面值价格表 - 淘淘收小程序
  • 2026执医技能通关攻略:高效工具+核心操作+避坑指南,助你一次过! - 品牌测评鉴赏家
  • CentOS 7 新磁盘LVM挂载详细步骤
  • 基于博弈与需求响应模型的光伏用户群电能共享方法探索
  • SWMM深度二次开发专题8:网络分析-最短路径查询
  • 跨境家具的海外仓安装教程广告互动形式是什么?
  • 2025年碳化硅品牌口碑榜:这些品牌为何备受青睐?磨料/不锈钢灰/棕刚玉/铬刚玉/碳化硅/黑碳化硅,碳化硅定制口碑推荐 - 品牌推荐师
  • 西门子840D HMI ADVANCED PC版:数控与PLC数据备份恢复、伺服调试、参数设定...
  • 转速恒压频比交流变频调速系统Simulink仿真
  • 点阵数码管显示屏驱动LED显示驱动芯片VK1S68C 数显驱动器原厂【FAE技术支持】
  • 安防监控视频汇聚平台EasyCVR打造出入口匝道安全畅行智慧管理方案
  • paperzz 开题报告:AI 工具如何把 “开题焦虑” 变成 “一键搞定”?
  • 程序员必看!大模型技术学习路径与实战指南,建议收藏
  • JAVA打造:同城服务预约陪诊医院陪护系统
  • centos7安装redis3.0以及phpredis扩展
  • 2026切割锯条品牌厂家TOP5权威推荐:定制实力厂商深度测评 - 工业品牌热点
  • 2026年北京配近视眼镜店服务排名,靠谱近视眼镜店服务选哪家推荐 - 工业设备
  • 找不到工作就好好学一下这份16W字Java面试合集
  • 100道软件功能测试面试题(针对刚毕业的人员)
  • Photoshop AVIF插件全面解析:开启图像压缩新纪元
  • 楼宇ICT规划实施标准:公区架构、基础设施与管理的稳定性保障
  • 2026年数控锯床供应商推荐,数控锯床靠谱生产商与不错的数控锯床工厂全解析 - 工业推荐榜
  • ComfyUI集成Z-Image全流程:可视化节点操作让AI绘画更高效
  • 超详细的常见漏洞代码审计方法,网络安全必看的零基础入门到精通教程!
  • 震惊,血的教训