ElasticSearch學習系列

ElasticSearch學習系列 - (3) Python操作es

1.Python模塊的安裝

<code>pip install elasticsearch/<code>

2.Python 連接ElasticSearch

<code>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 # 設置超時時間
)/<code>

3.配置忽略響應的狀態碼

<code>es = Elasticsearch(['127.0.0.1:9200'],ignore=400)  # 忽略返回的400狀態碼
es = Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502]) # 以列表的形式忽略多個狀態碼/<code>

4.es連接對象的操作

  • es.index, 向指定索引添加或者更新文檔,如果索引不存在,首先會創建改索引,然後再執行添加或者更新操作。
<code>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/<code>
  • es.get,查詢索引中指定文檔
<code>print(es.get(index='w2', doc_type='doc', id=5))/<code>
  • es.get_source,通過索引類型和ID獲取文檔的來源,直接返回需要的字典
<code>print(es.get_source(index='py3', doc_type='doc', id='1'))  # {'name': '王五', 'age': 19}/<code>

es.search,執行搜索查詢並獲取與查詢匹配的搜索匹配。這個用的最多,可以跟複雜的查詢條件。

  • index: 要搜索的以逗號分隔的索引名稱列表; 使用_all 或空字符串對所有索引執行操作。
  • doc_type: 要搜索的以逗號分隔的文檔類型列表; 留空以對所有類型執行操作。
  • body: 使用Query DSL(QueryDomain Specific Language查詢表達式)的搜索定義。
  • _source: 返回_source字段的true或false,或返回的字段列表,返回指定字段。
  • _source_exclude:要從返回的_source字段中排除的字段列表,返回的所有字段中,排除哪些字段。
  • _source_include:從_source字段中提取和返回的字段列表,跟_source差不多。
<code>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']))/<code>
  • es.count ,執行查詢獲取查詢到的數量
<code>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}}/<code>
  • es.create,添加一條數據,索引不存在的話創建索引添加數據
<code>print(es.create(index='py3', doc_type='doc', id='1', body={"name": '王五', "age": 20}))

print(es.get(index='py3', doc_type='doc', id='3'))/<code>
  • es.delete,刪除指定的文檔
<code>print(es.delete(index='py3', doc_type='doc', id='4'))/<code>
  • es.delete_by_query,刪除與查詢匹配的所有文檔。
<code>print(es.delete_by_query(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}}))/<code>
  • es.exists,查詢elasticsearch中是否存在指定的文檔,返回一個布爾值。
<code>print(es.exists(index='py3', doc_type='doc', id='1'))/<code>
  • es.info,獲取當前集群的基本信息。
  • es.ping,如果群集已啟動,則返回True,否則返回False。

5. es對象的Indices 創建索引,定義mapping

  • es.indices.create,在Elasticsearch中創建索引,用的最多。
<code>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添加索引的映射信息/<code>
  • es.indices.analyze,返回分詞結果。
<code>es.indices.analyze(body={'analyzer': "ik_max_word", "text": "皮特和茱麗當選“年度模範情侶”Brad Pitt and Angelina Jolie"})/<code>
  • es.indices.delete,在Elasticsearch中刪除索引。
<code>print(es.indices.delete(index='py4'))
print(es.indices.delete(index='w3')) # {'acknowledged': True}/<code>
  • es.indices.put_alias,為一個或多個索引創建別名,查詢多個索引的時候,可以使用這個別名。

index 別名應指向的逗號分隔的索引名稱列表(支持通配符),使用_all對所有索引執行操作。

name要創建或更新的別名的名稱。

body別名的設置,例如路由或過濾器。

<code>print(es.indices.put_alias(index='py4', name='py4_alias'))  # 為單個索引創建別名
print(es.indices.put_alias(index=['py3', 'py2'], name='py23_alias')) # 為多個索引創建同一個別名,聯查用/<code>
  • es.indices.delete_alias,刪除一個或多個別名。
<code>print(es.indices.delete_alias(index='alias1'))
print(es.indices.delete_alias(index=['alias1, alias2']))/<code>
  • es.indices.get_mapping,檢索索引或索引/類型的映射定義。
<code>print(es.indices.get_mapping(index='py4'))/<code>
  • es.indices.get_settings,檢索一個或多個(或所有)索引的設置。
<code>print(es.indices.get_settings(index='py4'))/<code>
  • es.indices.get,允許檢索有關一個或多個索引的信息。
<code>print(es.indices.get(index='py2'))    # 查詢指定索引是否存在
print(es.indices.get(index=['py2', 'py3']))/<code>
  • es.indices.get_alias,檢索一個或多個別名。
<code>print(es.indices.get_alias(index='py2'))
print(es.indices.get_alias(index=['py2', 'py3']))/<code>
  • es.indices.get_field_mapping,檢索特定字段的映射信息。
<code>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'))/<code>

5.Cluster(集群相關)

  • es.cluster.get_settigns,獲取集群設置。
<code>print(es.cluster.get_settings())/<code>
  • es.cluster.health,獲取有關群集運行狀況的非常簡單的狀態。
<code>print(es.cluster.health())/<code>
  • es.cluster.state,獲取整個集群的綜合狀態信息。
<code>print(es.cluster.state())/<code>
  • es.cluster.stats,返回群集的當前節點的信息。
<code>print(es.cluster.stats())/<code>

6.Node (節點相關)

  • es.nodes.info,返回集群中節點的信息。
<code>print(es.nodes.info())  # 返回所節點
print(es.nodes.info(node_id='node1')) # 指定一個節點
print(es.nodes.info(node_id=['node1', 'node2'])) # 指定多個節點列表/<code>
  • es.nodes.stats,獲取集群中節點統計信息。
<code>print(es.nodes.stats())
print(es.nodes.stats(node_id='node1'))
print(es.nodes.stats(node_id=['node1', 'node2']))/<code>
  • es.nodes.hot_threads,獲取指定節點的線程信息。
<code>print(es.nodes.hot_threads(node_id='node1'))
print(es.nodes.hot_threads(node_id=['node1', 'node2']))/<code>
  • es.nodes.usage,獲取集群中節點的功能使用信息。
<code>print(es.nodes.usage())
print(es.nodes.usage(node_id='node1'))
print(es.nodes.usage(node_id=['node1', 'node2']))/<code>

7.Cat(查詢)

  • es.cat.allocation,返回分片使用情況。
<code>print(es.cat.allocation())
print(es.cat.allocation(node_id=['node1']))
print(es.cat.allocation(node_id=['node1', 'node2'], format='json'))/<code>
  • es.cat.count,Count提供對整個群集或單個索引的文檔計數的快速訪問。
<code>print(es.cat.count())  # 集群內的文檔總數
print(es.cat.count(index='py3')) # 指定索引文檔總數
print(es.cat.count(index=['py3', 'py2'], format='json')) # 返回兩個索引文檔和/<code>
  • es.cat.health,從集群中health裡面過濾出簡潔的集群健康信息。
  • es.cat.indices,返回索引的信息;也可以使用此命令進行查詢集群中有多少索引。
  • es.cat.master,返回集群中主節點的IP,綁定IP和節點名稱。
  • es.cat.plugins,返回節點的插件信息。
  • es.cat.shards,返回哪個節點包含哪些分片的信息。


分享到:


相關文章: