安装依赖包
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