Python操作ES集群API

前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除

学习B站博主教程笔记:

最新版适合自学的ElasticStack全套视频(Elk零基础入门到精通教程)Linux运维必备---ElasticSearch+Logstash+Kibana精讲_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1VMW3e6Ezk/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=e539f90574cdb0bc2bc30a8b5cb3fc00

1、创建索引

cs 复制代码
 #!/usr/bin/env python3
 ​
 from elasticsearch import Elasticsearch
 ​
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 msg_body = {
     "settings": {
         "index": {
             "number_of_replicas": "0",
             "number_of_shards": "5"
         }
     },
     "mappings": {
         "properties": {
             "ip_addr": {
                 "type": "ip"
             },
             "name": {
                 "type": "text"
             },
             "id": {
                 "type": "long"
             },
             "hobby": {
                 "type": "text"
             },
             "email": {
                 "type": "keyword"
             }
         }
     },
     "aliases": {
         "cluster-elstaicstack-linux-python": {},
         "cluster-linux-python": {}
     }
 }
 ​
 result = es.indices.create(index="cluster-linux-2024", body=msg_body)
 print(result)
 ​
 es.close()

2、写入单个文档

cs 复制代码
 #!/usr/bin/env python3
 ​
 import sys
 from elasticsearch import Elasticsearch
 ​
 # 设置字符集,兼容Python2
 reload(sys)
 sys.setdefaultencoding('utf-8')
 ​
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # 写入单个文档
 msg_body = {
     "name": "Jason Yin",
     "ip_addr": "120.53.104.136",
     "blog": "https://blog.yinzhengjie.com/",
     "hobby": ["k8s", "docker", "elk"],
     "email": "yinzhengjie@qq.com",
     "id": 10086,
 }
 ​
 ​
 result = es.index(index="cluster-linux-2024", doc_type="_doc", body=msg_body)
 print(result)
 ​
 es.close()

3、写入多个文档

cs 复制代码
 #!/usr/bin/env python3
 ​
 import sys
 from elasticsearch import Elasticsearch
 from elasticsearch.helpers import bulk
 ​
 # 设置字符集,兼容Python2
 reload(sys)
 sys.setdefaultencoding('utf-8')
 ​
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # 批量写入多个文档
 doc2 = {
     "id": 10010,
     "name": "老男孩",
     "age": 45,
     "hobby": ["下棋", "抖音", "思想课"],
     "ip_addr": "10.0.0.101",
     "email": "oldboy@qq.com"
 }
 ​
 doc3 = {
     "id": 10011,
     "name": "李导",
     "age": 32,
     "hobby": ["三剑客", "打枪"],
     "email": "lidao@qq.com",
     "ip_addr": "10.0.0.201"
 }
 ​
 many_doc = [doc2, doc3]
 ​
 write_number, _ = bulk(es, many_doc, index="cluster-linux-2024")
 print(write_number)
 ​
 es.close()

4、全量查询

cs 复制代码
 #!/usr/bin/env python3
 ​
 from elasticsearch import Elasticsearch
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # 全量查询
 result = es.search(index="cluster-linux-2024")
 print(result)
 print(result["hits"])
 print(result["hits"]["hits"])
 print(result["hits"]["hits"][0]["_source"])
 print(result["hits"]["hits"][0]["_source"]["name"])
 print(result["hits"]["hits"][0]["_source"]["hobby"])
 ​
 es.close()

5、查看多个文档

cs 复制代码
 #!/usr/bin/env python3
 ​
 import sys
 from elasticsearch import Elasticsearch
 ​
 # 设置字符集,兼容Python2
 reload(sys)
 sys.setdefaultencoding('utf-8')
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # 获取多个文档
 doc1 = {'ids': ["5gIk24AB2f3QZVpX1AxN", "5AIk24AB2f3QZVpX1AxN"]}
 res = es.mget(index="cluster-linux-2024", body=doc1)
 print(res)
 print(res['docs'])
 ​
 es.close()

6、DSL查询

cs 复制代码
 #!/usr/bin/env python3
 ​
 import sys
 from elasticsearch import Elasticsearch
 ​
 # 设置字符集,兼容Python2
 reload(sys)
 sys.setdefaultencoding('utf-8')
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # DSL语句查询
 dsl = {
     "query": {
         "match": {
             "hobby": "王岩"
         }
     }
 ​
 }
 ​
 # DSL语句查询
 # dsl= {
 #     "query": {
 #         "bool": {
 #             "should": [
 #                 {
 #                     "match": {
 #                         "type": "pets"
 #                     }
 #                 },
 #                 {
 #                     "match": {
 #                         "type": "lunxury"
 #                     }
 #                 }
 #             ],
 #             "minimum_should_match": 1,
 #             "filter": {
 #                 "range": {
 #                     "price": {
 #                         "gt": 1500,
 #                         "lt": 2500
 #                     }
 #                 }
 #             }
 #         }
 #     },
 #     "sort": {
 #         "price": {
 #             "order": "desc"
 #         }
 #     },
 #     "_source": [
 #         "title",
 #         "price",
 #         "producer"
 #     ]
 # }
 #
 # res = es.search(index="shopping",body=dsl)
 # print(res)
 res = es.search(index="cluster-linux-2024", body=dsl)
 print(res)
 ​
 es.close()

7、查看索引是否存在

cs 复制代码
 #!/usr/bin/env python3
 ​
 from elasticsearch import Elasticsearch
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # 判断索引是否存在
 print(es.indices.exists(index="cluster-shopping"))
 ​
 es.close()

8、修改文档

cs 复制代码
 #!/usr/bin/env python3
 ​
 from elasticsearch import Elasticsearch
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 new_doc = {'doc': {"hobby": ['下棋', '抖音', '思想课', "Linux运维"], 'address': '中华人民共和国北京市昌平区沙河镇老男孩教育'}}
 ​
 # 更新文档
 res = es.update(index="cluster-linux-2024", id='5gIk24AB2f3QZVpX1AxN', body=new_doc)
 print(res)
 ​
 es.close()

9、删除单个文档

cs 复制代码
 #!/usr/bin/env python3
 ​
 from elasticsearch import Elasticsearch
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 ​
 # 删除单个文档
 result = es.delete(index="cluster-linux-2024", id="5gIk24AB2f3QZVpX1AxN")
 print(result)
 ​
 es.close()

10、删除索引

cs 复制代码
 #!/usr/bin/env python3
 ​
 from elasticsearch import Elasticsearch
 es = Elasticsearch(['192.168.1.10:9200', '192.168.1.11:9200', '192.168.1.12:9200'])
 # 删除索引
 result = es.indices.delete(index="cluster-linux-2024")
 print(result)
 ​
 es.close()

致谢

在此,我要对所有为知识共享做出贡献的个人和机构表示最深切的感谢。同时也感谢每一位花时间阅读这篇文章的读者,如果文章中有任何错误,欢迎留言指正。

学习永无止境,让我们共同进步!!

相关推荐
寂然如故2 小时前
Anaconda 安装
python
zhangbin_2372 小时前
【Python机器学习】NLP信息提取——命名实体与关系
开发语言·人工智能·python·深度学习·机器学习·自然语言处理
985小水博一枚呀3 小时前
【梯度消失|梯度爆炸】Vanishing Gradient|Exploding Gradient——为什么我的卷积神经网络会不好呢?
人工智能·python·深度学习·神经网络·计算机视觉·cnn·numpy
全能全知者4 小时前
不废话简单易懂的Selenium 页面操作与切换
python·selenium·测试工具·网络爬虫
你可以自己看5 小时前
python的基础语法
开发语言·python
akhfuiigabv7 小时前
使用Neo4j-Cypher-FT实现自然语言查询图数据库
数据库·python·oracle·neo4j
繁依Fanyi7 小时前
828华为云征文|华为Flexus云服务器搭建OnlyOffice私有化在线办公套件
服务器·开发语言·前端·python·算法·华为·华为云
zhangfeng11337 小时前
在 PyTorch 中,除了 pad_sequence 还有哪些其他处理序列数据的函数?时间序列数据 预处理
人工智能·pytorch·python·深度学习
python1567 小时前
Python Numpy布尔数组在数据分析中的应用
python·数据分析·numpy
AIAdvocate8 小时前
力扣-96.不同的二叉搜索树 题目详解
python·算法·动态规划