之前说到 ES 提供了 RESTful 风格的接口以便于我们操作,需要借助能够发送HTTP请求的工具调用这些API,工具是可以任意的,包括网页浏览器,当然也可以使用比如 Postman / Talend API tester等工具。这里为了学习 ElK 所以选择使用 Kibana,关于 Kibana 如何安装参考其他文章。

集群相关#

查看集群健康信息#

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
1646502528 17:48:48  elasticsearch green           1         1      3   3    0    0        0             0                  -                100.0%

主要字段描述:

  • cluster:集群名称
  • status:集群状态
  • node.total:集群中的节点数
  • node.data:集群中的数据节点数
  • shards:集群中总的分片数量
  • pri:主分片数量,英文全称为 private
  • relo:复制分片总数
  • unassign:未指定的分片数量,是应有分片数和现有的分片数的差值(包括主分片和复制分片)

集群共有 greenyellowred 三种状态:

  • green 代表一切正常,集群功能齐全
  • yellow 代表所有数据都是可用的,但是某些复制没有被分配(集群功能齐全)
  • red 代表因为某些原因,某些数据不可用。如果是red状态,数据很有可能已经丢失

可以在请求中添加 help 参数来查看每个操作返回结果字段的描述

GET _cat/health?help
epoch                 | t,time                                   | seconds since 1970-01-01 00:00:00  
timestamp             | ts,hms,hhmmss                            | time in HH:MM:SS                   
cluster               | cl                                       | cluster name                       
status                | st                                       | health status                      
node.total            | nt,nodeTotal                             | total number of nodes              
node.data             | nd,nodeData                              | number of nodes that can store data
shards                | t,sh,shards.total,shardsTotal            | total number of shards             
pri                   | p,shards.primary,shardsPrimary           | number of primary shards           
relo                  | r,shards.relocating,shardsRelocating     | number of relocating nodes         
init                  | i,shards.initializing,shardsInitializing | number of initializing nodes       
unassign              | u,shards.unassigned,shardsUnassigned     | number of unassigned shards        
pending_tasks         | pt,pendingTasks                          | number of pending tasks            
max_task_wait_time    | mtwt,maxTaskWaitTime                     | wait time of longest task pending  
active_shards_percent | asp,activeShardsPercent                  | active number of shards in percent 

ES中许多API都可以添加 help 参数来显示字段含义,如果觉得返回的东西太多看着烦,也可以人为指定返回的字段,比如:

GET _cat/health?h=cluster,status&v
cluster       status
elasticsearch green

查看集群中节点信息#

GET _cat/nodes?v
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           28          59  11                          dilm      *      WANGPENGLIANG

查看集群中索引信息#

GET _cat/indices?v
health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_task_manager_1   BT3RImw0S9mVrEwQaKIc1Q   1   0          2            0     41.3kb         41.3kb
green  open   .apm-agent-configuration 82PMto3PSt2UsRf7qYmfJg   1   0          0            0       283b           283b
green  open   .kibana_1                xSc4xPL6TXica1mIT8005A   1   0         12            6     60.4kb         60.4kb

更多的查看和监视 ElastaticSearch 的API查看:官网文档

索引相关(Index)#

创建索引#

创建名为 moviesindex ,两种写法等同,名字不能包含特殊字符,只能小写,不能以- , _ , + 开头,不能超过255字节。

第一种方式:

PUT movies

第二种方式:本质是 PUT http://ip:9200/test,kibana 做了优化因此写不写之前的 / 都可以

PUT /movies

第三种方式:当向一个不存在的索引写入文档时,索引会自动被创建

PUT movies/_doc/1
{
  "name":"大话西游"
}

查看索引#

如果试图访问一个不存在的索引会报错。

GET movies

删除索引#

如果试图删除一个不存在的索引会报错。

DELETE movies

查看某字母开头的索引#

GET _cat/indices/m*?v

查询所有索引#

GET _cat/indices

mappings / settings#

创建索引时指定 map#

PUT movies
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      }
    }
  }
}

创建索引后指定 map#

PUT movies
PUT movies/_mapping
{
    "properties" : {
      "name" : {
        "type" : "keyword"
      }
    }
}

创建索引时同时指定#

创建索引时指定分片数和副本数。

PUT movies
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type" : "keyword"
      }
    }
  },
  "settings" : {
    "index" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 2
    }
  }
}

修改副本数量#

PUT movie/_settings
{
  "index" : {
    "number_of_replicas" : 3
  }
}

修改分片数量#

分片数量必须在索引创建时指定,创建后无法修改。尝试修改时会报错:

# 请求
PUT movie/_settings
{
  "index" : {
    "number_of_shards" : 3
  }
}

# 响应
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[movie/BPnIxSCwTvWNtze-4gDJaw]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[movie/BPnIxSCwTvWNtze-4gDJaw]]"
  },
  "status": 400
}

数据相关#

语法#

添加

  *****************添加*************************
  // 如果不传id, 则系统自动生成一个UUID
  POST /index/type/
  {
  	"属性名":修改值
  }
  # 或者
  POST /index/type/id
  {
  	"属性名":修改值
  }
  *****************修改*************************
  # 没有带上的属性会被清除
  POST /index/type/id
  {
  	"属性名":修改值
  }
  *****************查询*************************
  GET /index/type/id  
  *****************删除*************************
  # 只是逻辑删除, 将其标记为 `delete` ,数据越来越多时, ES会自动物理删除
  DELETE /index/type/id

实例#