es集群的简单管理以及基本的CURD操作

时间:2021-6-19 作者:qvyue

Elasticsearch的安装
kibana的安装

1 快速检查集群的健康状况

GET /_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
1488006741 15:12:21  elasticsearch green          1         1      1   1    0    0        1             0                  -                 50.0%

了解集群的健康状况,green、yellow、red

  • green:每个索引的primary shard和replica shard都是active状态的
  • yellow:每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
  • red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了

为什么现在处于一个yellow状态?
我们现在就一个笔记本电脑,就启动了一个es进程,相当于就只有一个node。现在es中有一个index,就是kibana自己内置建立的index。由于默认的配置是给每个index分配5个primary shard和5个replica shard,而且primary shard和replica shard不能在同一台机器上(为了容错)。现在kibana自己建立的index是1个primary shard和1个replica shard。当前就一个node,所以只有1个primary shard被分配了和启动了,但是一个replica shard没有第二台机器去启动。

2 快速查看集群中有哪些索引

GET /_cat/indices?v
es集群的简单管理以及基本的CURD操作
查看结果

es集群的简单管理以及基本的CURD操作

3 CURD操作

  1. PUT新增操作
PUT /product/_doc/1
{
  "name":"HUAWEI P40 PRO",
  "price":"6000"
}

新增结果:

{
  "_index" : "product",    --索引名称
  "_type" : "_doc",        --类型
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {                  --分片
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,                 --文档更新一次后都会+1
  "_primary_term" : 1            --文档所在主分片的编号
}

2.GET查询数据

GET /product/_doc/1

查询结果:

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "HUAWEI P40 PRO",
    "price" : "6000"
  }
}

3.POST修改数据

修改ID为1的数据

POST /product/_doc/1/_update
{
  "doc":{
    "name":"HUAWEI P40"
  }
}

返回结果:

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,              --进行了+1
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,                --进行了+1
  "_primary_term" : 1
}

查看更新后的结果:

GET /product/_doc/1

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "HUAWEI P40",      --被更新的值
    "price" : "6000"
  }
}

4.DELETE数据

DELETE /product/_doc/1

返回结果:

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。