Elasticsearch 6.1官方参考手册(一)入门(3)集群入门学习

探究你的集群

REST API

既然咱们的节点(集群)已经安装成功而且已经启动运行,那么下一步就是去了解如何去操做它。幸运的是,Elasticsearch提供了很是全面和强大的REST API,咱们能够经过它去跟集群交互。经过API咱们能够完成以下的功能:html

  • 检查集群,节点和索引的健康情况,状态和统计数据
  • 管理集群,节点和索引的数据和原数据
  • 执行CRUD(增删改查)操做,依靠索引进行搜索
  • 执行高级搜索操做,好比分页,排序,过滤,脚本化,汇集等等

集群健康监控

让咱们从一个简单的健康检查开始,经过这个咱们能够了解咱们集群的运行状况。咱们将使用curl工具来作这个测试,固然你可使用任何能够发送HTTP/REST请求的工具。让咱们假设咱们依然在以前已启动的Elasticsearch节点上而且打开了另外一个shell窗口。node

咱们将使用 _cat API 去检查集群健康状态。HTTP请求内容为:shell

GET /_cat/health?v

你能够经过点击VIEW IN ConsoleKibana Console中运行命令,或者直接执行以下curl命令:json

curl -XGET 'localhost:9200/_cat/health?v&pretty'

响应结果为:网络

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%

咱们能够看到咱们的名称为“elasticsearch”的集群正在运行,状态标识为greenapp

不管什么时候查看集群健康状态,咱们会获得greenyellowred中的任何一个。curl

  • Green - 一切运行正常(集群功能齐全)
  • Yellow - 全部数据是能够获取的,可是一些复制品尚未被分配(集群功能齐全)
  • Red - 一些数据由于一些缘由获取不到(集群部分功能不可用)

注意:当一个集群处于red状态时,它会经过可用的分片继续提供搜索服务,可是当有未分配的分片时,你须要尽快的修复它。elasticsearch

另外,从上面的返回结果中咱们能够看到,当咱们里面没有数据时,总共有1个节点,0个分片。注意当咱们使用默认的集群名称(elasticsearch)而且当Elasticsearch默认使用单播网络发如今同一台机器上的其它节点时,极可能你会在你电脑上不当心启动不止一个节点而且他们都加入了一个集群。在这种状况下,你可能会从上面的返回结果中看到不止一个节点。ide

咱们也能够经过以下请求获取集群中的节点列表:工具

Http请求体

GET /_cat/nodes?v

Curl命令

curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from= https://www.elastic.co/guide/...

返回结果为:

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           10           5   5    4.46                        mdi      *      PB2SGZY

这里,咱们能够看到咱们的一个节点名称叫作“PB2SGZY”,它是目前咱们集群中的惟一的节点。

列出全部的索引

如今让咱们来大概看一看咱们的索引:
Http请求内容:

GET /_cat/indices?v

Curl命令

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from= https://www.elastic.co/guide/...

获得的返回结果为:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

这个返回结果只是一个表头,简单说就是咱们的集群中尚未任何索引。

建立一个索引

如今让咱们建立一个索引,名称为“customer”,而后再一次列出全部的索引:

Http请求内容:

PUT /customer?pretty

GET /_cat/indices?v

Curl命令

curl -XPUT 'localhost:9200/customer?pretty&pretty'

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from= https://www.elastic.co/guide/...

第一个命令使用PUT方法建立了一个名为“customer”的索引。咱们简单的在请求后面追加pretty参数来使返回值以格式化过美观的JSON输出(若是返回值是JSON格式的话)。

而后它的返回结果为:

第一个命令:
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
第二个命令:
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer 95SQ4TSUT7mWBT7VNHH67A   5   1          0            0       260b           260b

第二个命令的返回结果告诉咱们,咱们如今有1个名称为“customer”的索引,而且有5个主分片和1个拷贝(默认状况),而且里面包含0个文档。

你可能也注意到,这个customer索引的健康状态是yellow,回忆咱们以前讨论过的,yellow的意思是有一些拷贝尚未被分配。索引起生这种状况的缘由是Elasticsearch默认为当前索引建立一个拷贝。可是当前咱们只启动了一个节点,这个拷贝直到一段时间后有另外一个节点加入集群以前,不会被分配(为了高可用,拷贝不会与索引分配到同一个节点上)。一旦拷贝在第二个节点上得到分配,这个索引的健康状态就会变成green。

索引和文档查询

如今让咱们往customer索引中放点东西。以下请求将一个简单的顾客文档放入customer索引中,这个文档有一个ID为1:

Http请求内容:

PUT /customer/doc/1?pretty
{
  "name": "John Doe"
}

Curl命令

curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d '{"name": "John Doe"}'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_index_and_query_a_document/1.json

返回结果为:

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

从上面咱们能够看到,一个新的顾客文档已经在customer索引中成功建立。同时这个文档有一个本身的id,这个id就是咱们在将文档加入索引时指定的。

这里有一个重要的注意点,你不须要在将一个文档加入一个索引前明确的将这个索引预先建立好。在上面咱们建立文档的例子中,若是这个customer索引事先不存在,Elasticsearch会自动建立customer索引。

如今让咱们获取刚刚加入索引的文档:

Http请求体:

GET /customer/doc/1?pretty

Curl命令

curl -XGET 'localhost:9200/customer/doc/1?pretty&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_index_and_query_a_document/2.json

返回结果为:

{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : { "name": "John Doe" }
}

这里没有什么不寻常的,除了一个属性found,这个found属性表示咱们经过请求ID为1发现了一个文档,还有另外一个属性_source,_source属性返回咱们在上一步中加入索引的完整JSON文档内容。

删除一个索引

如今让咱们删除刚刚建立的索引而且再次列出全部的索引:

Http请求内容:

DELETE /customer?pretty
GET /_cat/indices?v

Curl命令

curl -XDELETE 'localhost:9200/customer?pretty&pretty'
curl -XGET 'localhost:9200/_cat/indices?v&pretty'

Kibana Console

http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_delete_an_index/1.json

第一个命令的返回结果为:

{
  "acknowledged" : true
}

第二个命令的返回结果为:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

以上结果意味着咱们的索引已经被删除,而且咱们回到了刚开始集群中什么都没有的地方。

在咱们继续前进以前,让咱们来仔细看一下到目前为止学习的这些API命令:

PUT /customer
PUT /customer/doc/1
{
  "name": "John Doe"
}
GET /customer/doc/1
DELETE /customer

若是咱们在学习上面的命令时很是仔细的话,咱们必定会发如今Elasticsearch中访问数据的模式。这个模式能够总结为如下形式:

<REST Verb> /<Index>/<Type>/<ID>

这种REST访问模式遍及全部的API命令,若是简单的记住它,你将会在掌握Elasticsearch的过程当中有一个很好的开端。

以下是我在上述章节实际作的操做:

[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/health?v&pretty'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1514269983 14:33:03  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/health?v&pretty'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1514270109 14:35:09  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/nodes?v&pretty'
ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1            7          93   0    0.00    0.01     0.05 mdi       *      sEicoNR
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
[root@bogon elasticsearch-6.1.1]# curl -XPUT 'localhost:9200/customer?pretty&pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer Azxs-a4FQnGKgAj0zdWXxQ   5   1          0            0      1.1kb          1.1kb
[root@bogon elasticsearch-6.1.1]# curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d '{"name": "John Doe"}'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/customer/doc/1?pretty&pretty'
{
  "_index" : "customer",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}
[root@bogon elasticsearch-6.1.1]# curl -XDELETE 'localhost:9200/customer?pretty&pretty'
{
  "acknowledged" : true
}
[root@bogon elasticsearch-6.1.1]# curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
[root@bogon elasticsearch-6.1.1]#
相关文章
相关标签/搜索