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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步