集群健康检查
curl -X GET "localhost:9200/_cat/health?v&pretty"
##或
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
1655337700 00:01:40 elasticsearch green 1 1 5 5 0 0 0 0 - 100.0%
- cluster:集群名字
- status:集群状态
- green:所有都是正常的,集群功能完好
- yellow:所有的数据是可用的,但是有些副本没有节点分配,集群功能完好
- red:一些数据由于某种原因不可用,集群部分功能可用,集群还可以继续通过可用的分片提供搜索服务,因为有未分配的分片,所以需要尽快修复
- node.total:节点总个数
- node.data:数据节点个数(es集群中的节点也有不同角色,不同的分工,后面的章节介绍)
- shards:总分片数
- pri:主分片数
- repo:副本分片数
列出所有的节点
GET /_cat/nodes?v
##或
curl -X GET "localhost:9200/_cat/nodes?v&pretty"
##输出
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.231.131 37 39 1 0.23 0.18 0.17 mdi * Y-afW_W
- heap.percent:Jvm 堆使用率
- ram.percent:内存使用率
- load-*:系统负载(分别是1分钟、5分钟、15分钟)
- node.role:节点角色
- master:是否是master
查看所有的索引
curl -X GET "localhost:9200/_cat/indices?v&pretty"
##或
GET /_cat/indices?v
##输出
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana CNoDAzhJQdCnmaiHAym3-Q 1 0 1 0 4kb 4kb
green open .monitoring-kibana-6-2022.06.15 WM9M8NmtRRCycOEzno6uJw 1 0 17 0 116.8kb 116.8kb
green open .monitoring-es-6-2022.06.15 _Hkcoh_OQiyLDl3-2ogdjQ 1 0 116 21 449.3kb 449.3kb
创建索引
创建一个名为customer的索引
PUT /customer?pretty
##或
curl -X PUT "localhost:9200/customer?pretty"
索引和查询文档
索引一条文档
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
##或
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'{
"name": "John Doe"
}'
根据id 查询文档
GET /customer/_doc/1?pretty
##或
curl -X GET "localhost:9200/customer/_doc/1?pretty"
删除索引
DELETE /customer?pretty
##或
curl -X DELETE "localhost:9200/customer?pretty"
索引/更新/替换整条文档
索引一条新的数据
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
##或
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
更新或替换整个文档
只需要使用PUT 方法,指定相同的 ID 就可以实现替换和更新整个文档。
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
##或
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
当使用POST 方法索引文档时,可以指定 ID 也可以不指定 ID;当不指定 ID时,Elasticsearch 可以生成随机 ID;
POST /customer/_doc?pretty
{
"name": "Jane Doe"
}
##或
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
当不指定 ID时,只能使用POST 方法,当指定 ID 时,POST/PUT 方法都可以。当PUT 方法不指定 ID 时,会报错
nsh@nsh:~/program/elasticsearch$ curl -XPUT "http://127.0.0.1:9200/customer/doc?pretty" -H 'Content-Type:application/json' -d '{"name":"tom","age":24}'
{
"error" : "Incorrect HTTP method for uri [/customer/doc?pretty] and method [PUT], allowed: [POST]",
"status" : 405
}
更新文档
只更新文档某个字段或添加新的字段,如下所示,当文档中存在addr 字段时,进行更新,不存在addr字段时,添加字段。
POST /customer/_doc/1/_update?pretty
{"doc":{"addr":"henansheng"}}
##或
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{"doc":{"addr":"henansheng"}}
'
通过脚本更新字段
POST /customer/_doc/1/_update?pretty
{
"script" : "ctx._source.age += 5"
}
##或
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}'
- ctx._source:指向的文档本身
- ctx._source.age:指向的是文档的age字段
删除文档
通过文档 ID 删除文档
DELETE /customer/_doc/2?pretty
##或
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
批处理
_bulk 请求每一行都需要通过 换行 分割
POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
##或
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
##每一行都需要换行,下面这种写法会报错
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }'
##上面的命令的输出
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
}
],
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
},
"status" : 400
}
同一个请求中可以有多种请求
POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
##或
##记得换行
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
_bulk 请求不会因为某一个是一个失败而导致整个失败,如果其中的一个action不论什么原因失败,都会继续处理其他文档,在整个_bulk 请求返回时,返回按照提交的顺序依次返回每一个action的状态。