Python之Elasticsearch8.2.0
目录
一、Elasticsearch
1、概念名词了解
- 非常强大的搜索引擎,便于存储和检索,可以快速存储、搜索和分析海量数据,维基百科/Stack Overflow/GitHub都采用其实现
- 一个分布式的实时文档存储库,每个字段都可以被索引与搜索
- 一个分布式的实时分析搜索引擎
- 能胜任上百个服务节点的扩展,并支持PB级别的结构化或者非结构化数据
- 分布式数据库,允许多台服务器协同工作,每台服务器均可运行多个Elasticsearch实例
节点Node
:单个Elasticsearch实例集群Cluster
:一组节点构成一个集群索引index
:Elasticsearch会索引所有字段,经过处理后写入一个反向索引(inverted index
),就相当于MongoDB/Mysql中的数据库概念,每个索引(数据库)
的名字必须小写
文档document
:索引里的单条记录称为文档,许多条文档构成一个索引,一个索引里的文档结构可以不同,但是不建议这样做类型Types
:文档可以分组,虚拟逻辑分组,用来过滤文档,类似MongoDB中的集合,MySQL中的数据表字段Fields
:每个文档类似一个JSON结构,包含很多字段,每个字段都有值,多个字段组成一个文档总结
:Elasticsearch:索引index
>类型Types
>文档document
>字段Fields
,es8.x彻底删除了type!
,es是面向文档的,es全部都是JSON,ELK是ElasticSearch, Logstash, Kibana三大开源框架首字母大写的简称
2、Elasticsearch安装
-
ElasticSearch是基于lucence开发的,也就是运行需要java jdk支持,所以要先安装JAVA环境,文档目录三JDK1.8安装 ,然后cmd窗口输入
java -version
如下展示代表java环境安装成功
-
Elasticsearch下载地址,然后解压
-
然后进入config目录下,修改如下两个文件,elasticsearch.yml修改部分配置,jvm.options修改es内存大小(添加两行这个
-Xms1g
)
-
elasticsearch.yml修改部分配置
# elasticsearch.yml文件下修改如下 cluster.name: mysy-es network.host: localhost http.port: 9200 # 是否启用ssl,若不改为false则无法连接端口,http访问 xpack.security.enabled: false
-
jvm.options修改es内存大小:添加两行这个
-Xms1g
-
然后进入bin目录下,双击执行elasticsearch.bat,稍等片刻如下加载好,注意这个cmd窗口不要关,否则es不能连接成功,除非你已配置为服务自启
-
然后打开http://localhost:9200/,出现如下界面代表安装成功
-
设置
ES_HOME
环境变量
3、ik分词插件安装
-
Elasticsearch检索功能,对于中文来说,需要安装一个分词插件
elasticsearch-analysis-ik
,注意安装版本与Elasticsearch版本一致 -
到这里下载对应安装包https://github.com/medcl/elasticsearch-analysis-ik/releases,注意下载的版本和Elasticsearch一致
-
在Elasticsearch的plugins目录下,将刚刚下载的压缩包解压到该文件夹下,并重命名为ik(貌似也可以不用重命名)
-
然后再次重启elasticsearch.bat,如图已加载
4、kibana可视化安装
- 到https://www.elastic.co/cn/downloads/kibana下载安装Kibana
- 解压后到bin目录下双击kibana.bat即可启动,然后到http://localhost:5601 就可以看到如下界面
5、Windows配置ElasticSearch服务
- 打开cmd窗口到bin目录下执行
elasticsearch-service.bat install
,然后elasticsearch-service.bat start
启动服务,我这里没试成功,所以就手动双击的bin目录下的elasticsearch.bat文件,参考文章
elasticsearch-service.bat install
: 安装Elasticsearch服务elasticsearch-service.bat remove
: 删除已安装的Elasticsearch服务(如果启动则停止服务)elasticsearch-service.bat start
: 启动Elasticsearch服务(如果已安装)elasticsearch-service.bat stop
: 停止服务(如果启动)elasticsearch-service.bat manager
:启动GUI来管理已安装的服务
二、python操作Elasticsearch
pip install elasticsearch
- 创建一个es实例,更多参数说明参考文档
from elasticsearch import Elasticsearch es = Elasticsearch(hosts=['http://localhost:9200/']).options( request_timeout=20, retry_on_timeout=True, ignore_status=[400, 404] )
- 总的代码,其中
ignore_status=[400, 404]
里面的400代表,如果索引已存在,会返回400但是不会抛出报错导致接下来代码无法运行,就是忽略索引已存在重复创建的错误;404则是忽略因索引不存在而删除失败导致程序中断的问题from elasticsearch import Elasticsearch es = Elasticsearch(hosts=['http://localhost:9200/']).options( request_timeout=20, retry_on_timeout=True, ignore_status=[400, 404] ) # 删除索引 result = es.indices.delete(index="news") print(result) # {'acknowledged': True} # 创建索引 result = es.indices.create(index="news") print(result) # {'acknowledged': True, 'shards_acknowledged': True, 'index': 'news'} # 插入数据 result = es.create(index='news', id='1', document={ "title": "你好周六"}) # 需指定id print(result) # {'_index': 'news', '_id': '1', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1} result = es.index(index='news', document={ "title": "你好周日"}) # 自动生成id print(result) # {'_index': 'news', '_id': 'zT_HwIABRhdG867DnYdw', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1} # 更新数据 result = es.index(index='news', id='1', document={ "title": "你好周日", "en": "hello zhou liu"}) print(result) # {'_index': 'news', '_id': '1', '_version': 2, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1} # 删除数据 result = es.delete(index='news', id='1') print(result) # {'_index': 'news', '_id': '1', '_version': 3, 'result': 'deleted', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 3, '_primary_term': 1}
1、创建索引
- 如创建索引news:
es.indices.create(index="news")
,然后打开http://127.0.0.1:9200/news也可看到数据from elasticsearch import Elasticsearch es = Elasticsearch(hosts=['http://localhost:9200/']).options( request_timeout=20, retry_on_timeout=True, ignore_status=[400, 404] ) result = es.indices.create(index="news") print(result) # {'acknowledged': True, 'shards_acknowledged': True, 'index': 'news'}
2、删除索引
- 删除索引news:
es.indices.delete(index="news")
# 删除索引 result = es.indices.delete(index="news") print(result) # {'acknowledged': True}
3、新增数据
- 插入数据有两种
es.create
和es.index
, create方法需要指定id,index自动生成id,返回结果部分内容:‘_version’: 1, ‘result’: ‘created’result = es.create(index='news', id='1', document={ "title": "你好周六"}) # 需指定id print(result) # {'_index': 'news', '_id': '1', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1} result = es.index(index='news', document={ "title": "你好周日"}) # 自动生成id print(result) # {'_index': 'news', '_id': 'zT_HwIABRhdG867DnYdw', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}
4、更新数据
- 需指定更新的id,
es.index
既可以插入数据也可以更新数据,其中返回的结果里面有个_version
字段,代表版本号每次更新都会加1,返回结果部分内容:‘_version’: 2, ‘result’: ‘updated’result = es.index(index='news', id='1', document={ "title": "你好周日", "en": "hello zhou liu"}) print(result) # {'_index': 'news', '_id': '1', '_version': 2, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}
5、删除数据
- 需指定删除的id,
es.delete
删除数据,返回结果部分内容:‘_version’: 3, ‘result’: ‘deleted’,result = es.delete(index='news', id='1') print(result) # {'_index': 'news', '_id': '1', '_version': 3, 'result': 'deleted', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 3, '_primary_term': 1}
6、查询数据
es.search
查询数据 ,更多查询使用from elasticsearch import Elasticsearch es = Elasticsearch(hosts=['http://localhost:9200/']).options( request_timeout=20, retry_on_timeout=True, ignore_status=[400, 404] ) properties = { "title": { 'type': 'text'} } # es.indices.delete(index="news") result = es.indices.put_mapping(index='news', properties=properties) print(result) # 插入数据 datas = [ { 'title': '美国留给伊拉克的是个烂摊子吗', 'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm', 'date': '2011-12-16'}, { 'title': '公安部:各地校车将享最高路权', 'url': 'http://www.chinanews.com/gn/2011/12-16/3536077.shtml', 'date': '2011-12-16'}, { 'title': '中韩渔警冲突调查:韩警平均每天扣1艘中国渔船', 'url': 'https://news.qq.com/a/20111216/001044.htm', 'date': '2011-12-17'}, { 'title': '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首', 'url': 'http://news.ifeng.com/world/detail_2011_12/16/11372558_0.shtml', 'date': '2011-12-18'} ] for data in datas: es.index(index='news', document=data) # 查询1 result = es.search(index='news') print(result) # 查询2 query = { 'match': { 'title': '平均' } } result = es.search(index='news', query=query) print(result)
作者:十一姐
来源链接:https://blog.csdn.net/weixin_43411585/article/details/124763838
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。