一、列出全部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包的添加解決方案]