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

ES入门教程

ES入门教程

1. 创建ES对象


from elasticsearch import Elasticsearch
# 实例化一个ip为localhost,端口为9200,允许超时一小时的es对象
es = Elasticsearch(hosts="localhost",port=9200,timeout=3600)
# 1. 创建 索引
index_name = "test"
es.indices.create(index=index_name)# 2. 删除 索引
es.indices.delete(index='123')# 3. 插入数据
doc = {"name": "方天", "age": "23"}
es.index(index=index_name, id=2, document=doc)# 4. 删除数据
## 4.1 es.delete  删除指定 id 数据
es.delete(index='test',id='2')# 5. 更新数据
##  5.1 es.update():更新指定字段
doc = {'name': '李邱俊','age': '20'}
es.update(index='test',id='2',doc=doc)

2. 数据查询(最重要)

1. es.search():按照指定规则查询
res = es.search(index='test', query={'match_all': {}})
print(res)

参数说明:

参数说明
index要查询的索引名称
size查询多少条数据(默认10)
from_从第几条开始查询(用于分页)
filter_path过滤返回字段,只显示指定内容
query查询规则
sort排序方式

2. 常见查询方式

✅ 2.1 查询所有数据:match_all

res = es.search(index='test', query={'match_all': {}})

✅ 2.2 模糊查询(分词):match

res = es.search(index='test', query={'match': {'name': '方'}})

✅ 2.3 短语匹配(不分词):match_phrase

res = es.search(index='test', query={'match_phrase': {'name': '方天'}})

✅ 2.4 精确查询单值:term

res = es.search(index='test', query={'term': {'name.keyword': '方天'}})

注意:如果字段是 text 类型,需要用 .keyword 进行精确匹配。


✅ 2.5 精确查询多值:terms

res = es.search(index='test', query={'terms': {'name.keyword': ['方天', '李邱俊']}})

✅ 2.6 多字段查询:multi_match

res = es.search(index='test',query={'multi_match': {'query': '方天','fields': ['name', 'age']}}
)

✅ 2.7 前缀查询:prefix

res = es.search(index='test', query={'prefix': {'name.keyword': '方'}})

✅ 2.8 通配符查询:wildcard

res = es.search(index='test', query={'wildcard': {'name.keyword': '方?天'}})
? 表示一个字符,* 表示0个或多个字符

✅ 2.9 正则查询:regexp

res = es.search(index='test', query={'regexp': {'name.keyword': '方.*'}})

✅ 2.10 多条件查询:bool

must:与(AND)

res = es.search(index='test', query={'bool': {'must': [{'match': {'name': '方天'}},{'term': {'age': '23'}}]}
})

should:或(OR)

res = es.search(index='test', query={'bool': {'should': [{'match': {'name': '方天'}},{'match': {'name': '李邱俊'}}]}
})

must_not:非(NOT)

res = es.search(index='test', query={'bool': {'must_not': [{'term': {'name.keyword': '方天'}}]}
})

✅ 2.11 存在字段查询:exists

res = es.search(index='test', query={'exists': {'field': 'age'}})

✅ 2.12 范围查询:range

res = es.search(index='test', query={'range': {'age': {'gte': 20,'lte': 30 }}
})

✅ 2.13 嵌套查询:nested

假设数据结构为:

{"name": "方天","info": {"hobby": "篮球","city": "北京"}
}

查询嵌套字段:

res = es.search(index='test', query={'nested': {'path': 'info','query': {'match': {'info.hobby': '篮球'}}}
})

3. 排序:sort

升序(asc)

res = es.search(index='test', sort={'age': {'order': 'asc'}})

降序(desc)

res = es.search(index='test', sort={'age': {'order': 'desc'}})

4. 分页查询:sizefrom_
res = es.search(index='test', size=5, from_=0)

5. 过滤返回字段:filter_path
res = es.search(index='test',filter_path=['hits.hits._source.name']
)

6. 完整示例
# 查询 name 包含“方”且 age 在 20 到 30 之间,按 age 升序排列,只返回前 5 条
res = es.search(index='test',query={'bool': {'must': [{'match': {'name': '方'}}],'filter': [{'range': {'age': {'gte': 20, 'lte': 30}}}]}},sort={'age': {'order': 'asc'}},size=5
)# 打印结果
for hit in res['hits']['hits']:print(hit['_source'])

http://www.lryc.cn/news/624595.html

相关文章:

  • Mysql实战案例 | 利用Mycat实现MYSQL的读写分离
  • Linux 服务:RAID 级别解析与 mdadm 工具实操指南
  • 【OLAP】trino安装和基本使用
  • 功能测试相关问题
  • Linux 编译器 gcc 与 g++
  • 代码随想录算法训练营四十五天|图论part03
  • llamafactory使用qlora训练
  • 无人设备遥控器之操控信号精度篇
  • unity实现背包拖拽排序
  • 【机器人-基础知识】ROS2常用命令
  • 第一阶段C#基础-15:面向对象梳理
  • 论往返之迴响:时间之织锦与信息之曼舞
  • 第三十二天(并发)
  • 如何在VS Code中使用Copilot与MCP服务器增强开发体验
  • 【C++】 using声明 与 using指示
  • 云原生Ansible渗透场景(⾃动化的运维⼯具)
  • Netty架构与组成
  • 45 C++ STL模板库14-容器6-容器适配器-优先队列(priority_queue)
  • 贪心算法(Greedy Algorithm)详解
  • 【C语言】gets和getchar的区别
  • 深度优先遍历dfs(模板)
  • 具身智能2硬件架构(人形机器人)摘自Openloong社区
  • 数据结构:查找表
  • 宏观认识 Unitree LiDAR L1 及其在自动驾驶中的应用
  • 【opencv-Python学习日记(7):图像平滑处理】
  • 阿里云odps和dataworks的区别
  • Poisson分布:稀有事件建模的理论基石与演进
  • 前端纯JS实现手绘地图 地图导引
  • YAML 语法结构速查表(完整版)
  • 【tips】unsafe-eval线上页面突然空白