ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 一、概述 所有其他语言可以使用 RESTful API 通过端口*9200*和 Elasticsearch 进行通信,你可以用你最喜爱的 web 客户端访问 Elasticsearch; Java语言还可以通过JAVA API的方式与Elasticsearch 进行通信; 这里介绍RESTful API 方式; ## 二、RESTful API 命令举例 ### **index操作** **查看当前节点的所有 Index** ``` curl --user elastic:'ray!@#333' GET 'http://localhost:9200/_cat/indices?v' ``` ![](https://img.kancloud.cn/26/ad/26ad04abe574d45c65d316040d9e7089_1410x98.png) **新建和删除 Index** 新建一个名叫rayes的 Index ``` curl --user elastic:'ray!@#333' PUT 'localhost:9200/rayes' ``` ![](https://img.kancloud.cn/87/98/8798a074e05cfd76590f2e54c9f35f94_1401x86.png) 再检查一遍所有的index,发现已经新增成功了; ![](https://img.kancloud.cn/4f/c0/4fc019449ea7e7efa827a35d2a448115_1416x87.png) 删除名叫rayes的 Index ``` curl --user elastic:'ray!@#333' DELETE 'localhost:9200/rayes' ``` ### **document操作** >[danger] 规则 > 从7.X版本开始,typs已经被废弃了,索引一个文档的命令,不再需要文档类型了,而是用`PUT {index}/_doc/{id}`或者`POST {index}/_doc`(自动产生ids); **新增文档** ``` curl --user elastic:'ray!@#333' -H "Content-Type: application/json" -X POST 'localhost:9200/rayes/_doc' -d '{"userName":"A","birthDate":"2008/10/18","desc":"数据库管理"}' ``` ![](https://img.kancloud.cn/8f/f4/8ff4c42d9bdf560e337b7b08119f1a68_1920x942.png) **查看文档** ``` curl --user elastic:'ray!@#333' 'localhost:9200/rayes/1/hg6-snwBMTAE7MZApHxA?pretty=true' ``` ![](https://img.kancloud.cn/6e/76/6e76091eaf4a891f8da66526434658ab_1411x327.png) **搜索文档** ``` curl --user elastic:'ray!@#333' -H "Content-Type: application/json" -X POST localhost:9200/_search?pretty -d '{"query":{"query_string": {"query": "rayes"}}} ' ``` ![](https://img.kancloud.cn/20/d1/20d12edcbd28a9d4f2ff0d42cf9b6e9b_1441x912.png) ``` curl --user elastic:'ray!@#333' -H "Content-Type: application/json" -X POST localhost:9200/_search?pretty -d '{"query":{"query_string": {"query": "ray"}}} ' ``` ![](https://img.kancloud.cn/46/29/4629606661395365b679078083e7ff2c_1424x674.png)