elasticsearch for Python之操作篇


前言

Python中關於elasticsearch的操作,主要集中一下幾個方面:

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

結果過濾

print(es.search(index='py2', filter_path=['hits.total', 'hits.hits._source']))    # 可以省略type類型
print(es.search(index='w2', doc_type='doc'))        # 可以指定type類型
print(es.search(index='w2', doc_type='doc', filter_path=['hits.total']))

filter_path參數用於減少elasticsearch返回的響應,比如僅返回hits.totalhits.hits._source內容。
除此之外,filter_path參數還支持*通配符以匹配字段名稱、任何字段或者字段部分:

print(es.search(index='py2', filter_path=['hits.*']))
print(es.search(index='py2', filter_path=['hits.hits._*']))
print(es.search(index='py2', filter_path=['hits.to*']))  # 僅返回響應數據的total
print(es.search(index='w2', doc_type='doc', filter_path=['hits.hits._*']))        # 可以加上可選的type類型

Elasticsearch(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', id=6, body={"name": "鳴人", "age": 22}))  # 會報錯,TypeError: index() missing 1 required positional argument: 'doc_type'
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))  # 正常
print(es.get(index='w2', doc_type='doc'))  # TypeError: get() missing 1 required positional argument: 'id'
print(es.get(index='w2',  id=5))  # TypeError: get() missing 1 required positional argument: 'doc_type'
  • 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.get_source,通過索引、類型和ID獲取文檔的來源,其實,直接返回想要的字典。
print(es.get_source(index='py3', doc_type='doc', id='1'))  # {'name': '王五', 'age': 19}
  • es.count,執行查詢並獲取該查詢的匹配數。比如查詢年齡是18的文檔。
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'))

在內部,調用了index,等價於:

print(es.index(index='py3', doc_type='doc', id='4', body={"name": "麻子", "age": 21}))

但個人覺得沒有index好用!

  • es.delete,刪除指定的文檔。比如刪除文章id為4的文檔,但不能刪除索引,如果想要刪除索引,還需要es.indices.delete來處理
print(es.delete(index='py3', doc_type='doc', id='4'))
  • es.delete_by_query,刪除與查詢匹配的所有文檔。
    • index 要搜索的以逗號分隔的索引名稱列表; 使用_all 或空字符串對所有索引執行操作。
    • doc_type 要搜索的以逗號分隔的文檔類型列表; 留空以對所有類型執行操作。
    • body 使用Query DSL的搜索定義。
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,獲取當前集群的基本信息。
print(es.info())
  • es.ping,如果群集已啟動,則返回True,否則返回False。
print(es.ping())

Indices(es.indices)

  • es.indices.create,在Elasticsearch中創建索引,用的最多。比如創建一個嚴格模式、有4個字段、並為title字段指定ik_max_word查詢粒度的mappings。並應用到py4索引中。這也是常用的創建自定義索引的方式。
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)
  • 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'))
  • 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,獲取集群設置。
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())

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']))

Cat(一種查詢方式)

  • es.cat.aliases,返回別名信息。
    • name要返回的以逗號分隔的別名列表。
    • formatAccept標頭的簡短版本,例如json,yaml
print(es.cat.aliases(name='py23_alias'))
print(es.cat.aliases(name='py23_alias', format='json'))
  • 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.fielddata,基於每個節點顯示有關當前加載的fielddata的信息。有些數據為了查詢效率,會放在內存中,fielddata用來控制哪些數據應該被放在內存中,而這個es.cat.fielddata則查詢現在哪些數據在內存中,數據大小等信息。
print(es.cat.fielddata())
print(es.cat.fielddata(format='json', bytes='b'))

bytes顯示字節值的單位,有效選項為:'b','k','kb','m','mb','g','gb','t','tb' ,'p','pb'
formatAccept標頭的簡短版本,例如json,yaml

  • es.cat.health,從集群中health里面過濾出簡潔的集群健康信息。
print(es.cat.health())
print(es.cat.health(format='json'))
  • es.cat.help,返回es.cat的幫助信息。
print(es.cat.help())
  • es.cat.indices,返回索引的信息;也可以使用此命令進行查詢集群中有多少索引。
print(es.cat.indices())
print(es.cat.indices(index='py3'))
print(es.cat.indices(index='py3', format='json'))
print(len(es.cat.indices(format='json')))  # 查詢集群中有多少索引
  • es.cat.master,返回集群中主節點的IP,綁定IP和節點名稱。
print(es.cat.master())
print(es.cat.master(format='json'))
  • es.cat.nodeattrs,返回節點的自定義屬性。
print(es.cat.nodeattrs())
print(es.cat.nodeattrs(format='json'))
  • es.cat.nodes,返回節點的拓撲,這些信息在查看整個集群時通常很有用,特別是大型集群。我有多少符合條件的節點?
print(es.cat.nodes())
print(es.cat.nodes(format='json'))
  • es.cat.plugins,返回節點的插件信息。
print(es.cat.plugins())
print(es.cat.plugins(format='json'))
  • es.cat.segments,返回每個索引的Lucene有關的信息。
print(es.cat.segments())
print(es.cat.segments(index='py3'))
print(es.cat.segments(index='py3', format='json'))
  • es.cat.shards,返回哪個節點包含哪些分片的信息。
print(es.cat.shards())
print(es.cat.shards(index='py3'))
print(es.cat.shards(index='py3', format='json'))
  • es.cat.thread_pool,獲取有關線程池的信息。
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,任務列表。

see also:[API文檔-Elasticsearch6.3.1文檔](https://elasticsearch-py.readthedocs.io/en/master/api.html#global-options) | [Fielddata 的過濾](https://www.elastic.co/guide/cn/elasticsearch/guide/current/_fielddata_filtering.html) | [cat nodeattrs](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-nodeattrs.html) | [cat nodes](https://www.elastic.co/guide/en/elasticsearch/reference/current/cat-nodes.html#cat-nodes) | [任務管理API](https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html) | [napshot And Restore]( ) 歡迎斧正,that's all


免責聲明!

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



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