【Elasticsearch】第6篇:Elasticsearch的CURD

curl簡單介紹

簡單的認爲是能夠在命令行下面訪問url的一個工具,使用curl能夠簡單實現常見的get/post請求。curl命令詳解node

curl 命令:linux

-X 指定http的請求方法 有HEAD GET POST PUT DELETE 
-d 指定要傳輸的數據 
-H 指定http請求頭信息 
-i 獲取響應頭

cat系列

用於獲取Elasticsearch集羣狀態接口json

curl -XGET 'http://localhost:9200/_cat'

接口展現:app

=^.^=
/_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

cluster系列

1.集羣系統信息,包括CPU JVM等curl

curl -XGET 'https://localhost:9200/_cluster/stats?pretty=true'

2.集羣的詳細信息,節點、分片等elasticsearch

curl -XGET 'http://localhost:9200/_cluster/state?pretty=true'

3.集羣堆積的任務工具

curl -XGET 'http://localhost:9200/_cluster/pending_tasks?pretty=true'

集羣關閉操做

1.關閉集羣節點post

curl -XPOST 'http://localhost:9200/_cluster/nodes/127.0.0.1/_shutdown'

2.關閉主節點url

curl -XPOST 'http://localhost:9200/_cluster/nodes/_master/_shutdown'

3.關閉整個集羣spa

curl -XPOST 'http://localhost:9200/_cluster/nodes/_all/_shutdown'

Index的創建和刪除

查看所有索引
curl -XGET 'http://localhost:9200/_cat/indices?v'
新建Index

1.命令建立:

curl -XPUT 'http://localhost:9200/city'
結果:
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "city"
}

2.elasticsearch-head建立:

clipboard.png

3.刪除索引

curl -XDELETE 'http://localhost:9200/account'

數據的簡單操做

添加數據
curl -H 'Content-Type: application/json' -XPOST 'http://localhost:9200/city/south/1' -d '{"cityName":"guangzhou"}'

結果

{
    "_index":"city",
    "_type":"south",
    "_id":"1",
    "_version":1,
    "result":"created",
    "_shards":{
        "total":2,
        "successful":2,
        "failed":0
    },
    "_seq_no":0,
    "_primary_term":1
}
查詢數據
curl -i -XGET 'http://localhost:9200/city/south/1'

結果

{
    "_index":"city",
    "_type":"south",
    "_id":"1",
    "_version":1,
    "found":true,
    "_source":{
        "cityName":"guangzhou"
    }
}
刪除數據
curl -XDELETE 'http://localhost:9200/city/south/1'

結果

{
    "_index":"city",
    "_type":"south",
    "_id":"1",
    "_version":2,
    "result":"deleted",
    "_shards":{
        "total":2,
        "successful":2,
        "failed":0
    },
    "_seq_no":1,
    "_primary_term":1
}
更新數據
curl -i -H 'Content-Type: application/json'  -XPUT 'http://localhost:9200/city/south/1' -d '{"cityName":"HUNAN"}'

結果

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 153
   
{
    "_index":"city",
    "_type":"south",
    "_id":"1",
    "_version":2,
    "result":"updated",
    "_shards":{
        "total":2,
        "successful":2,
        "failed":0
    },
    "_seq_no":3,
    "_primary_term":2
}
相關文章
相關標籤/搜索