221rrere 发表于 2016-6-27 09:59:07

探索Elasticsearch集群API

 Elasticsearch提供了一个非常全面和强大的REST API,您可以使用与您的集群进行交互。为数不多的可以用API的事情如下:
[*]检查您的集群、节点和索引健康状态和统计数据

[*]管理集群、节点和索引数据和元数据

[*]执行CRUD(创建、读取、更新和删除)索引和搜索操作

[*]执行高级搜索操作,比如分页、排序、过滤、脚本、聚合,和许多其他人


1. 可以使用 _cat API查看elasticsearch的健康状况,但是启动时不能以守护进程方式启动。

1
2
3
#curl '127.0.0.1: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
1463648421 17:00:21elasticsearch green         1         1      0   0    0    0      0             0                  -                100.0%




健康状态有三个状态:

[*]    Green:集群一切正常

[*]    Yellow:数据可用,副本不可用

[*]    Red:部分数据不可用,也可能有部分数据能用。


2. 查看节点列表

1
2
3
# curl '127.0.0.1:9200/_cat/nodes?v'
host      ip      heap.percent ram.percent load node.role master name   
127.0.0.1 127.0.0.1            2          43 0.00 d         *      Analyzer





3. 查看索引

1
2
# curl '127.0.0.1:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size




没有任何索引

4. 创建“customer”并查看索引

1
2
3
4
5
6
7
# curl -XPUT '127.0.0.1:9200/customer?pretty'
{
"acknowledged" : true
}
# curl '127.0.0.1:9200/_cat/indices?v'
health status index    pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer   5   1          0            0       260b         260b




   yellow 正如前面所说的副本不可用,有5个分片和1个副本(默认值),它包含0文件。

5. 添加索引文档

1
2
# curl -XPUT '127.0.0.1:9200/customer/external/1?retty' -d '{"name":"Little Boy"}'
{"_index":"customer","_type":"external","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}





6. 查询文档

1
2
3
4
5
6
7
8
9
10
11
# curl -XGET '127.0.0.1:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
    "name" : "Little Boy"
}
}





6.删除索引

1
2
3
4
5
6
# curl -XDELETE '127.0.0.1:9200/customer?pretty'
{
"acknowledged" : true
}
# curl '127.0.0.1:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size





  以上就是Elasticsearch在集群方面常用的API,借助这些API,我们可以很快的查询服务器的状态,发现服务器的运行问题。

页: [1]
查看完整版本: 探索Elasticsearch集群API