Python操作Elasticsearch對象


操作幾個方面

  • 結果過濾,對於返回結果做過濾,主要是優化返回內容。
  • 直接操作elasticsearch對象,處理一些簡單的索引信息。一下幾個方面都是建立在es對象的基礎上。
  • Indices,關於索引的細節操作,比如創建自定義的mappings。
  • Cluster,關於集群的相關操作。
  • Nodes,關於節點的相關操作。
  • Cat API,換一種查詢方式,一般的返回都是json類型的,cat提供了簡潔的返回結果。
  • Snapshot,快照相關,快照是從正在運行的Elasticsearch集群中獲取的備份。我們可以拍攝單個索引或整個群集的快照,並將其存儲在共享文件系統的存儲庫中,並且有一些插件支持S3,HDFS,Azure,Google雲存儲等上的遠程存儲庫。
  • Task Management API,任務管理API是新的,仍應被視為測試版功能。API可能以不向后兼容的方式更改。

結果過濾

  • filter_path參數用於過濾減少es返回信息,可以指定返回相關的內容,還支持一些通配符的操作*
 1 body = {
 2     "query": {
 3         "match": {
 4             "name": "成都"
 5         }
 6     }
 7 }
 8 # print(es.search(index="p1", body=body))
 9 print(es.search(index="p1", body=body, filter_path=["hits.hits"]))
10 print(es.search(index="p1", body=body, filter_path=["hits.hits._source"]))
11 print(es.search(index="p1", body=body, filter_path=["hits.hits._source", "hits.total"]))
12 print(es.search(index="p1", body=body, filter_path=["hits.*"]))
13 print(es.search(index="p1", body=body, filter_path=["hits.hits._*"]))

Elasticsearch(es對象 )

  • es.index 向指定索引添加更新文檔 如果索引不存在就會創建,然后執行添加更新等操作
1 # print(es.index(index="p2", doc_type="doc", id=1, body={"name": "棒槌", "age": "18"}))  # 正常
2 # print(es.index(index="p2", doc_type="doc", id=2, body={"name": "棒棒噠", "age": 20}))  # 正常
3 # print(es.index(index="p2", doc_type="doc", body={"name": "熊大", "age": "10"}))  # 如果添加文檔不帶id自動會創建一個
  • es.get 查詢索引中指定文檔
1 # get(self, index, id, doc_type="_doc", params=None):
2 # print(es.get(index='p2', doc_type='doc', id=1))  # 正常
3 # print(es.get(index='p2', doc_type='doc'))  # TypeError: get() missing 1 required positional argument: 'id' 只能取單個文檔對象
4 # print(es.get(index='p2', id=2))  # 要指定文檔類型elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"_index":"p2","_type":"_doc","_id":"2","found":false}')
  • es.search 執行搜索查詢並獲取其匹配它可以跟復雜的查詢條件 
  1. index要搜索的以逗號分隔的索引名稱列表; 使用_all 或空字符串對所有索引執行操作。
  2. doc_type 要搜索的以逗號分隔的文檔類型列表; 留空以對所有類型執行操作。
  3. body 使用Query DSL(QueryDomain Specific Language查詢表達式)的搜索定義。
  4. _source 返回_source字段的true或false,或返回的字段列表,返回指定字段。
  5. _source_exclude要從返回的_source字段中排除的字段列表,返回的所有字段中,排除哪些字段。
  6. _source_include_source字段中提取和返回的字段列表,跟_source差不多
1 # print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}))
2 # print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}, _source=['name', 'age']))
3 # print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}, _source_exclude=['age']))
4 print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}, _source_include=['age']))

 排除結果

{'took': 3, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 2, 'max_score': 0.2876821, 'hits': [{'_index': 'p2', '_type': 'doc', '_id': 'lEP_u2wBsrL_vQjNIMFf', '_score': 0.2876821, '_source': {'name': '熊大'}}, {'_index': 'p2', '_type': 'doc', '_id': 'W0MjvGwBsrL_vQjNNd0x', '_score': 0.2876821, '_source': {'sex': '男', 'name': '熊大', 'desc': '卡通智障兒童'}}]}}
  • es.get_source,通過索引、類型和ID獲取文檔的來源,其實,直接返回想要的字典。 
1 print(es.get_source(index='p2', doc_type='doc', id='1'))
  • es.count,執行查詢並獲取該查詢的匹配數。比如查詢年齡是18的文檔。
 1 body = {
 2     "query": {
 3         "match": {
 4             "age": 18
 5         }
 6     }
 7 }
 8 # print(es.count(index='p2', doc_type='doc', body=body))
 9 # print(es.count(index='p2', doc_type='doc', body=body)['count'])
10 print(es.count(index='p2')) # 查在p2索引中總的文檔個數{'count': 4, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
11 print(es.count(index='p2', doc_type='doc')) # {'count': 4, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
  • es.create,創建索引(索引不存在的話)並新增一條數據,索引存在僅新增(只能新增,重復執行會報錯),還是覺得index好用些
1 # print(es.create(index='p2', doc_type='doc', id=3, body={"city": "成都", "desc": "旅游的地方頗多, 小吃出名"}))
2 print(es.get_source(index='p2', doc_type='doc', id=3)) 
  • es.delete,刪除指定的文檔。比如刪除文章id為4的文檔,但不能刪除僅只刪除索引,如果想要刪除索引,還需要es.indices.delete來處理
  • es.delete_by_query,刪除與查詢匹配的所有文檔。
1 # print(es.delete(index='p2', doc_type='doc', id=1))
2 # print(es.delete_by_query(index='p2', body={"query": {"match": {"age": 20}}}))
3 # print(es.search(index='p2'))
  • es.exists,查詢elasticsearch中是否存在指定的文檔,返回一個布爾值。
1 print(es.exists(index='p2', doc_type='doc', id='1'))
  • es.info,獲取當前集群的基本信息。
1 print(es.info())
  • es.ping,如果集群已啟動,則返回True,否則返回False。
1 print(es.ping())

Indices(es.indices 索引相關)  

 創建索引 

 1 body = {
 2     "mappings": {
 3         "doc": {
 4             "dynamic": "strict",
 5             "properties": {
 6                 "title": {
 7                     "type": "text",
 8                     "analyzer": "ik_max_word"
 9                 },
10                 "url": {
11                     "type": "text"
12                 },
13                 "action_type": {
14                     "type": "text"
15                 },
16                 "content": {
17                     "type": "text"
18                 }
19             }
20         }
21     }
22 }
23 
24 print(es.indices.create(index='p3', body=body))

創建結果

1 {'acknowledged': True, 'shards_acknowledged': True, 'index': 'p3'}
  • es.indices.analyze,返回分詞結果。
1 print(es.indices.analyze(body={'analyzer': 'ik_max_word', 'text': "皮特和茱麗當選“年度模范情侶”Brad Pitt and Angelina Jolie"}))
  • es.indices.delete,在Elasticsearch中刪除索引。
1 print(es.indices.delete(index='p3'))
2 print(es.indices.delete(index='p2'))    # {'acknowledged': True}
  •  es.indices.put_alias,為一個或多個索引創建別名,查詢多個索引的時候,可以使用這個別名。
1 print(es.indices.put_alias(index='p3', name='p3_alias'))  # 為單個索引創建別名
2 print(es.indices.put_alias(index=['p3', 'p2'], name='p23_alias'))  # 為多個索引創建同一個別名,聯查用
  • es.indices.delete_alias,刪除一個或多個別名。
1 print(es.indices.delete_alias(index='p1'))
2 print(es.indices.delete_alias(index=['p1, p2'])) 
  • es.indices.get_mapping,檢索索引或索引/類型的映射定義。
1 print(es.indices.get_mapping(index='p3'))
  • es.indices.get_settings,檢索一個或多個(或所有)索引的設置。
1 print(es.indices.get_settings(index='p3'))
  • es.indices.get,允許檢索有關一個或多個索引的信息。
1 print(es.indices.get(index='p2'))    # 查詢指定索引是否存在
2 print(es.indices.get(index=['p2', 'p3']))
  • es.indices.get_alias,檢索一個或多個別名。
1 print(es.indices.get_alias(index='p2'))
2 print(es.indices.get_alias(index=['p2', 'p3']))
  • es.indices.get_field_mapping,檢索特定字段的映射信息。
1 print(es.indices.get_field_mapping(fields='url', index='p3', doc_type='doc'))
2 print(es.indices.get_field_mapping(fields=['url', 'title'], index='p3', doc_type='doc'))
  • es.indices.delete_alias,刪除特定別名。
  • es.indices.exists,返回一個布爾值,指示給定的索引是否存在。
  • es.indices.exists_type,檢查索引/索引中是否存在類型/類型。
  • es.indices.flus,明確的刷新一個或多個索引。
  • es.indices.get_field_mapping,檢索特定字段的映射。
  • es.indices.get_template,按名稱檢索索引模板。
  • es.indices.open,打開一個封閉的索引以使其可用於搜索。
  • es.indices.close,關閉索引以從群集中刪除它的開銷。封閉索引被阻止進行讀/寫操作。
  • es.indices.clear_cache,清除與一個或多個索引關聯的所有緩存或特定緩存。
  • es.indices.put_alias,為特定索引/索引創建別名。
  • es.indices.get_uprade,監控一個或多個索引的升級程度。
  • es.indices.put_mapping,注冊特定類型的特定映射定義。
  • es.indices.put_settings,實時更改特定索引級別設置。
  • es.indices.put_template,創建一個索引模板,該模板將自動應用於創建的新索引。
  • es.indices.rollove,當現有索引被認為太大或太舊時,翻轉索引API將別名轉移到新索引。API接受單個別名和條件列表。別名必須僅指向單個索引。如果索引滿足指定條件,則創建新索引並切換別名以指向新別名。
  • es.indices.segments,提供構建Lucene索引(分片級別)的低級別段信息。

Cluster(集群相關)

  • es.cluster.get_settigns,獲取集群設置。
1 print(es.cluster.get_settings())
  • es.cluster.health,獲取有關群集運行狀況的非常簡單的狀態。
1 print(es.cluster.health()) 
  • es.cluster.state,獲取整個集群的綜合狀態信息。
1 print(es.cluster.state())
  • es.cluster.state,獲取整個集群的綜合狀態信息。
1 print(es.cluster.stats()) 

 Node(節點相關) 

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

Cat(一種查詢方式)

  • es.cat.aliases,返回別名信息。
1 print(es.cat.aliases(name='p23_alias'))
2 print(es.cat.aliases(name='p23_alias', format='json')) 
  • es.cat.allocation,返回分片使用情況。
1 print(es.cat.allocation())
2 print(es.cat.allocation(node_id=['node1']))
3 print(es.cat.allocation(node_id=['node1', 'node2'], format='json'))
  • es.cat.count,Count提供對整個群集或單個索引的文檔計數的快速訪問。
1 print(es.cat.count())  # 集群內的文檔總數
2 print(es.cat.count(index='p3'))  # 指定索引文檔總數
3 print(es.cat.count(index=['p3', 'p2'], format='json'))  # 返回兩個索引文檔和
  • es.cat.fielddata,基於每個節點顯示有關當前加載的fielddata的信息。有些數據為了查詢效率,會放在內存中,fielddata用來控制哪些數據應該被放在內存中,而這個es.cat.fielddata則查詢現在哪些數據在內存中,數據大小等信息。

1 print(es.cat.fielddata())
2 print(es.cat.fielddata(format='json', bytes='b'))
1 bytes 單位'b''k''kb''m''mb''g''gb''t''tb''p''pb'
  • es.cat.health,從集群中health里面過濾出簡潔的集群健康信息
1 print(es.cat.health())
2 print(es.cat.health(format='json'))
  • es.cat.help,返回es.cat的幫助信息。
1 print(es.cat.help())
  • es.cat.indices,返回索引的信息。
1 print(es.cat.indices())
2 print(es.cat.indices(index='p3'))
3 print(es.cat.indices(index='p3', format='json'))
  • es.cat.master,返回集群中主節點的IP,綁定IP和節點名稱。
1 print(es.cat.master())
2 print(es.cat.master(format='json'))
  • es.cat.nodeattrs,返回節點的自定義屬性。
1 print(es.cat.nodeattrs())
2 print(es.cat.nodeattrs(format='json'))
  • es.cat.nodes,返回節點的拓撲,這些信息在查看整個集群時通常很有用,特別是大型集群。我有多少符合條件的節點?
1 print(es.cat.nodes())
2 print(es.cat.nodes(format='json'))
  • es.cat.plugins,返回節點的插件信息。
1 print(es.cat.plugins())
2 print(es.cat.plugins(format='json'))
  • es.cat.segments,返回每個索引的Lucene有關的信息。
1 print(es.cat.segments())
2 print(es.cat.segments(index='p3'))
3 print(es.cat.segments(index='p3', format='json'))
  • es.cat.shards,返回哪個節點包含哪些分片的信息。
1 print(es.cat.shards())
2 print(es.cat.shards(index='p3'))
3 print(es.cat.shards(index='p3', format='json'))
  • es.cat.thread_pool,獲取有關線程池的信息。
1 print(es.cat.thread_pool())

Snapshot(快照相關)

  • es.snapshot.create,在存儲庫中創建快照。
    • repository存儲庫名稱。
    • snapshot快照名稱。
    • body快照定義。
  • es.snapshot.delete,從存儲庫中刪除快照。
  • es.snapshot.create_repository。注冊共享文件系統存儲庫。
  • es.snapshot.delete_repository,刪除共享文件系統存儲庫。
  • es.snapshot.get,檢索有關快照的信息。
  • es.snapshot.get_repository,返回有關已注冊存儲庫的信息。
  • es.snapshot.restore,恢復快照。
  • es.snapshot.status,返回有關所有當前運行快照的信息。通過指定存儲庫名稱,可以將結果限制為特定存儲庫。
  • es.snapshot.verify_repository,返回成功驗證存儲庫的節點列表,如果驗證過程失敗,則返回錯誤消息。

Task(任務相關)

  • es.tasks.get,檢索特定任務的信息。
  • es.tasks.cancel,取消任務。
  • es.tasks.list,任務列表。

更多玩法https://elasticsearch-py.readthedocs.io/en/master/api.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM