一、列出全部index:html
http://localhost/_cat/indices?v
相应Python部分:python
es = Elasticsearch(hosts="localhost") print es.cat.indices(params={'v': 'true'})
运行结果:sql
D:\Anaconda2\python.exe D:/!Python/ElasticSearch/elastic_search_test.py health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open my-index3 LxaaZPHeQ82AdhtQ0lx94g 5 1 3 0 12.6kb 12.6kb yellow open my-index4 NznoQ8muQciDQbvrTkszvw 5 1 21 0 37.7kb 37.7kb yellow open my-index 0OzNbpltQhynWRb8MZ9bGw 5 1 1 0 5kb 5kb yellow open my-index2 j6hXpBDEQfaqQQ_M-5VH4Q 5 1 2 0 8.8kb 8.8kb
咱们会发现,在Python打开es后,RESTful对应的_cat是es的一个属性,而indices则是python中的一个方法,参数v单独指定,输入为字符串的'true',而不是True。json
二、列出index中的数据type(假设ES开了不一样的端口,好比说8086):bash
http://localhost:8086/_mapping
对应的Python代码为:restful
from elasticsearch import Elasticsearch es = Elasticsearch(hosts="localhost:8086") result_dict = es.indices.get_mapping() for key in result_dict.keys(): print key, '------', result_dict[key]
输出结果为:app
PYTHON结果elasticsearch
三、列出一个index的总条数(count)ui
http://roc-3:8088/test-index/person_doc_type/_count
获得结果为:spa
{"count":28,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0}}
四、转化任意一个restful请求到python代码:
result = es.transport.perform_request('POST', '/_xpack/sql', params={'format': 'txt'}, body={"query": "SELECT * FROM \"my-index4\" LIMIT 10"})
五、查询,显示特定列的内容
下列SQL对应的ES查询为
SELECT name,tel FROM index.doctype1;
http://localhost:9200/index1/doctype1/_search?pretty&_source=name,tel
pretty意为格式化json使其工整
es = Elasticsearch(hosts="http://localhost:9200") result = es.search('index1', 'doctype1', params={'_source': 'name,tel'}) print result
es = Elasticsearch(hosts="http://localhost:9200") result = es.transport.perform_request('GET', '/index1/doctype1/_search', params={'_source': 'name,tel'}) print result
reference: [Spark应用依赖jar包的添加解决方案]