@[ElasticSearch]
REST API
现在我们已经运行了节点和集群,下一步就是理解如何连接Elasticsearch
。Elasticsearch
提供了全面和强大的REST API
来跟Elasticsearch
集群相互作用。我们可以用REST API
来做一下这些事情:
- 检查集群,节点和索引的健康,状态和统计信息;
- 管理集群,节点和索引的数据和元数据;
- 对索引执行
CRUD (Create, Read, Update, and Delete)
和搜索操作; - 执行高级搜索操作,例如分页,排序,过滤,脚本,聚合等等。
集群健康
开始基本的健康检查,可以查看集群在做什么。本文使用curl
进行操作,但是你可以使用任何工具来进行调用REST/HTTP
请求。假设我们仍然在上文提到的运行着Elasticsearch
的节点上,重新打开一个终端窗口。
我们可以使用_cat API
来检查集群的健康,在这之前应当确认我们的节点在http
的9200端口可以访问:
curl 'localhost:9200/_cat/health?v'
我们可以得到如下的返回结果:
epoch timestamp cluster status node.total node.data shards pri relo init unassign
1394735289 14:28:09 elasticsearch green 1 1 0 0 0 0 0
我们可以看到我们的名字为elasticsearch
的集群的状态为green
。
集群健康分为三种,green
,yellow
,red
。Green的意思是所有状态都很好(集群功能全部可用),Yellow的意思是所有的数据都可以使用,但是某些副本没有被分配(集群功能全部可用),Red的意思是某些数据不可用。需要注意的是,即使状态为red,也有一部分数据可,但是你需要尽快修复它,因为你有丢失数据。
从上面的返回结果中我们可以看到节点数目只有一个,分片数目为0,因为我们还没有分配数据。需要注意的一点,因为我们使用了默认的集群名称(elasticsearch
),而且Elasticsearch
默认使用单播网络发现同一台机器中的节点,很有可能你会启动不仅仅一个节点。在这种情况下,你可能得到不仅仅一个节点。
我们也可以使用下面的命令得到节点列表:
curl 'localhost:9200/_cat/nodes?v'
下面是返回结果:
curl 'localhost:9200/_cat/nodes?v'
host ip heap.percent ram.percent load node.role master name
mwubuntu1 127.0.1.1 8 4 0.00 d * New Goblin
我们可以看到我们唯一的节点命名为New Goblin
。
列出所有的索引
现在我们可以看看我们的索引:
curl 'localhost:9200/_cat/indices?v'
返回结果为:
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size
上面说明我们现在还没有索引。
创建索引
现在我们创建个名字为customer
的索引重新列出所有的索引:
curl -XPUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
第一条命令使用PUT
创建一个名字为customer
的索引,我们添加pretty
参数的原因是将结果打印成为JSON
返回:
下面是结果:
curl -XPUT 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size
yellow customer 5 1 0 0 495b 495b
第二条结果告诉我们我们现在有一个名为customer
的索引,它有5个主要分片和1个复制分片(均为默认),它没有包含任何的文档。
你肯定观察到了customer
索引的状态为yellow,我们之前讨论过,yellow意味着复制分片有一部分没有分配。这里的原因是默认索引有一个复制分片,但是我们只有一个节点,而复制节点不会被分配到跟主要节点在同一个节点中。当有其他节点加入集群,复制分片被分配以后,yellow状态就会发生改变,成为green。
索引和查询文档
现在我们可以放一些数据到我们的索引中了,前文提到过,在索引文档之前,我们应当先指定类型。
我们现在索引一条数据,在customer
索引中,external
类型,文档的ID
为1:
我们的JSON
文档为:{ "name": "John Doe" }
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
返回结果为:
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"created" : true
}
从上面的结果中我们看到,一条customer
文档成功的插入到了customer
索引的external
类型中,文档还有一个我们在索引过程中指定的一个内部ID
为1。
Elasticsearch
不需要你在索引数据的时候显性的创建一个索引,在上面的例子中,Elasticsearch
可以自动的创建customer
索引,如果之前不存在的话。
现在我们可以取回我们刚索引的文档:
curl -XGET 'localhost:9200/customer/external/1?pretty'
返回结果为:
curl -XGET 'localhost:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : { "name": "John Doe" }
}
found
字段说明我们用ID
1找到了这条文档,_source
字段返回了之前我们索引的整条JSON
文档。
删除索引
现在删除我们刚才建立的索引,然后列出所有的索引:
curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
返回结果为:
curl -XDELETE 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size
结果说明索引删除成功,我们现在集群中没有任何的索引了。
在进行下面的之前,我们可以回顾一下我们目前学到的API
:
curl -XPUT 'localhost:9200/customer'
curl -XPUT 'localhost:9200/customer/external/1' -d '
{
"name": "John Doe"
}'
curl 'localhost:9200/customer/external/1'
curl -XDELETE 'localhost:9200/customer'
我们仔细看上面的命令,会发现我们访问Elasticsearch
数据的命令模式,可以总结为:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
这种REST
访问模式在API
中很常见,因此如果你可以记住的话,你将会在掌握Elasticsearch
过程中有一个良好的开始。