当前位置: 首页 > 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.dtcms.com/a/337331.html

相关文章:

  • CSDN转PDF【无水印且免费!!!】
  • linux 内核 - 进程地址空间的数据结构
  • 【STM32】STM32H750 CubeMX 配置 USB CDC 虚拟串口笔记
  • 保姆级教学:使用 Jenkins 部署前端项目(2025 年最新版)
  • 基于JS实现的中国象棋AI系统:多模块协同决策与分析
  • ffmpeg编译
  • 音视频面试题集锦第 26 期
  • 计算机网络-IPv6
  • 679. 24 点游戏
  • Android Cutout(屏幕挖孔)详解
  • ubuntu 编译ffmpeg6.1 增加drawtext,libx264,libx265等
  • Leetcode 3648. Minimum Sensors to Cover Grid
  • OCR库pytesseract安装保姆级教程
  • LeetCode:无重复字符的最长子串
  • SQLite 加密与不加密性能对比与优化实践
  • Opsqueue:为重负载而生的轻量级批处理队列,已开源!
  • 视频因为264问题无法网页播放,解决方案之一:转化视频
  • 智创飞跃|2025 Google 开发者大会伴你成长精进
  • 兴趣爱好——虾哥开源小智AI机器人搭建(丐版—最低成本)ESP32开发板 MicroPython V1.0.0 Rev1
  • 嵌入式Linux学习 -- 进程和线程4
  • 三高架构杂谈
  • Ansible 自动化运维实践笔记:Jinja2 模板、LNMP+WordPress 部署与大项目管理
  • 飞算JavaAI智慧校园场景实践:从校园管理到师生服务的全链路技术革新
  • 【C++✨】多种 C++ 解法固定宽度右对齐输出(每个数占 8 列)
  • 常见的光源频闪控制方式
  • GitHub 热榜项目 - 日榜(2025-08-18)
  • 为什么有些相机“即插即用”,而有些则需要采集卡?
  • 联动无影(TscanPlus)送激活码
  • 短剧小程序系统开发:推动短剧行业规范化与标准化发展
  • 【计算机网络】TCP/IP