【Elasticsearch】第6篇:Elasticsearch的CURD

curl简单介绍

简单的认为是可以在命令行下面访问url的一个工具,使用curl可以简单实现常见的get/post请求。curl命令详解

curl 命令:

-X 指定http的请求方法 有HEAD GET POST PUT DELETE 
-d 指定要传输的数据 
-H 指定http请求头信息 
-i 获取响应头

cat系列

用于获取Elasticsearch集群状态接口

curl -XGET 'http://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

cluster系列

1.集群系统信息,包括CPU JVM等

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

2.集群的详细信息,节点、分片等

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

3.集群堆积的任务

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

集群关闭操作

1.关闭集群节点

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

2.关闭主节点

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

3.关闭整个集群

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创建:

【Elasticsearch】第6篇:Elasticsearch的CURD

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
}

相关推荐