天天看點

Elasticsearch API 使用指南(全)

1. 叢集支援的選項

curl -XGET "http://172.0.0.52:9200/_cat"           

2. 檢視節點資訊

curl -XGET "http://172.0.0.52:9200/_cat/nodes?v"           

3. 檢視master節點資訊

curl -XGET "http://172.0.0.52:9200/_cat/master?v"           

4.檢視所有節點上的熱點線程

curl -XGET "http://172.0.0.52:9200/_nodes/hot_threads"           

5.檢視有問題的分片或索引

curl -XGET "http://172.0.0.52:9200/_cluster/allocation/explain?pretty"           

6.檢視線程池設定

curl -XGET "http://172.0.0.52:9200/_nodes/thread_pool/"           

7.統計全部資訊

curl -XGET "http://172.0.0.52:9200/_cluster/stats?human&pretty"           

8.檢視叢集狀态

curl -XGET "http://172.0.0.52:9200/_cluster/health?pretty"           

9.檢視ES資訊

curl -XGET "http://172.0.0.52:9200/"           

10.擷取所有索引的資訊

curl -XGET "http://172.0.0.52:9200/_cat/indices?v&pretty"
curl -XGET "http://172.0.0.52:9200/_cat/nodes?v"
curl -XGET "http://172.0.0.52:9200/_cat/segments?v&h=shard,segment,size,size.memory"           

11.擷取所有文檔數量

curl -XGET "http://172.0.0.52:9200/_count?pretty" -H 'Content-Type: application/json' -d'
{

    "query": {

        "match_all": {}

    }

}'           

12. 檢視叢集的健康狀态

  • green:所有功能都是完好的;
  • yellow:所有資料是可用的,但是一些副本還沒有被配置設定;
  • red代表一些資料由于某些原因已經不可用。
  • 注意,盡管一個叢集是red狀态,它仍然可以提供部分服務(比如,它會繼續從可用的切片資料裡搜尋),但是在失去部分資料後,需要盡快去修複。
curl -XGET "http://172.0.0.52:9200/_cat/health?v"           

13. 建立索引

  • test_one 索引名
  • pretty 參數表示輸出格式良好的JSON響應(如果存在)
curl -XPUT "http://172.0.0.52:9200/test_one?pretty"           

14. 檢視索引清單

curl -XGET "http://172.0.0.52:9200/_cat/indices?v"
curl -XGET "http://172.0.0.52:9200/test_one"           

15. 删除索引

  • 根據索引名稱删除
curl -XDELETE "http://172.0.0.52:9200/test_one?pretty"           
  • 可以一次删除多個索引(以逗号間隔)删除所有索引_all或通配符 *

16. 建立文檔

  • 向es中插入文檔的時候,必須要指定一個類型(type)
16.1.使用PUT來建立文檔,需要指定id
  • 索引 index:test_one
  • 類型 type:test_type
  • _id:1
curl -XPUT "http://172.0.0.52:9200/test_one/test_type/1?pretty" -H 'Content-Type: application/json' -d'
{"name": "ghl", "age": 24, "sex": "male"}'           
16.2. 使用POST來建立文檔,可以不指定id(不指定時随機生成id)
curl -XPOST "http://172.0.0.52:9200/test_one/test_type?pretty" -H 'Content-Type: application/json' -d'
{"name": "Jack"}'           

17. 檢視文檔

curl -XGET "http://172.0.0.52:9200/test_one/test_type/1?pretty"           

18. 替換文檔

使用PUT并指定id時,es會使用新的文檔替換原文檔
curl -XPUT "http://172.0.0.52:9200/test_one/test_type/1?pretty" -H 'Content-Type: application/json' -d'
{"name": "Jack"}'           

19. 更新文檔

curl -XPOST "http://172.0.0.52:9200/test_one/test_type/1/_update?pretty" -H 'Content-Type: application/json' -d'
{"doc":{"name": "Lucy"}}'           

20. 删除文檔

curl -XDELETE "http://172.0.0.52:9200/test_one/test_type/1?pretty"           

21. 索引的增删改查有一個類似的格式下:

curl -XGET "http://172.0.0.52:9200/<Index>/<Type>/<ID>"           
  • REST Verb

    :REST風格的文法謂詞;
  • Node

    :節點ip;
  • port

    :節點端口号,預設9200;
  • Index

    :索引名;
  • Type

    :索引類型;
  • ID

    :操作對象的ID号;

22. 判斷索引是否存在

curl -XHEAD "http://172.0.0.52:9200/test_one"           

23. 檢視索引模闆

curl -XGET "http://172.0.0.52:9200/_template/template_1"
curl -XGET "http://172.0.0.52:9200/_template/temp*"
curl -XGET "http://172.0.0.52:9200/_template/template_1,template_2"
curl -XGET "http://172.0.0.52:9200/_template"           

24. 删除模闆

curl -XDELETE "http://172.0.0.52:9200/_template/template_1"           

25. 打開/關閉索引

curl -XPOST "http://172.0.0.52:9200/test_one/_close"
curl -XPOST "http://172.0.0.52:9200/test_one/_open"
curl -XGET "http://172.0.0.52:9200/_cat/indices?v"           

26.檢視索引狀态資訊

curl -XGET "http://172.0.0.52:9200/_stats"
curl -XGET "http://172.0.0.52:9200/logstash-nginx-access-2019.08.07,test_one/_stats"           

27.檢視索引段資訊

curl -XGET "http://172.0.0.52:9200/test_one/_segments"
curl -XGET "http://172.0.0.52:9200/logstash-nginx-access-2019.08.07,test_one/_segments"
curl -XGET "http://172.0.0.52:9200/_segments"