共计 9039 个字符,预计需要花费 23 分钟才能阅读完成。
1. _cat 查看信息
1.1. _cat/health 查看集群健康状况
1602493310 09:01:50 elasticsearch yellow 1 1 10 10 0 0 5 0 – 66.7%
1.2. _cat/master 查看主节点信息
bQY8I8hFSjmCHO_1-MKfZg 127.0.0.1 127.0.0.1 83c34a872f4c
1.3 _cat/indices 查看索引信息
green open .security-7 M61Q-ekUQmGZP1FtS8ukfQ 1 0 42 0 74kb 74kb
yellow open niewj UuJpWw6UQcKBH8-TWquNng 5 1 0 0 1.3kb 1.3kb
green open .kibana_task_manager_1 1ebsopUOSN-P8ToMX-mklA 1 0 2 0 22.5kb 22.5kb
green open .apm-agent-configuration Le9Ig6qiSdaxMRLvmg43Lg 1 0 0 0 283b 283b
green open kibana_sample_data_logs jo7AsHgeTniG509NZWUi-A 1 0 14074 0 11.7mb 11.7mb
green open .kibana_1 o1ntzX2YRFGfU__BzFPYhg 1 0 47 0 109.1kb 109.1kb
1.4 其余 _cat 命令
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
2. put/post 新增和批改二合一
留神下划线开始的属性是元数据
2.1 带 ID 的 PUT 和 POST
2.1.1 带 ID 的 put
PUT customer/_doc/1
{
"name": "niewj",
"age": 20,
"gender": "male"
}
间断两次申请后果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 21,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 24,
"_primary_term" : 1
}
---
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 22,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 25,
"_primary_term" : 1
}
可见: _seq_no 和 version 都呈现了新增, 每次都是 updated;
2.1.2 带 ID 的 post 跟 put 一样
2.2 不带 ID 的 put= 报错 405
put customer/_doc/
{
"name": "niewj",
"age": 20,
"gender": "male"
}
报错 405:
{"error": "Incorrect HTTP method for uri [/customer/_doc/?pretty] and method [PUT], allowed: [POST]",
"status": 405
}
2.3 不带 ID 的 post= 每次生成随机新 ID
POST customer/_doc/
{
"name": "niewj",
"age": 20,
"gender": "male"
}
间断两次申请后果: 每次都能够胜利
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "BvNkUHUB7InhghdEZD0I",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 26,
"_primary_term" : 1
}
---
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "B_NkUHUB7InhghdEqD3G",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 27,
"_primary_term" : 1
}
然而每次都生成不同的 ”_id”, 也就是说: 每次都是新的数据!
3. POST+_update 的更新: 查看更新
#POST customer/_doc/1/_update
POST customer/_update/1
{
"doc":{
"name": "niewj",
"age": 20,
"gender": "male"
}
}
留神:
POST customer/_doc/1/_update 这种形式不再举荐 (因为 type 机制移除的起因), 如果应用下面的
POST customer/_doc/1/_update: 会提醒:
#! Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
POST customer/_update/1 间断屡次申请的后果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 22,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 25,
"_primary_term" : 1
}
---
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 22,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 25,
"_primary_term" : 1
}
可见, 屡次申请无变动, post+_update 的形式会查看内容的变动, 如果无变动, 则不更新, 有变动才更新; 单纯 post 和 put 的形式, 是会每次更新的, 不查看内容; 另外须要留神的是 post+_update 的调用形式, 须要把数据放在 doc 构造里:
POST customer/_update/1
{
"doc":{
"name": "niewj",
"age": 20,
"gender": "male"
}
}
4. get 查问数据
4.1 指定 ID 的查问:
GET customer/_doc/1
查问后果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 22,
"_seq_no" : 25,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "niewj",
"age" : 20,
"gender" : "male"
}
}
4.2 间接 GET 索引名: 返回的是索引的定义信息
GET customer
查问后果:
{
"customer" : {"aliases" : {},
"mappings" : {
"properties" : {
"age" : {"type" : "long"},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1602493864109",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "j5DyzqgTTnG9aWRnAgHVmA",
"version" : {"created" : "7040299"},
"provided_name" : "customer"
}
}
}
}
包含: mappings/alias/settings, 其中 mappings 包含 properties, 它的内容就是文档数据的字段类型的阐明:
age/gender/name 等索引字段的定义阐明
5. 乐观锁和并发批改:_seq_no 和_primary_term
5.1 乐观锁字段阐明
_seq_no: 文档版本号, 作用同 version
_primary_term: 文档所在位置
咱们在并发批改的时候, 指定文档序列号 (_seq_no):
5.2 并发批改模仿
- 咱们先查问看看:
GET customer/_doc/1
数据:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 22,
"_seq_no" : 25,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "niewj",
"age" : 20,
"gender" : "male"
}
}
- 并发批改时:
PUT customer/_doc/1?if_seq_no=25&if_primary_term=1
{
"name": "jack",
"age": 22
}
后果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 23,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 29,
"_primary_term" : 1
}
- 在此查问 看后果:
GET customer/_doc/1
后果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 23,
"_seq_no" : 29,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "jack",
"age" : 22
}
}
文档已更新;(留神是全量更新)
- 此时如果再次调用, 就会报错, 因为_seq_no 曾经产生了扭转:
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [25], primary term [1]. current document has seqNo [29] and primary term [1]",
"index_uuid": "j5DyzqgTTnG9aWRnAgHVmA",
"shard": "0",
"index": "customer"
}
],
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, required seqNo [25], primary term [1]. current document has seqNo [29] and primary term [1]",
"index_uuid": "j5DyzqgTTnG9aWRnAgHVmA",
"shard": "0",
"index": "customer"
},
"status": 409
}
6. 删除文档和索引
6.1 删除 document
DELETE customer/_doc/1
后果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 24,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 30,
"_primary_term" : 1
}
--- 再次删除时会:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 31,
"_primary_term" : 1
}
第一次调用会胜利删除 deleted, 再次调用就: not_found
6.2 删除 index
es 去掉了 type 的概念之后 (其实之前也没有删除 type 的机制), 就更明了了:
删除索引:
DELETE customer
result:
{"acknowledged" : true}
--- 再次调用时报错:
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [customer]",
"resource.type" : "index_or_alias",
"resource.id" : "customer",
"index_uuid" : "_na_",
"index" : "customer"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [customer]",
"resource.type" : "index_or_alias",
"resource.id" : "customer",
"index_uuid" : "_na_",
"index" : "customer"
},
"status" : 404
}
第一次删除索引时胜利 acknowledged=true, ; 再次删除因为没有了, 所以报错!
7. bulk 批量导入数据:post+_bulk
7.1 批量执行 3 个文档的插入:
POST customer/_bulk
{"index": {"_id": "1"}}
{"name": "niewj"}
{"index": {"_id": "2"}}
{"name": "jack"}
{"index": {"_id": "5"}}
{"name": "michel"}
第一行是元信息, 随后的一行是具体的数据, 此处的操作是, 批量执行 3 个索引文档的操作, 每个操作有本人独立的后果 item 信息:
{
"took" : 6,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 10,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 11,
"_primary_term" : 1,
"status" : 201
}
}
]
}
每个 item 是有各自的操作后果的! -> “result” : “created”
7.2 混合的批量操作
POST customer/_bulk
{"delete": {"_id": "1"}}
{"delete": {"_id": "2"}}
{"delete": {"_id": "5"}}
{"index": {"_id": "1"}}
{"name": "niewj01"}
{"index": {"_id": "2"}}
{"name": "niewj02"}
混合的批量操作: 3 个 delete 文档的操作, 2 个索引文档的操作, 所以这个后果应该有 5 个 items 元素:
{
"took" : 28,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 12,
"_primary_term" : 1,
"status" : 200
}
},
{
"delete" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 13,
"_primary_term" : 1,
"status" : 200
}
},
{
"delete" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "5",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 14,
"_primary_term" : 1,
"status" : 200
}
},
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 15,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "_doc",
"_id" : "2",
"_version" : 3,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 16,
"_primary_term" : 1,
"status" : 201
}
}
]
}
下面的批量操作都是在一个 index 下的, 其实也能够不指定 index, 在元信息中指定, 这样, 能够对多个文档进行批量操作, 具体操作略~
7.3 批量初始化官网的 accounts.json 案例数据
accounts.json 数据内容
POST bank/_bulk
{"index":{"_id":"938"}}
{"account_number":938,"balance":9597,"firstname":"Sharron","lastname":"Santos","age":40,"gender":"F","address":"215 Matthews Place","employer":"Zenco","email":"sharronsantos@zenco.com","city":"Wattsville","state":"VT"}
{"index":{"_id":"940"}}
{"account_number":940,"balance":23285,"firstname":"Melinda","lastname":"Mendoza","age":38,"gender":"M","address":"806 Kossuth Place","employer":"Kneedles","email":"melindamendoza@kneedles.com","city":"Coaldale","state":"OK"}
...... 数据的内容粘贴
后果:
{
"took" : 612,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "bank",
"_type" : "_doc",
"_id" : "6",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},......
]}