[toc]
一. 查看集群健康、节点、分片
1.查看集群健康度
GET _cluster/health
{
"cluster_name" : "elasticsearch",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 8,
"active_shards" : 8,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 3,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 72.72727272727273
}
2.查看节点信息
GET _cat/nodes
127.0.0.1 21 95 3 0.00 0.03 0.05 dilm * niewj
3. 查看分片信息
GET _cat/shards
.security-7 0 p STARTED 42 80.1kb 127.0.0.1 niewj
.apm-agent-configuration 0 p STARTED 0 283b 127.0.0.1 niewj
users 0 p STARTED 4 5.3kb 127.0.0.1 niewj
users 0 r UNASSIGNED
movies 0 p STARTED 9743 1.3mb 127.0.0.1 niewj
movies 0 r UNASSIGNED
.kibana_1 0 p STARTED 88 101.9kb 127.0.0.1 niewj
kibana_sample_data_flights 0 p STARTED 13059 6.3mb 127.0.0.1 niewj
.kibana_task_manager_1 0 p STARTED 2 12.5kb 127.0.0.1 niewj
my_index 0 p STARTED 2 10kb 127.0.0.1 niewj
my_index 0 r UNASSIGNED
二. index、create、update文档
4. index一个文档
4.1 第一次index
PUT books/_doc/1
{
"bookId":"1",
"bookName":"Thinking in Java",
"price": 99.99
}
结果:
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
再执行一次呢?
4.2 第一次index
PUT books/_doc/1
{
"bookId":"1",
"bookName":"Thinking in Java",
"author": "Bruce Eckel"
}
输出如下:(为了看出区别,加了个"author",减了个"price")
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
区别:
执行次数/区别 | 第一次 | 第二次 |
---|---|---|
_version | 1 | 2 |
result | created | updated |
_seq_no | 0 | 1 |
执行完后,index查询有没有"price"呢?有就说明是更新了, 没有就说明是覆盖了:
GET books/_search
{
"query": {"match_all": {}}
}
看看:
{
"took" : 1002,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"bookId" : "1",
"bookName" : "Thinking in Java",
"author" : "Bruce Eckel"
}
}
]
}
}
没price了!说明什么?
- index时,如果id已存在,则先删除->再增加->
_version
+_seq_no
都会增加; result
不同:第一次:created
; 第二次:updated
这里虽然是updated,但并不是只修改了原来的字段, 而是先删,再建,昔有今无的字段是没的了!
就想一个商品房:新买的主人住进去一年,卖给第二个人,updated的是房间的状态,原来的主人,基本不会在那儿了!
5. create一个文档
5.1 第1种create方式:PUT idx名/_create/id串
第1次create
PUT books/_create/3
{
"bookId": "3",
"bookName": "中国通史",
"author": "吕思勉"
}
{
"_index" : "books",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
第2次create
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[3]: version conflict, document already exists (current version [1])",
"index_uuid": "64ki-Bg-SkeC6mTRY-7AnA",
"shard": "0",
"index": "books"
}
],
"type": "version_conflict_engine_exception",
"reason": "[3]: version conflict, document already exists (current version [1])",
"index_uuid": "64ki-Bg-SkeC6mTRY-7AnA",
"shard": "0",
"index": "books"
},
"status": 409
}
直接:"status": 409了;报错了!
4xx的报错一般都是告诉你输入有问题
5.2 第2种create方式:POST idx名/_doc(自动生成ID)
POST books/_doc
{
"bookId": "4",
"bookName": "中国通史4",
"author": "吕思勉"
}
连续执行了多次:
{
"_index" : "books",
"_type" : "_doc",
"_id" : "OivOY4ABRxSL2QPxYNGs",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 10,
"_primary_term" : 1
}
- 每次_id都不一样
- _seq_no一直在增
因为每次都是存一个新文档了!查询看看:
{
"took" : 627,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"bookId" : "1",
"bookName" : "Thinking in Java",
"author" : "Bruce Eckel"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"bookId" : "2",
"bookName" : "中国通史",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"bookId" : "3",
"bookName" : "中国通史",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "NCvOY4ABRxSL2QPxNNHf",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "NSvOY4ABRxSL2QPxPdEy",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "NivOY4ABRxSL2QPxQdEo",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "NyvOY4ABRxSL2QPxRNHC",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "OCvOY4ABRxSL2QPxSdHA",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "OSvOY4ABRxSL2QPxTNHb",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "OivOY4ABRxSL2QPxYNGs",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "中国通史4",
"author" : "吕思勉"
}
}
]
}
}
这里就可以做一个小结:
6. index和create的区别
1.语法
index语法:PUT books/_doc/1
create语法:PUT books/_create/1
或者 POST books/_doc
2. 语义
index多次同一个id: 会删除重建,但是版本一直++;
create的PUT books/_create/1
方式第二次执行会报错,而不是++版本号!
create支持自动生成id:此时的多次执行就是索引多份document了;
7. update文档
update一个已有文档:
POST books/_update/NCvOY4ABRxSL2QPxNNHf
{
"doc": {
"bookName": "鲁迅散文",
"author": "鲁迅"
}
}
查询看看:bookId并没有丢失, 只是更新了赋值的字段
GET books/_search?q=_id:NCvOY4ABRxSL2QPxNNHf
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "NCvOY4ABRxSL2QPxNNHf",
"_score" : 1.0,
"_source" : {
"bookId" : "4",
"bookName" : "鲁迅散文",
"author" : "鲁迅"
}
}
]
}
}
使用 GET books/_doc/NCvOY4ABRxSL2QPxNNHf
可以看到版本:版本也发生了变化:update也会增加版本!
{
"_index" : "books",
"_type" : "_doc",
"_id" : "NCvOY4ABRxSL2QPxNNHf",
"_version" : 2,
"_seq_no" : 11,
"_primary_term" : 1,
"found" : true,
"_source" : {
"bookId" : "4",
"bookName" : "鲁迅散文",
"author" : "鲁迅"
}
}
三.创建索引
创建索引的3点总结
- 当index或者create文档时,如果索引不存在,就会自动创建索引;
- 同时mapping和setting会使用默认的(也可以自定义模板,如果模板匹配,模板就会起作用,此时创建的索引的mapping和setting就会被index template定义的内容约束)
- 默认mapping是dynamic=true的,就是有新增的字段,自动识别猜测类型;dynamic=false/strict时,就不会索引新的字段,区别是:strict直接报错不让增加新字段;false可以新增,_source里也会存入,但是字段不会被分词和生成索引,作为搜索条件去查询是也会报错!
四.删除索引
DELETE books
删除后再去查:GET books/_doc/1
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [books1]",
"resource.type" : "index_expression",
"resource.id" : "books1",
"index_uuid" : "_na_",
"index" : "books1"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [books1]",
"resource.type" : "index_expression",
"resource.id" : "books1",
"index_uuid" : "_na_",
"index" : "books1"
},
"status" : 404
}