二、ElasticSearch的常用API

ElasticSearch的常用API

在服務器上怎麼查ES的信息

#通過使用_cat可以查看支持的命令

curl localhost:9200/_cat

[es@localhost config]$ curl localhost:9200/_cat

=^.^=

/_cat/allocation

/_cat/shards

/_cat/shards/{index}

/_cat/master

/_cat/nodes

/_cat/tasks

/_cat/indices

/_cat/indices/{index}

/_cat/segments

/_cat/segments/{index}

/_cat/count

/_cat/count/{index}

/_cat/recovery

/_cat/recovery/{index}

/_cat/health

/_cat/pending_tasks

/_cat/aliases

/_cat/aliases/{alias}

/_cat/thread_pool

/_cat/thread_pool/{thread_pools}

/_cat/plugins

/_cat/fielddata

/_cat/fielddata/{fields}

/_cat/nodeattrs

/_cat/repositories

/_cat/snapshots/{repository}

/_cat/templates

verbose

每個命令都支持使用?v參數,來顯示詳細的信息:

[es@localhost config]$ curl localhost:9200/_cat/master?v
id                     host           ip             node
TFtqZGtjTTiHsX3KHYIMyw 192.168.21.131 192.168.21.131 localhost.localdomain

help

每個命令都支持使用help參數,來輸出可以顯示的列:

[es@localhost config]$ curl localhost:9200/_cat/master?v
id                     host           ip             node
TFtqZGtjTTiHsX3KHYIMyw 192.168.21.131 192.168.21.131 localhost.localdomain
[es@localhost config]$ curl localhost:9200/_cat/master?help
id   |   | node id    
host | h | host name  
ip   |   | ip address 
node | n | node name 

headers

通過h參數,可以指定輸出的字段:

[es@localhost config]$ curl localhost:9200/_cat/master?v
id                     host           ip             node
TFtqZGtjTTiHsX3KHYIMyw 192.168.21.131 192.168.21.131 localhost.localdomain
[es@localhost config]$ curl localhost:9200/_cat/master?h=id,ip,node
TFtqZGtjTTiHsX3KHYIMyw 192.168.21.131 localhost.localdomain

數字類型的格式化

很多的命令都支持返回可讀性的大小數字,比如使用mb或者kb來表示。

[es@localhost config]$ curl localhost:9200/_cat/indices

green open customer2 9im2_3hIT9-chDznVXwOtg 1 1 0 0 566b  283b

green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0  7kb 3.5kb

bytes: 數值列還原爲原始值. 如diskSize, 默認轉爲以kb/mb/gb表示, 打開後還原爲原始值

[es@localhost config]$ curl localhost:9200/_cat/indices?bytes=b

green open customer2 9im2_3hIT9-chDznVXwOtg 1 1 0 0  566  283

green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0 7260 3630

# 創建customer索引
curl -X PUT 'localhost:9200/customer?pretty'

[es@localhost config]$ curl -X PUT 'localhost:9200/customer?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}

# 刪除索引
curl -X DELETE localhost:9200/customer?pretty

[es@localhost config]$ curl -X DELETE localhost:9200/customer?pretty
{
  "acknowledged" : true
}

#獲取全部索引【列出所有索引, 並展示索引基本信息】
curl localhost:9200/_cat/indices?v

[es@localhost config]$ curl localhost:9200/_cat/indices?v
health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   customer1 uaA_cmxYSU-76jT93-hs0w   1   1          1            0        7kb          3.5kb
green  open   customer  FTTKAgKKQAWpBI78aTUO9Q   1   1          1            0     15.7kb          7.8kb
--health                    索引的健康狀態
--status                    索引狀態信息open/close
--index                     索引名
--uuid                      索引的UUID
--pri                       索引主分片數量
--rep                       索引複製分片數量
--store.size            索引主分片 複製分片 總佔用存儲空間
--pri.store.size    索引總佔用空間, 不計算複製分片 佔用空間
#查看集羣健康狀態簡短顯示
curl localhost:9200/_cat/health?v
eg:
[es@localhost config]$ curl localhost:9200/_cat/health?v
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1592809420 07:03:40  elasticsearch green           2         2      4   2    0    0        0             0                  -                100.0%

#查看集羣健康狀態詳細顯示
curl localhost:9200/_cluster/health?pretty

[es@localhost config]$ curl localhost:9200/_cluster/health?pretty
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 2,
  "active_shards" : 4,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

# 查看節點列表【列出所有node, 並展示node所在機器的運行狀態信息.】
curl localhost:9200/_cat/nodes?v

[es@localhost config]$ curl localhost:9200/_cat/nodes?v
ip             heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.21.131           17          94  13    0.40    0.32     0.17 dilm      *      localhost.localdomain
192.168.21.131           12          93  15    0.40    0.32     0.17 dilm      -      localhost.localdomain
--ip                        ip
--heap.percent              堆內存佔用百分比
--ram.percent               內存佔用百分比
--cpu                       CPU佔用百分比
--master                    *表示節點是集羣中的主節點、其他表示從節點
--name                      節點名

#查看分片
curl localhost:9200/_cat/shards

[es@localhost config]$ curl localhost:9200/_cat/shards
customer1 0 p STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer1 0 r STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer  0 p STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer  0 r STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
--prirep p表示該分片是主分片, r 表示該分片是複製分片

#各節點機器存儲信息【列出所有node, 並展示所屬機器的配置信息】
curl localhost:9200/_cat/allocation?v

[es@localhost config]$ curl localhost:9200/_cat/allocation?v
shards disk.indices disk.used disk.avail disk.total disk.percent host           ip             node
     2        3.8kb     6.5gb     40.4gb     46.9gb           13 192.168.21.131 192.168.21.131 localhost.localdoma
     2        3.8kb     6.5gb     40.4gb     46.9gb           13 192.168.21.131 192.168.21.131 localhost.localdoma
--shards                節點說承載的分片數
--disk.indices          索引佔用的空間大小
--disk.used             節點所在機器已使用磁盤空間
--disk.avail            節點所在機器可用磁盤空間
--disk.total            節點所在機器總磁盤空間
--disk.percent          節點所在機器磁盤空間佔用百分比
--host                  host
--ip                    節點所屬機器IP地址
--node                  節點名

#獲取所有的type
curl -X GET http://localhost:9200/_mapping?pretty

[es@localhost config]$ curl -X GET http://localhost:9200/_mapping?pretty
{
  "customer" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  },
  "customer1" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

集羣

# 查看集羣狀態 curl localhost:9200/_cat/health?v

 

# 插入一條數據id爲1的文檔,如果已經存在該ID則替換改文檔
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'

 

# 插入一個文檔,不指定ID,ID自動生成
curl -X POST "localhost:9200/customer/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'

 

# 查詢id爲1的文檔
curl -X GET localhost:9200/customer/_doc/1?pretty

 

# 查詢一個索引的所有文檔
curl localhost:9200/customer/_search?pretty

 

# 條件查詢
curl "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"

# 條件查詢,_source查詢的字段,size查詢條數,sort排序,query查詢條件
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "from":5,
  "size":5,
  "sort":{
    "balance":{"order":"desc"}
    },
  "_source":["_id","name"]
}'

 

# 模糊查詢,match中跟要模糊查詢的字段,模糊的值在value中用空格隔開,用match_phrase它返回地址中包含短語"mill lane"的所有帳戶
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match": {"address":"mill lane"} },
  "from":5,
  "size":5,
  "sort":{
    "balance":{"order":"desc"}
    },
  "_source":["_id","name"]
}'

 

# 該bool must子句指定了所有必須爲true的查詢才能將文檔視爲匹配項。相反,此示例組成兩個match查詢,並返回地址中包含“ mill”或“ lane”的所有帳戶:
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

 

# 該bool should子句指定了一個查詢列表,對於將文檔視爲匹配項,其中任一查詢都必須爲true。本示例組成兩個match查詢,並返回地址中既不包含“ mill”也不包含“ lane”的所有帳戶
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

 

# 該bool must_not子句指定了一個查詢列表,對於被視爲匹配的文檔,沒有一個查詢列表必須爲真。我們可以在查詢中同時組合must,should和must_not子句bool。此外,我們可以bool在任何這些bool子句中組成查詢,以模仿任何複雜的多級布爾邏輯
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

 

# 我們可以在查詢中同時組合must,should和must_not子句bool。此外,我們可以bool在任何這些bool子句中組成查詢,以模仿任何複雜的多級布爾邏輯。
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}'

 

# filter子句,這些子句允許我們使用查詢來限制將要與其他子句匹配的文檔,而無需更改分數的計算方式。作爲示例,讓我們介紹一下rangequery,它允許我們按一定範圍的值過濾文檔。通常用於數字或日期過濾
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}'

 

# 執行聚合,按狀態對所有帳戶進行分組,然後返回按計數遞減排序的前10個(默認)狀態(也是默認)
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}'

 

# 更新ID爲1的文檔
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'{"doc":{"name": "Jane Doe","age":"18"}}'

 

# 批量更新文檔
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }'

 

# 刪除ID爲2的文檔
curl -X DELETE localhsot:9200/customer/_doc/2?pretty

 

# 批量刪除和更新
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}'

 

# 批量將accounts.json文件中的數據導入到bank索引中
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"


 

curl -X POST "localhost:9200/twitter/_update_by_query?conflicts=proceed&pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}
'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章