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

Python操作ES集群API(增删改查等)

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

学习B站博主教程笔记: 

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

1、创建索引

 #!/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、写入单个文档

 #!/usr/bin/env python3​import sysfrom elasticsearch import Elasticsearch​# 设置字符集,兼容Python2reload(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、写入多个文档

 #!/usr/bin/env python3​import sysfrom elasticsearch import Elasticsearchfrom elasticsearch.helpers import bulk​# 设置字符集,兼容Python2reload(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、全量查询

 #!/usr/bin/env python3​from elasticsearch import Elasticsearches = 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、查看多个文档

 #!/usr/bin/env python3​import sysfrom elasticsearch import Elasticsearch​# 设置字符集,兼容Python2reload(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查询

 #!/usr/bin/env python3​import sysfrom elasticsearch import Elasticsearch​# 设置字符集,兼容Python2reload(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、查看索引是否存在

 #!/usr/bin/env python3​from elasticsearch import Elasticsearches = 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、修改文档

 #!/usr/bin/env python3​from elasticsearch import Elasticsearches = 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、删除单个文档

 #!/usr/bin/env python3​from elasticsearch import Elasticsearches = 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、删除索引

 #!/usr/bin/env python3​from elasticsearch import Elasticsearches = 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()

致谢

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

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 民生水暖工程背后的科技力量引领工程智能化转型
  • 使用FastJson2将对象转成JSON字符串时,小数转换出错
  • RedissonClient 分布式队列工具类
  • 智能客服的演变:从传统到向量数据库的新时代
  • [iBOT] Image BERT Pre-Training with Online Tokenizer
  • springboot高校实验室预约系统-计算机毕业设计源码58031
  • 无需温度修正,测值准确可靠 GEO ACxxxx型振弦式锚索测力计
  • 机器学习特征分析
  • macos安装ArgoCD
  • Docker 学习 Day 1
  • 鸿蒙开发(API 12 Beta6版)【通用属性协议】 网络篇
  • 十分钟学会Kubernetes(K8S) 部署SpringBoot3.0
  • Java语言程序设计基础篇_编程练习题**17.20 (二进制编辑器)
  • 系统编程-多路IO复用
  • NLP自然语言处理学习过程中知识点总结
  • @angular/forms 源码解析之双向绑定
  • 0基础学习移动端适配
  • 2017届校招提前批面试回顾
  • angular学习第一篇-----环境搭建
  • C++11: atomic 头文件
  • const let
  • Cookie 在前端中的实践
  • Git 使用集
  • JS+CSS实现数字滚动
  • MySQL-事务管理(基础)
  • Promise初体验
  • Rancher如何对接Ceph-RBD块存储
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • v-if和v-for连用出现的问题
  • vue总结
  • 从输入URL到页面加载发生了什么
  • 大主子表关联的性能优化方法
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 你不可错过的前端面试题(一)
  • 入口文件开始,分析Vue源码实现
  • 微信小程序实战练习(仿五洲到家微信版)
  • 微信支付JSAPI,实测!终极方案
  • 我建了一个叫Hello World的项目
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (2022 CVPR) Unbiased Teacher v2
  • (39)STM32——FLASH闪存
  • (WSI分类)WSI分类文献小综述 2024
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (第二周)效能测试
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (理论篇)httpmoudle和httphandler一览
  • (三)终结任务
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (转)jdk与jre的区别
  • (转)Windows2003安全设置/维护