一、初步检索
1、_cat
GET /_cat/nodes : 查看所有结点
GET /_cat/health : 查看es的健康状况
GET /_cat/master : 查看主节点
GET /_cat/indices :查看所有索引
2、索引一个文档
保存一个数据,保存在哪个索引的哪个类型下,指定用哪个唯一标识
PUT customer/external/1 : 在customer索引下的 external 类型下保存1号数据
PUT customer/external/1
{
"name":"houchen"
}
返回数据:
{
"_index" : "customer", // 带_的为元数据 索引
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
【总结】
PUT 和 POST都可以进行新增操作
PUT必须要带id,id存在的话即修改,id不存在的话则是新增
带id的情况下,多次PUT/POST是修改
不带id的情况下,多次POST是新增
3、查询文档
GET /customer/external/1
结果:
{
"_index" : "customer", //在哪个索引
"_type" : "external", //在哪个类型
"_id" : "1", //记录id
"_version" : 1, //版本号
"_seq_no" : 0, //并发控制字段,每次更新就会+1,用来做乐观锁
"_primary_term" : 1, //同上,主分片重新分配,会重启就会变化
"found" : true, //是否找到
"_source" : {
"name" : "houchen"
}
}
并发修改时,需要携带 _seq_no
put /customer/external/1?if_seq_no=1&if_primary_term=1
4、更新文档
带 _update的更新语句:会对比原来的数据,如果没有变化,则版本号不会更新
POST customer/external/1/_update
{
"doc":{
"name":"john Doew"
}
}
不带_update的更新语句,会不断更新,叠加版本_version
POST customer/external/1
{
"name":"john Doew"
}
PUT customer/external/1
{
"name":"john Doew"
}
5、删除文档和索引
删除文档
DELETE customer/external/1
删除索引
DELETE customer
6、bulk批量API
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"111"}
{"index":{"_id":"2"}}
{"name":"222"}
复杂操作:
POST /_bulk
{"delete":{"_index":"wehsite","_type":"blog","_id":"123"}} //删除文档
{"create":{"_index":"wehsite","_type":"blog","_id":"123"}} //创建文档
{"title":"My first blog post"}
{"index":{"_index":"wehsite","_type":"blog"}} //索引文档
{"title":"My second blog post"}
{"update":{"_index":"wehsite","_type":"blog","_id":"123"}}
{"doc":{"title":"My second blog post"}}
【导入官网测试数据】
post /bank/account/_bulk
https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip
–restart=always : 开机启动,失败也会一直重启;
–restart=on-failure:10 : 表示最多重启10次