通過Python庫elasticsearch_dsl處理elasticsearch

ElasticSearch與數據庫的對應關係

ES RDBS
index database
type table
filed column

通過Python庫elasticsearch_dsl處理ElasticSearch

添加連接

from elasticsearch_dsl import connections
connections.create_connection(hosts=['127.0.0.1:9200'], timeout=20)

獲取index(database)

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch('127.0.0.1:9200')

indexs = client.indices.get('*') # 獲取所有的index

indexnames = indexs.keys()

獲取type(table)

index = indexnames[0]
tables = indexs[index]['mappings'].keys

獲取所有的字段(columns)

table = tables[0]
colunms = inidexs[table]['properties'].keys()

數據查詢

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch('10.0.4.122:9200')
s = Search().using(client).query("match", account_number=5)
response = s.execute()

result = []
for row in response.hits:
    # print row.to_dict()
    result.append(row.to_dict())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章