Python 3.8
Elasticsearch 7.5
VirtaulBox 虚拟机客户端CentOs7
参考文档: https://elasticsearch-py.readthedocs.io/en/v7.12.1/transports.html
pip install elasticsearch
# -*- coding=utf8 -*- from elasticsearch import Elasticsearch import json es = Elasticsearch(hosts="192.168.0.102", port=9200)
三.API详解
第一种: 获取索引信息
(a) 方法: cat
(b) 代码示例:es.cat.indices(index=None)
© 代码:
# index= None:表示查询所有的索引, 如果传入单个索引名表示查询单个索引信息 print(es.cat.indices(index=None)) # print(es.cat.indices(index='bark')) # yellow open bark NZYr9-8jSM-uoYvIHiYe6g 1 1 150 0 55.4kb 55.4kb
结果:
green open .kibana_7.12.1_001 OJwFwVauSTWcL_RI7PLdZA 1 0 68 10 2.1mb 2.1mb green open .apm-custom-link ZiM3n3fWR0icqm3FO9RKhg 1 0 0 0 208b 208b yellow open bark NZYr9-8jSM-uoYvIHiYe6g 1 1 150 0 55.4kb 55.4kb green open .apm-agent-configuration Z9NrQV5dRuuO84bPYEUQyA 1 0 0 0 208b 208b green open .kibana_task_manager_7.12.1_001 HD8P3_JsRmWKsVhzZyf26g 1 0 9 1470 221.9kb 221.9kb green open .kibana-event-log-7.12.1-000001 OcqhJ8bcTluGbtcnglVVUw 1 0 2 0 11kb 11kb green open .kibana-event-log-7.12.1-000002 9aaYdlYpSnK3Ih_8qWgfDA 1 0 1 0 5.6kb 5.6kb green open .tasks KknWSFFbRxubG5TzdX-pzg 1 0 2 0 7.8kb 7.8kb yellow open customer BskrxEfmTt6wEjfNVASyDg 1 1 1 0 3.8kb 3.8kb
第二种:获取索引信息
indices.get_mapping
es.indices.get_mapping(index=None,doc_type=None,params=None)
# -*- coding=utf8 -*- from elasticsearch import Elasticsearch import json # 连接es服务端,配置端口 es = Elasticsearch(hosts="192.168.0.102", port=9200) # 获取所以方法: # 参数为: 获取哪个索引就获取哪个索引的信息, 不传代码获取全部的索引信息. get= es.indices.get_mapping(index=None,doc_type=None,params=None) # 获取指定的索引. #get= es.indices.get_mapping(index="bark",doc_type=None,params=None) # 返回结果是dict格式,通过json转成JSON格式 dumps = json.dumps(get) print(dumps)
© 返回的结果,与postman调用查询所有的索引的结果一致
删除索引:
方法: es.indices.delete(index=indexName)
代码:
delete= es.indices.delete(index='test01') print(delete)
返回结果:
{'acknowledged': True}