这里主要是使用命令对elasticsearch进行管理,不需要借助其他客户端工具
环境说明:
ES服务端:192.168.2.100 端口9200
# 查看索引的状态
curl -XGET http://192.168.2.100:9200/_cat/indices?v
# 关闭索引
curl -X POST http://192.168.2.100:9200/[索引名]/_close?pretty
# 打开索引
curl -X POST http://192.168.2.100:9200/[索引名]/_open?pretty
# 查看ES健康状态
curl -XGET http://192.168.2.100:9200/_cat/health?v
# 查看ES 节点
curl -XGET http://192.168.2.100:9200/_cat/nodes?v
# 查看所有索引的分片情况
curl -XGET http://192.168.2.100:9200/_cat/shards?v
# 查看指定索引的分配情况
curl -XGET http://192.168.2.100:9200/_cat/shards/[索引名]?v
# 查看正在执行的任务
curl -XGET http://192.168.2.100:9200/_cat/tasks?v
# 查看安装的插件
curl -XGET http://192.168.2.100:9200/_cat/plugins?v
-------------------------------------------------------
/_cat/allocation #查看单节点的shard分配整体情况
/_cat/shards #查看各shard的详细情况
/_cat/shards/{index} #查看指定分片的详细情况
/_cat/master #查看master节点信息
/_cat/nodes #查看所有节点信息
/_cat/indices #查看集群中所有index的详细信息
/_cat/indices/{index} #查看集群中指定index的详细信息
/_cat/segments #查看各index的segment详细信息,包括segment名, 所属shard, 内存(磁盘)占用大小, 是否刷盘
/_cat/segments/{index}#查看指定index的segment详细信息
/_cat/count #查看当前集群的doc数量
/_cat/count/{index} #查看指定索引的doc数量
/_cat/recovery #查看集群内每个shard的recovery过程.调整replica。
/_cat/recovery/{index}#查看指定索引shard的recovery过程
/_cat/health #查看集群当前状态:红、黄、绿
/_cat/pending_tasks #查看当前集群的pending task
/_cat/aliases #查看集群中所有alias信息,路由配置等
/_cat/aliases/{alias} #查看指定索引的alias信息
/_cat/thread_pool #查看集群各节点内部不同类型的threadpool的统计信息,
/_cat/plugins #查看集群各个节点上的plugin信息
/_cat/fielddata #查看当前集群各个节点的fielddata内存使用情况
/_cat/fielddata/{fields} #查看指定field的内存使用情况,里面传field属性对应的值
/_cat/nodeattrs #查看单节点的自定义属性
/_cat/repositories #输出集群中注册快照存储库
/_cat/templates #输出当前正在存在的模板信息
----------------------------------------------------------
#直接创建索引,不指定mapping
curl -XPUT '192.168.2.100:9200/{index}'
例:
curl -XPUT '192.168.2.100:9200/customer'
#设定mapping信息创建索引
curl -XPUT '192.168.2.100:9200/{index}' -d '{}'
例:
curl -XPUT '192.168.2.100:9200/customer' -d '
{
"mappings": {
"external": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}'
# 插入数据
curl -XPUT '192.168.2.100:9200/{index}/{type}/{id}?pretty' -d '{}'
#在customer索引下类型为external,插入id为1的数据,数据体必须为json格式
curl -XPUT '192.168.2.100:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
# 查询数据
curl -XGET '192.168.2.100:9200/{index}/{type}/{id}?pretty'
#获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观
curl -XGET '192.168.2.100:9200/customer/external/1?pretty'
# 修改数据 完全覆盖
curl -XPUT '192.168.2.100:9200/{index}/{type}/{id}?pretty' -d'{}'
#在customer索引下类型为external,插入id为1的数据,数据体必须为json格式,相同id的数据会被覆盖
curl -XPUT '192.168.2.100:9200/customer/external/1?pretty' -d'
{
"name": "John Doe xxxxxxx"
}'
# 更新数据 更新指定字段,其他字段不受影响
#只更新name字段的信息
curl -XPOST '192.168.2.100:9200/customer/external/1/_update?pretty' -d'
{
"doc": {
"name": "Jane Doe"
}
}'
#更新name字段的信息,同时还新增了age字段
curl -XPOST '192.168.2.100:9200/customer/external/1/_update?pretty' -d '
{
"doc": {
"name": "Jane Doe",
"age": 20
}
}'
#使用脚本方式进行更新,age自增5
curl -XPOST '192.168.2.100:9200/customer/external/1/_update?pretty' -d'
{
"script": "ctx._source.age += 5"
}'
# 删除数据
#删除customer索引下类型为external的ID为2的数据
curl -XDELETE '192.168.2.100:9200/customer/external/2?pretty'
# 删除索引
curl -XDELETE '192.168.2.100:9200/customer'
#检查es版本信息
curl -u <user>:<passwd> http://hostname:9200
#此时观察ES集群状态:
curl -XGET http://hostname:9200/_cluster/health?pretty
# 查询es 所有node 信息
curl -XGET 'http://10.50.30.147:9200/_cat/nodes'
# 查询es 健康状况
curl -XGET 'http://10.50.30.147:9200/_cat/health'
# 查看主节点
curl -XGET 'http://10.50.30.147:9200/_cat/master'
#查看所有索引
curl -XGET 'http://10.50.30.147:9200/_cat/indices'
# 创建索引
curl -X PUT "hostname:9200/test_index?pretty" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards" : 5,
"number_of_replicas" : 0
}
}
'
# 修改索引 index的shard数量定好后,就不能再修改
curl -X PUT "hostname:9200/test_index/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"number_of_replicas": 1
}'
#观察集群内各索引状态:
curl http://hostname:9200/_cat/indices?pretty
#查询elasticsearch节点状态:
curl -XGET http://hostname:9200/_cat/shards |grep UNASSIGNED
# 删除索引信息
curl -X DELETE "hostname:9200/test-index"
# 查看任务堆积情况
curl -XGET http://hostname:9200/_cat/pending_tasks
#列出所有索引及存储大小
curl 'http://hostname:9200/_cat/indices?v'
#创建索引
创建索引名为XX,默认会有5个分片,1个副本
curl -XPUT 'http://hostname:9200/XX?pretty'
# 查询每个shard 的大小
curl -s -XGET "http://hostname:9200/_cat/shards?v&h=index,shard,docs,store" -H 'Content-Type: application/json'
index shard docs store
test_index 2 3819 181.9kb
test_index 2 3819 171.4kb
test_index 1 3834 182.7kb
test_index 1 3834 184.4kb
test_index 4 3832 173.5kb
test_index 4 3832 173.5kb
test_index 3 3807 173.9kb
test_index 3 3807 173.9kb
test_index 0 3908 189.9kb
test_index 0 3908 189kb
#创建索引名为qsh_test ,有10个分片,2个副本
curl -XPUT http://hostname:9200/qsh_test -d '{
"settings": {
"number_of_shards": 10,
"number_of_replicas": 2
}
}'
查询索引分片信息
curl -X GET "hostname:9200/_cat/shards?v&pretty"
#添加一个类型
curl -XPUT 'http://hosname:9200/XX/external/2?pretty' -d '
{
"gwyy": "John"
}'
#更新一个类型
curl -XPOST 'http://hostname:9200/XX/external/1/_update?pretty' -d '
{
"doc": {"name": "Jaf"}
}'
#删除指定索引
curl -XDELETE 'http://hosname:9200/_index?pretty'
#删除索引:booklist 下的所有数据
curl -XPOST http://<ip>:<port>/booklist/_delete_by_query?pretty -d '{
"query": {
"match_all": {
}
}
}'
# 设置 minimum_master_nodes 为 2 确保一个是active 一个standby
curl -XPUT 'http://hostname:9200/_cluster/settings' -H 'Content-Type: application/json' -d '{
"persistent" : {
"discovery.zen.minimum_master_nodes" : 2
}
}'
# 从集群中剔除某个节点或者实例
curl -XPUT http://hostname:9200/_cluster/settings?pretty -H 'Content-Type: application/json' -d '{
"transient": {
"cluster.routing.allocation.exclude._name": "{node.name}"
}
}'
上面其实会触发分片的 Allocation 机制,涉及的参数为 cluster.routing.allocation.exclude.{attribute},其中 {attribute} 表示节点的匹配方式,支持三种:
_name:匹配 node 名称,多个 node 名称用逗号隔开;
_ip:匹配 node ip 地址,多个地址用逗号隔开;
_host:匹配 node 主机名,多个主机名用逗号隔开;
执行后将导致 {node.name} 节点上的分片慢慢迁移到其他节点,可能会花几分钟甚至更多的时间,期间不会影响正常业务
curl -s "http://hostname:9200/_cat/shards" | grep RELOCATING
curl http://hostname:9200/_cluster/health?pretty