1.Python模塊的安裝
pip install elasticsearch
2.Python 連接ElasticSearch
from elasticsearch import Elasticsearch
# es = Elasticsearch() # 默認連接本地elasticsearch
# es = Elasticsearch(['127.0.0.1:9200']) # 連接本地9200端口
es = Elasticsearch(
["192.168.1.10", "192.168.1.11", "192.168.1.12"], # 連接集羣,以列表的形式存放各節點的IP地址
sniff_on_start=True, # 連接前測試
sniff_on_connection_fail=True, # 節點無響應時刷新節點
sniff_timeout=60 # 設置超時時間
)
3.配置忽略響應的狀態碼
es = Elasticsearch(['127.0.0.1:9200'],ignore=400) # 忽略返回的400狀態碼
es = Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502]) # 以列表的形式忽略多個狀態碼
4.es連接對象的操作
- es.index, 向指定索引添加或者更新文檔,如果索引不存在,首先會創建改索引,然後再執行添加或者更新操作。
print(es.index(index='w2', doc_type='doc', id='4', body={"name":"可可", "age": 18})) # 正常
print(es.index(index='w2', doc_type='doc', id=5, body={"name":"卡卡西", "age":22})) # 正常
print(es.index(index='w2', doc_type='doc', body={"name": "鳴人", "age": 22})) # 可以不指定id,默認生成一個id
- es.get,查詢索引中指定文檔
print(es.get(index='w2', doc_type='doc', id=5))
- es.get_source,通過索引類型和ID獲取文檔的來源,直接返回需要的字典
print(es.get_source(index='py3', doc_type='doc', id='1')) # {'name': '王五', 'age': 19}
- es.search,執行搜索查詢並獲取與查詢匹配的搜索匹配。這個用的最多,可以跟複雜的查詢條件。
- index: 要搜索的以逗號分隔的索引名稱列表; 使用_all 或空字符串對所有索引執行操作。
- doc_type: 要搜索的以逗號分隔的文檔類型列表; 留空以對所有類型執行操作。
- body: 使用Query DSL(QueryDomain Specific Language查詢表達式)的搜索定義。
- _source: 返回_source字段的true或false,或返回的字段列表,返回指定字段。
- _source_exclude:要從返回的_source字段中排除的字段列表,返回的所有字段中,排除哪些字段。
- _source_include:從_source字段中提取和返回的字段列表,跟_source差不多。
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}})) # 一般查詢
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source=['name', 'age'])) # 結果字段過濾
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source_exclude =[ 'age']))
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source_include =[ 'age']))
- es.count ,執行查詢獲取查詢到的數量
body = {
"query": {
"match": {
"age": 18
}
}
}
print(es.count(index='py2', doc_type='doc', body=body)) # {'count': 1, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='py2', doc_type='doc', body=body)['count']) # 1
print(es.count(index='w2')) # {'count': 6, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='w2', doc_type='doc')) # {'count': 6, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
- es.create,添加一條數據,索引不存在的話創建索引添加數據
print(es.create(index='py3', doc_type='doc', id='1', body={"name": '王五', "age": 20}))
print(es.get(index='py3', doc_type='doc', id='3'))
- es.delete,刪除指定的文檔
print(es.delete(index='py3', doc_type='doc', id='4'))
- es.delete_by_query,刪除與查詢匹配的所有文檔。
print(es.delete_by_query(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}}))
- es.exists,查詢elasticsearch中是否存在指定的文檔,返回一個布爾值。
print(es.exists(index='py3', doc_type='doc', id='1'))
- es.info,獲取當前集羣的基本信息。
- es.ping,如果羣集已啓動,則返回True,否則返回False。
5. es對象的Indices 創建索引,定義mapping
- es.indices.create,在Elasticsearch中創建索引,用的最多。
body = {
"mappings": {
"doc": {
"dynamic": "strict", # 嚴格的格式
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"url": {
"type": "text"
},
"action_type": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
}
}
es.indices.create('py4', body=body) # 給索引py4添加索引的映射信息
- es.indices.analyze,返回分詞結果。
es.indices.analyze(body={'analyzer': "ik_max_word", "text": "皮特和茱麗當選“年度模範情侶”Brad Pitt and Angelina Jolie"})
- es.indices.delete,在Elasticsearch中刪除索引。
print(es.indices.delete(index='py4'))
print(es.indices.delete(index='w3')) # {'acknowledged': True}
- es.indices.put_alias,爲一個或多個索引創建別名,查詢多個索引的時候,可以使用這個別名。
- index 別名應指向的逗號分隔的索引名稱列表(支持通配符),使用_all對所有索引執行操作。
- name要創建或更新的別名的名稱。
- body別名的設置,例如路由或過濾器。
print(es.indices.put_alias(index='py4', name='py4_alias')) # 爲單個索引創建別名
print(es.indices.put_alias(index=['py3', 'py2'], name='py23_alias')) # 爲多個索引創建同一個別名,聯查用
- es.indices.delete_alias,刪除一個或多個別名。
print(es.indices.delete_alias(index='alias1'))
print(es.indices.delete_alias(index=['alias1, alias2']))
- es.indices.get_mapping,檢索索引或索引/類型的映射定義。
print(es.indices.get_mapping(index='py4'))
- es.indices.get_settings,檢索一個或多個(或所有)索引的設置。
print(es.indices.get_settings(index='py4'))
- es.indices.get,允許檢索有關一個或多個索引的信息。
print(es.indices.get(index='py2')) # 查詢指定索引是否存在
print(es.indices.get(index=['py2', 'py3']))
- es.indices.get_alias,檢索一個或多個別名。
print(es.indices.get_alias(index='py2'))
print(es.indices.get_alias(index=['py2', 'py3']))
- es.indices.get_field_mapping,檢索特定字段的映射信息。
print(es.indices.get_field_mapping(fields='url', index='py4', doc_type='doc'))
print(es.indices.get_field_mapping(fields=['url', 'title'], index='py4', doc_type='doc'))
5.Cluster(集羣相關)
- es.cluster.get_settigns,獲取集羣設置。
print(es.cluster.get_settings())
- es.cluster.health,獲取有關羣集運行狀況的非常簡單的狀態。
print(es.cluster.health())
- es.cluster.state,獲取整個集羣的綜合狀態信息。
print(es.cluster.state())
- es.cluster.stats,返回羣集的當前節點的信息。
print(es.cluster.stats())
6.Node (節點相關)
- es.nodes.info,返回集羣中節點的信息。
print(es.nodes.info()) # 返回所節點
print(es.nodes.info(node_id='node1')) # 指定一個節點
print(es.nodes.info(node_id=['node1', 'node2'])) # 指定多個節點列表
- es.nodes.stats,獲取集羣中節點統計信息。
print(es.nodes.stats())
print(es.nodes.stats(node_id='node1'))
print(es.nodes.stats(node_id=['node1', 'node2']))
- es.nodes.hot_threads,獲取指定節點的線程信息。
print(es.nodes.hot_threads(node_id='node1'))
print(es.nodes.hot_threads(node_id=['node1', 'node2']))
- es.nodes.usage,獲取集羣中節點的功能使用信息。
print(es.nodes.usage())
print(es.nodes.usage(node_id='node1'))
print(es.nodes.usage(node_id=['node1', 'node2']))
7.Cat(查詢)
- es.cat.allocation,返回分片使用情況。
print(es.cat.allocation())
print(es.cat.allocation(node_id=['node1']))
print(es.cat.allocation(node_id=['node1', 'node2'], format='json'))
- es.cat.count,Count提供對整個羣集或單個索引的文檔計數的快速訪問。
print(es.cat.count()) # 集羣內的文檔總數
print(es.cat.count(index='py3')) # 指定索引文檔總數
print(es.cat.count(index=['py3', 'py2'], format='json')) # 返回兩個索引文檔和
- es.cat.health,從集羣中health裏面過濾出簡潔的集羣健康信息。
- es.cat.indices,返回索引的信息;也可以使用此命令進行查詢集羣中有多少索引。
- es.cat.master,返回集羣中主節點的IP,綁定IP和節點名稱。
- es.cat.plugins,返回節點的插件信息。
- es.cat.shards,返回哪個節點包含哪些分片的信息。