这篇文章旨在告诉大家怎么在windows系统下安装ES,怎么在windows系统下使用它的REST API操作ES。
(一)window下ES的安装
在装es之前确保机器已经装了java,最好是较新版本的,我机器上装的是10。
C:\>java -version
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)
es官网下载地址https://www.elastic.co/downloads/elasticsearch
找到对应的版本下载之后解压一下,找到elasticsearch.bat双击启动一下
待执行成功后查看以下信息
到此,安装启动就结束了。
用Ctrl+c可以将其关闭。
现在我们已经有一个正常运行的节点(和集群),下一步就是要去理解怎样与其通信。幸运的是,Elasticsearch提供了非常全面和强大的REST API,利用这个REST API你可以同你的集群交互。在Windows上我们用git bash来执行相关的命令,不是在cmd下(没装的同学可以装一下)。
(一)操作集群
下面是利用这个API,可以做的几件事情:
- 查你的集群、节点和索引的健康状态和各种统计信息
-
//查看集群状态 $ curl 'localhost:9200/_cat/health?v' //响应数据 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- 100 296 100 296 0 0 9548 0 --:--:-- --:--:-- --:--:-- 9548epoch timestamp cluster status node.tot al node.data shards pri relo init unassign pending_tasks max_tas k_wait_time active_shards_percent 1539669345 13:55:45 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0% ————————————————————————————————————————————————————————————————————————————————————————— //获得节集群中的节点列表 $ curl 'localhost:9200/_cat/nodes?v' //响应数据 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 175 100 175 0 0 5645 0 --:--:-- --:--:-- --:--:-- 5645ip percent ram.percent cpu load_1m load_5m load_15m node.role master name 127.0.0.1 38 67 2 mdi * QxN ————————————————————————————————————————————————————————————————————————————————————————— //列出所有的索引 $ curl 'localhost:9200/_cat/indices?v' //响应数据(因为我们还未建索引,所以在这索引是空的) % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 83 100 83 0 0 5533 0 --:--:-- --:--:-- --:--:-- 5533health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
- 管理你的集群、节点、索引数据和元数据
- 对你的索引进行 CRUD(创建、读取、更新和删除)和搜索操作
-
//创建一个索引叫myindices的索引,pretty附加到调用的尾部,使其以美观的形式打印出JSON响应 $ curl -XPUT 'localhost:9200/myindices?pretty' //响应数据 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 85 100 85 0 0 38 0 0:00:02 0:00:02 --:--:-- 38{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "myindices" } ————————————————————————————————————————————————————————————————————————————————————————— //索引一个文档错误示例 $ curl -XPUT 'localhost:9200/myindices/sutdent/1?pretty' -d '{"name":"yuhua"}' //当执行完上面的命令后你会发现报了如下的错误,是因为相应参数的格式不对 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 125 100 109 100 16 7266 1066 --:--:-- --:--:-- --:--:-- 8333{ "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported", "status" : 406 } //索引一个文档的正确示例,我试过http://好像加不加都可以 $ curl -H "Content-Type:application/json" -XPUT 'http://localhost:9200/myindices/student/1?pretty' -d '{"name":"yuhua"}' //响应数据 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 242 100 226 100 16 965 68 --:--:-- --:--:-- --:--:-- 1034{ "_index" : "myindices", "_type" : "student", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 } ————————————————————————————————————————————————————————————————————————————————————————— //提取索引的文档 $ curl -XGET 'http://localhost:9200/myindices/student/1?pretty' //响应数据 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 145 100 145 0 0 9062 0 --:--:-- --:--:-- --:--:-- 9062{ "_index" : "myindices", "_type" : "student", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name" : "yuhua" } } ————————————————————————————————————————————————————————————————————————————————————————— //删除索引 $ curl -XDELETE 'http://localhost:9200/myindices?pretty' //响应数据 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0curl: (6) Could not resolve host: E 100 28 100 28 0 0 68 0 --:--:-- --:--:-- --:--:-- 68{ "acknowledged" : true } ————————————————————————————————————————————————————————————————————————————————————————— curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID> 这个REST访问模式普遍适用于所有的API命令
- 执行高级的查询操作, 像是分页、排序、过滤、脚本编写(scripting)、小平面刻画(faceting)、聚合(aggregations)和许多其它操作
还是那句嚼碎了的老话,想用好一个工具就要养成查询它的文档及api的习惯,百种千种的方法靠我们自己脑袋记住是十分困难的,当然也不排除有这种超凡的同学。下面有两份不错的材料,大家可以备用。
ES权威指南
https://es.xiaoleilu.com/010_Intro/25_Tutorial_Indexing.html
ES API文档
https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/java-api/index-api.html