python簡單操作ES
阿新 • • 發佈:2021-12-27
建立連結
from elasticsearch import Elasticsearch import json es = Elasticsearch( ['localhost:9200'], # 啟動前嗅探es叢集伺服器 # sniff_on_start=True, # es叢集伺服器結點連線異常時是否重新整理es節點資訊 # sniff_on_connection_fail=True, # 每60秒重新整理節點資訊 # sniffer_timeout=60 )
一、插入
# 插入單條 body = { 'name': '張三View Code', 'sex': 'man', 'age': 10, 'school': '清華' } # 插入多條 bodys = [ { "name": "python", "addr": "深圳", "age": 18, "time": 12, }, { "name": "深圳", "addr": "北京", "age": 20, "time": 100, }, { "name": "c", "addr": "香港", "age": 11, "time": 1300, }, { "name": "ios", "addr": "美國", "age": 8, "time": 120, }, { "name": "呼和浩特", "addr": "澳大利亞", "age": 1, "time": 111, }, { "name": "清河", "addr": "菲律賓","age": 15, "time": 50, }, ] for body in bodys: # 增加 rs = es.index(index='index', doc_type='type', body=body)
二、查詢
# 查詢 # rs = es.get(index='index', doc_type='type', id='qCbY4H0B8m4STfI58yWM') # 只需要獲取_id資料,多個條件用逗號隔開 rs = es.search(index='index', doc_type='type', filter_path=["hits.hits._id"]) # 獲取所有資料 rs = es.search(index="index", doc_type="type", filter_path=["hits.hits._*"]) print(json.dumps(rs, indent=2, ensure_ascii=False))View Code
三、更新
# 更新 rs = es.update(index='index', doc_type='type', id='qSbY4H0B8m4STfI58yWO', body=body)
四、刪除
# 刪除 es.delete(index='index', doc_type='type', id='pCZP4H0B8m4STfI5_CU6')