[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 niewjusers 0 p STARTED 4 5.3kb 127.0.0.1 niewjusers 0 r UNASSIGNED movies 0 p STARTED 9743 1.3mb 127.0.0.1 niewjmovies 0 r UNASSIGNED .kibana_1 0 p STARTED 88 101.9kb 127.0.0.1 niewjkibana_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 niewjmy_index 0 p STARTED 2 10kb 127.0.0.1 niewjmy_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}