Elasticsearch学习之CRUD操作

ES的各类操作可以基于Restful风格来进行,所以我们主要使用curl命令来对ES进行操作。
语法格式:

  curl -X<VERB> ‘<PROTOCOL>://PORT/<PATH>?<QUERY_STRING>’ -d '<BODY>'
  VERB:请求方法,GET,PUT,DELETE等;
  PROTOCOL:http,https;
  QUERY_STRING:查询参数,例如?pretty表示用易读的JSON格式输出;
  BODY:请求的主体;

1)检查ES工作是否正常

[root@node1 ~]# curl -XGET 'http://localhost:9200/?pretty'
{
  "name" : "node1",
  "cluster_name" : "my-elk",
  "cluster_uuid" : "cVBsHir6Ti6TmvoBWytSWA",
  "version" : {
    "number" : "6.6.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "3bd3e59",
    "build_date" : "2019-03-06T15:16:26.864148Z",
    "build_snapshot" : false,
    "lucene_version" : "7.6.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

2)查看cat子句所支持的语句

[root@node1 ~]# 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

3)显示集群的所以节点(?v显示详细信息)

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/nodes?v'
ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.146.130           50          32   0    0.00    0.01     0.05 mdi       -      node2
192.168.146.128           52          94  14    0.15    0.28     0.31 mdi       *      node1

4)显示help的帮助信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/nodes?help'
id                           | id,nodeId                      | unique node id                                                                                                   
pid                          | p                              | process id                                                                                                       
ip                           | i                              | ip address                                                                                                       
port                         | po                             | bound transport port                                                                                             
http_address                 | http                           | bound http address                                                                                               
version                      | v                              | es version                                                                                                       
flavor                       | f                              | es distribution flavor                                                                                           
type                         | t                              | es distribution type     

5)显示指定的ip、端口、运行时间、堆内存信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/nodes?h=name,ip,prot,uptime,heap.current'
node2 192.168.146.130 3.8h 255.6mb
node1 192.168.146.128 3.8h 267.5mb

6)显示主节点详细信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/master?v'
id                     host            ip              node
m3dNOQ03SPmPKmwED4xrkQ 192.168.146.128 192.168.146.128 node1

7)显示节点状态信息

[root@node1 ~]# curl -XGET 'http://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
1575269323 06:48:43  my-elk  green           2         2     40  20    0    0        0             0                  -                100.0%

8)查询集群状态信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cluster/health?pretty'
{
  "cluster_name" : "my-elk",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 20,
  "active_shards" : 40,
  "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
}

9)显示集群版本信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cluster/state/version?pretty'
{
  "cluster_name" : "my-elk",
  "compressed_size_in_bytes" : 15704,
  "cluster_uuid" : "cVBsHir6Ti6TmvoBWytSWA",
  "version" : 38,
  "state_uuid" : "GmV2QiC7T3elMcrRgclbCQ"
}

10)显示集群节点信息

扫描二维码关注公众号,回复: 8966710 查看本文章
[root@node1 ~]# curl -XGET 'http://localhost:9200/_cluster/state/nodes?pretty'
{
  "cluster_name" : "my-elk",
  "compressed_size_in_bytes" : 15704,
  "cluster_uuid" : "cVBsHir6Ti6TmvoBWytSWA",
  "nodes" : {
    "BzvbR9ArQsWV5JXkN9zO3w" : {
      "name" : "node2",
      "ephemeral_id" : "VHqyncZHQZah_vQRLKbTig",
      "transport_address" : "192.168.146.130:9300",
      "attributes" : {
        "xpack.installed" : "true"
      }
    },
    "m3dNOQ03SPmPKmwED4xrkQ" : {
      "name" : "node1",
      "ephemeral_id" : "4LOdG3-8S2uBx3SwFd6TOQ",
      "transport_address" : "192.168.146.128:9300",
      "attributes" : {
        "xpack.installed" : "true"
      }
    }
  }
}

11)显示集群的数据统计信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cluster/state?pretty' | less 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
{ 0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  "cluster_name" : "my-elk",
  "compressed_size_in_bytes" : 15704,
  "cluster_uuid" : "cVBsHir6Ti6TmvoBWytSWA",
  "version" : 38,
  "state_uuid" : "GmV2QiC7T3elMcrRgclbCQ",
  "master_node" : "m3dNOQ03SPmPKmwED4xrkQ",
  "blocks" : { },
  "nodes" : {
    "BzvbR9ArQsWV5JXkN9zO3w" : {
      "name" : "node2",
      "ephemeral_id" : "VHqyncZHQZah_vQRLKbTig",
      "transport_address" : "192.168.146.130:9300",
      "attributes" : {
        "xpack.installed" : "true"
      }
    },

12)显示分片的详细信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/shards?v' 
index     shard prirep state   docs  store ip              node
student   1     p      STARTED    2  8.2kb 192.168.146.128 node1
student   1     r      STARTED    2  8.2kb 192.168.146.130 node2
student   0     p      STARTED    1  4.2kb 192.168.146.128 node1
student   0     r      STARTED    1  4.2kb 192.168.146.130 node2
jike      1     p      STARTED   33 26.3kb 192.168.146.128 node1
jike      1     r      STARTED   33 26.3kb 192.168.146.130 node2
jike      0     p      STARTED   36 27.9kb 192.168.146.128 node1

13)显示具体索引的详细分片信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/shards/student?v' 
index   shard prirep state   docs store ip              node
student 1     p      STARTED    2 8.2kb 192.168.146.128 node1
student 1     r      STARTED    2 8.2kb 192.168.146.130 node2
student 0     p      STARTED    1 4.2kb 192.168.146.128 node1
student 0     r      STARTED    1 4.2kb 192.168.146.130 node2

14)显示所有索引的详细信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/indices?v' 
health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   user2     xS9YkbrrR7i4mJH9jLdeUA   5   1         14            0    104.9kb         52.4kb
green  open   jike      lC8W40rFRoaqkx7Cs8VqPw   2   1         69            0    108.6kb         54.3kb
green  open   students  YewCyaHgRq29-SeQ5TkYqw   5   1          2            0     17.5kb          8.7kb
green  open   student   a_lJjfqPS6WrlrHIqVmI6A   2   1          3            0     24.8kb         12.4kb
green  open   users     ZzxwcttUTgeUrw2Zr5_Tig   5   1         22            0    145.1kb         72.5kb
green  open   .kibana_1 vD7b9nMHTi6I3Ghn51c7ww   1   1          5            0     51.1kb         25.5kb

15)显示索引详细分段信息

[root@node1 ~]# curl -XGET 'http://localhost:9200/_cat/segments/student?v' 
index   shard prirep ip              segment generation docs.count docs.deleted  size size.memory committed searchable version compound
student 0     p      192.168.146.128 _0               0          1            0 3.9kb        1699 true      true       7.6.0   true
student 0     r      192.168.146.130 _0               0          1            0 3.9kb        1699 true      true       7.6.0   true
student 1     p      192.168.146.128 _1               1          1            0 3.9kb        1699 true      true       7.6.0   true
student 1     p      192.168.146.128 _2               2          1            0 3.8kb        1699 true      true       7.6.0   true
student 1     r      192.168.146.130 _1               1          1            0 3.9kb        1699 true      true       7.6.0   true
student 1     r      192.168.146.130 _2               2          1            0 3.8kb        1699 true      true       7.6.0   true

16)添加文档操作

[root@node2 ~]# curl -XPUT -H Content-Type:application/json 'localhost:9200/student/class1/1?pretty'  -d'
{
  "name":"GuoJing",
  "age":20,
  "sex":"M"
}'
{
  "_index" : "student",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 9
}

17)查看索引中的文档内容(通过id来查询)

[root@node2 ~]# curl  -XGET -H Content-Type:application/json 'localhost:9200/student/class1/1?pretty'
{
  "_index" : "student",   //索引名称;
  "_type" : "class1",    //类型;
  "_id" : "1",    //自定义的文档id;
  "_version" : 3,    //版本号,被修改的次数;
  "_seq_no" : 3,    
  "_primary_term" : 9,
  "found" : true,
  "_source" : {     //元数据信息;
    "name" : "GuoJing",
    "age" : 20,
    "sex" : "M"
  }
}

18)修改字段信息(调用_update的API完成)

[root@node2 ~]# curl -XPOST  -H Content-Type:application/json  'localhost:9200/student/class1/1/_update?pretty'  -d' {
    "doc":{"age":30}
}'
{
  "_index" : "student",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 9
}

//再次查看
[root@node2 ~]# curl -XGET  -H Content-Type:application/json 'localhost:9200/student/class1/1?pretty'
{
  "_index" : "student",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 4,
  "_seq_no" : 4,
  "_primary_term" : 9,
  "found" : true,
  "_source" : {
    "name" : "GuoJing",
    "age" : 30,
    "sex" : "M"
  }
}

19)删除指定文档

[root@node2 ~]# curl -XDELETE  -H Content-Type:application/json 'localhost:9200/student/class1/1?pretty'
{
  "_index" : "student",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 8,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 9
}

//再次查看
[root@node2 ~]# curl -XGET  -H Content-Type:application/json 'localhost:9200/student/class1/1?pretty'
{
  "_index" : "student",
  "_type" : "class1",
  "_id" : "1",
  "found" : false    //文档对象已删除;
}

20)删除索引

[root@node2 ~]# curl -XDELETE -H Content-Type:application/json 'localhost:9200/student?pretty'
{
  "acknowledged" : true
}
[root@node2 ~]# curl -XGET  -H Content-Type:application/json 'localhost:9200/_cat/indices'   | grep "student"  -w 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   340  100   340    0     0   9592      0 --:--:-- --:--:-- --:--:--  9714
发布了83 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Micky_Yang/article/details/103348354