• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MC_Hotdog
Yeah, you're right I'm still riding that crappy bike
博客园    首页    新随笔    联系   管理    订阅  订阅

Python连接Elasticsearch

安装依赖包

1 pip3 install -i https://pypi.douban.com/simple elasticsearch

Python连接elasticsearch

 1 from elasticsearch import Elasticsearch
 2 
 3 # es = Elasticsearch()
 4 # es = Elasticsearch(['127.0.0.1:9200'])
 5 es = Elasticsearch(
 6     ['192.168.10.10', '192.168.10.11', '192.168.10.12'],  # 连接集群,以列表的形式存放节点的ip地址
 7     sniff_on_start=True,  # 连接前测试
 8     sniff_on_connection_fail=True,  # 节点无响应时刷新节点
 9     sniff_timeout=60    # 设置超时时间
10 )

配置忽略响应状态码

1 es = Elasticsearch(['127.0.0.1:9200'],ignore=400)  # 忽略返回的400状态码
2 es = Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502])  # 以列表的形式忽略多个状态码

 

简单的示例

1 from elasticsearch import Elasticsearch
2 
3 es = Elasticsearch()
4 
5 print(es.index(index="p1", doc_type="doc", id=1, body={"name": "成都"}))
6 print(es.index(index="p1", doc_type="doc", id=2, body={"name": "绵阳"}))
7 print(es.get(index="p1", doc_type="doc", id=1))
8 print(es.delete(index="p1", doc_type="doc", id=2))

 

了解更多https://elasticsearch-py.readthedocs.io/en/master/api.html 

 

posted @ 2019-08-23 22:35  MC_Hotdog  阅读(3653)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3