2024-03-13
原文作者:吴声子夜歌 原文地址: https://blog.csdn.net/cold___play/article/details/104761197

简单集群管理操作

es提供了一套api,叫做cat api,可以查看es中各种各样的数据。

查看集群状态:

    GET /_cat/health?v

202403132036152691.png

集群的状态用3中颜色表示:

  • 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没有第二台机器去启动。

做一个小实验:此时只要启动第二个es进程,就会在es集群中有2个node,然后那1个replica shard就会自动分配过去,然后cluster status就会变成green状态。

查看索引:

    GET /_cat/indices?v

202403132036156362.png

简单索引操作

新增索引:

    PUT /test_index?pretty

202403132036160543.png
删除索引:

    DELETE /test_index?pretty

202403132036165484.png

增删改查

新增:

    //格式
    PUT /index/type/id
    {
      "json数据"
    }

示例:

    PUT /ecommerce/product/1
    {
        "name" : "gaolujie yagao",
        "desc" :  "gaoxiao meibai",
        "price" :  30,
        "producer" :      "gaolujie producer",
        "tags": [ "meibai", "fangzhu" ]
    }
    
    PUT /ecommerce/product/2
    {
        "name" : "jiajieshi yagao",
        "desc" :  "youxiao fangzhu",
        "price" :  25,
        "producer" :      "jiajieshi producer",
        "tags": [ "fangzhu" ]
    }
    
    PUT /ecommerce/product/3
    {
        "name" : "zhonghua yagao",
        "desc" :  "caoben zhiwu",
        "price" :  40,
        "producer" :      "zhonghua producer",
        "tags": [ "qingxin" ]
    }

返回值:

    {
      "_index": "ecommerce",
      "_type": "product",
      "_id": "1",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 2,//主分片一个,副本分片一个,每条数据应该存2份
        "successful": 1,//但是我们只有一个节点,只有主分片存入成功
        "failed": 0
      },
      "created": true
    }

es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索

查询:

    //语法格式:
    GET /index/type/id

示例:

    GET /ecommerce/product/1

返回:

    {
      "_index": "ecommerce",
      "_type": "product",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "name": "gaolujie yagao",
        "desc": "gaoxiao meibai",
        "price": 30,
        "producer": "gaolujie producer",
        "tags": [
          "meibai",
          "fangzhu"
        ]
      }
    }

修改:

  • 方式一:替换
       PUT /ecommerce/product/1
    {
        "name" : "jiaqiangban gaolujie yagao",
        "desc" :  "gaoxiao meibai",
        "price" :  30,
        "producer" :      "gaolujie producer",
        "tags": [ "meibai", "fangzhu" ]
    }

返回:

    {
      "_index": "ecommerce",
      "_type": "product",
      "_id": "1",
      "_version": 2,//更新为2
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      },
      "created": false
    }

替换方式有一个不好,即使必须带上所有的field,才能去进行信息的修改;否则会造成数据丢失

  • 方式二:更新(加上_update)
    POST /ecommerce/product/1/_update
    {
      "doc": {
        "name": "jjj gaolujie yagao"
      }
    }

返回:

    {
      "_index": "ecommerce",
      "_type": "product",
      "_id": "1",
      "_version": 3,
      "result": "updated",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }

删除

    格式:
    DELETE /index/type/id

示例:

    DELETE /ecommerce/product/1

返回:

    {
      "found": true,
      "_index": "ecommerce",
      "_type": "product",
      "_id": "1",
      "_version": 4,
      "result": "deleted",
      "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
      }
    }

再次查询:

    {
      "_index": "ecommerce",
      "_type": "product",
      "_id": "1",
      "found": false
    }
阅读全文