基本语法
1.cat
GET /_cat/nodes:查看所有节点
GET /_cat/health:查看es健康状况
GET /_cat/master:查看主节点
GET /_cat/indices:查看所有索引
2.索引一个文档
PUT mytest/basic/1 //保存在mytest索引下的basic类型id为1号的数据
{
“name":"test"
}
返回信息
{
"_index": "mytest", //索引名称
"_type": "basic", //类型
"_id": "1", //id
"_version": 1, //版本
"result": "created", //操作
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
POST不带id为新增操作,带了id则查看存在,则为修改,其他为新增。
put则必须带id,不然无法发送
3.查看文档
GET mytest/basic/1 //查看在mytest索引下的basic类型id为1号的数据
返回结果
{
"_index": "mytest", //索引
"_type": "basic", //类型
"_id": "1", //记录id
"_version": 1, //版本号
"_seq_no": 0, //并发字段,更新+1。,可以做乐观锁
"_primary_term": 1, //主分片
"found": true,
"_source": { //内容
"name": "test"
}
}
4.更新文档
POST mytest/basic/1/_update //带来_update则会对比原来的数据,不相同版本才+,相同则不改变。且需要编写doc
{
"doc":{
"name":"mytest"
}
}
POST mytest/basic/1/ //不带update,则直接修改,版本+
{
"name":"mytest"
}
put和不带_update的post都会直接更新数据
5.删除文档
DELETE mytest/basic/1 //删除mytest索引下的basic类型的1号数据
DELETE mytest删除一个索引
6. bulk批量API
POST /mytest/basic/_bulk
{"index":{"_id":"1"}}
{"name":"test"}
{"index":{"_id":"2"}}
{ "name":"test2"}
返回结果
{
"took" : 12, //花费时间
"errors" : false, //没有错误
"items" : [0
{
"index" : { //第一个数据
"_index" : "mytest",
"_type" : "basic",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1,
"status" : 200
}
},
{
"index" : { //第二个数据
"_index" : "mytest",
"_type" : "basic",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1,
"status" : 201
}
}
]
}
POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }} //删除website索引下blog类型下的id为123的记录,后面的以之前的类推即可
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123"}}
{ "doc" : {"title" : "My updated blog post"} }
批量数据测试:https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true
POST bank/account/_bulk //后面接受批量处理的json