ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] 现在,我们已经启动并运行了自己的节点(集群),下一步是要了解如何沟通。幸运的是,Elasticsearch提供了一个非常全面和强大的REST API,让我们可以与集群进行交互,例如: - 检查我们的集群、节点和索引健康状态和统计数据 - 管理集群、节点和索引数据和元数据 - 执行CRUD(创建、读取、更新和删除)索引和搜索操作 - 执行高级搜索操作,比如分页、排序、过滤、脚本、聚合等 >[success] API 匹配模式:\<HTTP Verb> /\<Index>/\<Type>/\<ID> &nbsp; ## **集群管理** ### **查看集群健康状态: `GET /_cat/health` 或 `GET /_cluster/health`** 查看集群健康状态命令: ``` curl -X GET "localhost:9200/_cat/health?v" ``` 运行结果如下: ```cmd epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1543028474 03:01:14 docker-cluster green 1 1 0 0 0 0 0 0 - 100.0% ``` ``` curl -X GET "localhost:9200/_cluster/health?pretty" ``` 运行结果如下: ```cmd { "cluster_name" : "docker-cluster", "status" : "green", "timed_out" : false, "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 0, "active_shards" : 0, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "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" : 100.0 } ``` 每当我们问集群健康,我们要么得到green,yellow和red。 - Green - everything is good (cluster is fully functional) - Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional) - Red - some data is not available for whatever reason (cluster is partially functional) ### **查看集群中的节点: `GET /_cat/nodes`** 查看集群中的节点命令: ``` curl -X GET "localhost:9200/_cat/nodes?v" ``` 运行结果如下: ```cmd ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 172.17.0.2 10 95 13 0.16 0.24 0.49 mdi * jNMR62Z ``` 在这里,我们可以看到一个名为“jNMR62Z”的节点.目前在我们的集群是单一节点。 ### **列出当前所有的索引: `GET /_cat/indices`** 列出当前所有的索引命令: ``` curl -X GET "localhost:9200/_cat/indices?v" ``` 运行结果如下: ```cmd health status index uuid pri rep docs.count docs.deleted store.size pri.store.size ``` 目前,我们还没有创建任何一个索引 ### **创建索引: `PUT /<index>`** 创建一个索引“customer”命令: ``` curl -X PUT "localhost:9200/customer?pretty" ``` 我们使用PUT方法,创建了一个名字为“customer”的索引,上面的参数`pretty`,是格式化输出JSON的作用 运行结果如下: ```cmd { "acknowledged" : true, "shards_acknowledged" : true, "index" : "customer" } ``` 列出当前所有索引命令: ``` curl -X GET "localhost:9200/_cat/indices?v" ``` 运行结果如下: ```cmd health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open customer H4FuIykjRIeAbPij8yLRug 5 1 0 0 1.1kb 1.1kb ``` ### **删除索引: `DELETE /<index>`** 删除上面练习中添加的索引customer,命令: ``` curl -X DELETE "localhost:9200/customer?pretty" ``` 运行结果如下: ```cmd { "acknowledged" : true } ``` 由上面结果可知,已成功删除索引“teacher”