现在我们已经启动并运行了节点(和集群),下一步是了解如何与它进行通信。 幸运的是,Elasticsearch提供了一个非常全面和强大的REST API,您可以使用它与集群进行交互。 使用API可以完成的一些事项如下:
- 检查群集,节点和索引运行状况,状态和统计信息
- 管理您的群集,节点和索引数据和元数据
- 对索引执行CRUD(创建,读取,更新和删除)和搜索操作
- 执行高级搜索操作,例如分页,排序,过滤,脚本编写,聚合等等
1. 集群的健康
浏览器访问:http://localhost:9200/_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
1547524888 04:01:28 my_cluster_name green 1 1 2 2 0 0 0 0 - 100.0%
通过Kibana访问:
我们可以看到名为“elasticsearch”的群集处于绿色状态。
每当我们要求群集健康时,我们要么获得绿色,黄色或红色。
- 绿色 - 一切都很好(集群功能齐全)
- 黄色 - 所有数据均可用,但尚未分配一些副本(群集功能齐全)
- 红色 - 某些数据由于某种原因不可用(群集部分功能)
注意:当群集为红色时,它将继续提供来自可用分片的搜索请求,但您可能需要尽快修复它,因为存在未分配的分片。
同样从上面的响应中,我们可以看到总共1个节点,并且我们有0个分片,因为我们还没有数据。 请注意,由于我们使用的是默认群集名称(my_cluster_name ),并且由于my_cluster_name 默认使用单播网络发现来查找同一台计算机上的其他节点,因此您可能会意外启动计算机上的多个节点并拥有它们 所有人都加入一个集群。 在这种情况下,您可能会在上面的响应中看到多个节点。
我们还可以获得群集中的节点列表,如下所示:
GET /_cat/nodes?v
浏览器访问:http://localhost:9200/_cat/nodes?v
结果如下:
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 51 82 10 mdi * my_node_name
通过Kibana访问:
在这里,我们可以看到一个名为“my_node_name”的节点,它是我们集群中当前的单个节点。
2 列出所有索引
GET /_cat/indices?v
访问:http://localhost:9200/_cat/indices?v
结果:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 YiK6qXrvT2-dsiQplb-3ww 1 0 18 0 897.1kb 897.1kb
green open kibana_sample_data_ecommerce D92tAKGTSJCKUUDdL8NpPQ 1 0 4675 0 4.7mb 4.7mb
3. 创建索引
PUT /customer?pretty
结果如下:
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0;
# if you wish to continue using the default of [5] shards,
# you must manage this on the create index request or with an index template
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "customer"
}
再次查询:http://localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 YiK6qXrvT2-dsiQplb-3ww 1 0 18 0 897.1kb 897.1kb
green open kibana_sample_data_ecommerce D92tAKGTSJCKUUDdL8NpPQ 1 0 4675 0 4.7mb 4.7mb
yellow open customer -ySb9KIuRFOTEqr4Kq_JDw 5 1 0 0 1.2kb 1.2kb
我们现在有一个名为customer的索引,它有5个主分片和1个副本(默认值),并且它包含0个文档。
注意到customer索引标记了黄色运行状况。 回想一下我们之前的讨论,黄色表示某些副本尚未(尚未)分配。 此索引发生这种情况的原因是因为默认情况下Elasticsearch为此索引创建了一个副本。 由于我们目前只有一个节点在运行,因此在另一个节点加入集群的较晚时间点之前,尚无法分配一个副本(用于高可用性)。 将该副本分配到第二个节点后,此索引的运行状况将变为绿色。
4 索引和查询文档
现在让我们在customer索引中加入一些内容。 我们将一个简单的客户文档索引到客户索引中,ID为1,如下所示:
PUT /customer/_doc/1?pretty
{
"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
}
值得注意的是,Elasticsearch在您将文档编入索引之前不需要先显式创建索引。 在前面的示例中,如果客户索引事先尚未存在,则Elasticsearch将自动创建客户索引。
测试如下:
PUT /customer2/_doc/1?pretty
{
"name": "John Doe"
}
返回结果:
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0;
# if you wish to continue using the default of [5] shards,
# you must manage this on the create index request or with an index template
{
"_index" : "customer2",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
现在检索刚刚编入索引的文档:
GET /customer/_doc/1?pretty
结果:
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
字段解释:
found:说明我们查询的结果状态
_source:full JSON document
5. 删除索引
DELETE /customer?pretty
结果:
{
"acknowledged" : true
}
总结
1. PUT /customer
2. PUT /customer/_doc/1
{
"name": "John Doe"
}
3. GET /customer/_doc/1
4. DELETE /customer