ES中REST API详解

ES REST API

elasticsearch支持多种通讯,其中包括http请求响应服务,因此通过curl命令,可以发送http请求,并得到json返回内容。

常用的REST请求有:

检查集群状态

1
curl localhost:9200/_cat/health?v
APACHE

检查节点状态

1
curl localhost:9200/_cat/nodes?v
APACHE

查询全部索引

1
curl localhost:9200/_cat/indices?v
APACHE

注:集群状态分为green yellow red三种状态,green 表示健康,yellow表示数据完整但是副本存在问题,red表示数据不完整。
创建索引

1
curl -XPUT localhost:9200/索引名/类型/id -d {//JSON格式的body体}
AWK

删除索引

1
curl -XDELETE localhost:9200/索引名
APACHE

查询索引

1
curl -XGET localhost:9200/索引名/类型/id
APACHE

 

ES 使用bulk 添加数据

使用bulk命令,添加json文件中的数据。
新建json文件accounts.json,定义json数据格式,其中每个数据格式都是如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "index":{"_id":"1"}
   "account_number": 0,
   "balance": 16623,
   "firstname": "Bradshaw",
   "lastname": "Mckenzie",
   "age": 29,
   "gender": "F",
   "address": "244 Columbus Place",
   "employer": "Euron",
   "email": "bradshawmckenzie@euron.com",
   "city": "Hobucken",
   "state": "CO"
  }
PERL

执行命令,批量添加:

1
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
NGINX

查询索引

1
curl 'localhost:9200/_cat/indices?v'
1C

表示我们已经成功批量导入1000条数据索引到bank索引中。


ES中REST API详解
https://leehoward.cn/2019/10/16/ES中REST API详解/
作者
lihao
发布于
2019年10月16日
许可协议