ElasticSearch 的安装 (7.16.1 版本)Kibana DevTool 的是使用创建索引,删除索引数据写入,更新,删除数据修改等操作。
😄博主:程序员一灯,一个老菜鸟,哈哈哈哈
🚌一个人可以走的很快,一群人可以走的很远🇨🇳
- 🎉点赞➕评论➕收藏 ➕关注== 养成习惯(一键四连)📝
- 🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝
- 🙏作者水平有限,欢迎各位大佬指点,相互学习进步!😆
🏮一、基本CRUD
🥃1.1、新增文档
PUT ${index_name}
PUT schools
🍸1)新增文档,并指定id字段(replace)
该模式下,若指定id对应的文档存在,则删除源文档后并写入新文档,并且version版本加1。
PUT ${index_name}/_doc/1
{
...
}
//多次执行后,version一直递增
POST schools/_doc/1
{"name":"Central School","description":"CBSE Affiliation","street":"Nagan","city":"paprola","state":"HP","zip":"176115","location":[31.8955385,76.8380405],"fees":2000,"tags":["Senior Secondary","beautiful campus"],"rating":"3.5"}
🍸2)新增文档,并指定ID字段(insert)
该模式下,若指定id对应的文档存在,则直接返回报错
PUT ${index_name}/_create/1
{
...
}
或者
PUT ${index_name}/_create/1?op_type=create
{
...
}
PUT bluepig/_create/1
{
"name":"八戒1号猪",
"date":"2021年12月23日11:04:39"
}
PUT bluepig/_create/2?op_type=create
{
"name":"八戒2号猪",
"date":"2021年12月23日11:06:02"
}
执行查询
GET bluepig/_search
返回
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bluepig",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "八戒1号猪",
"date" : "2021年12月23日11:04:39"
}
},
{
"_index" : "bluepig",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "八戒2号猪",
"date" : "2021年12月23日11:06:02"
}
}
]
}
}
🍸3)新增文档,不指定ID字段,系统自动生产ID
POST ${index_name}/_doc
{
...
}
POST zhangsan/_doc
{
"name":"张三",
"age":"25"
}
get zhangsan/_search
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "zhangsan",
"_type" : "_doc",
"_id" : "8XpD5X0BStHCKgTnOEeI",
"_score" : 1.0,
"_source" : {
"name" : "张三",
"age" : "25"
}
}
]
}
}
🥃1.2、查询文档
🍸1)GET查询指定ID文档
GET /${index_name}/_doc/3
执行查询
GET /bluepig/_doc/2
返回
{
"_index" : "bluepig",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "八戒2号猪",
"date" : "2021年12月23日11:06:02"
}
}
🍸2)search api查询满足条件的文档
查询索引下全部文档
GET /${index_name}/_search
查询满足指定条件全部文档
GET /#{index_name}/_search
{
"query": {
"match": {
...
}
}
}
GET /bluepig/_search
{
"query": {
"match": {
"name": "八戒"
}
}
}
🍸3) 查看索引的基本配置信息
GET /${index_name}
GET schools
🍸4) 查看索引的基本状态信息
GET _cat/indices/${index_name}
GET _cat/indices/schools
🥃1.3、更新文档
🍸1)更新指定ID文档,存在则更新,不存在则新增(也是新增文档的一种)
PUT ${index_name}/_doc/1
{
...
}
PUT schools2/_doc/11
{"name":"Central School","description":"CBSE Affiliation","street":"Nagan","city":"paprola","state":"HP","zip":"176115","location":[31.8955385,76.8380405],"fees":2000,"tags":["Senior Secondary","beautiful campus"],"rating":"3.5"}
🍸2)更新指定ID文档,更新文档必须存在,否则报错
//限制操作类型是更新,那么如果不存在则更新不了,报错
POST /${index_name}/_update/1
{
"doc": {
"name":"aa",
"age":12
}
}
🥃1.4、删除文档
🍸1)删除指定ID文档
DELETE /${index_name}/_doc/1
🍸2)删除满足条件查询文档
POST /${index_name}/_delete_by_query
{
"query":{
"match":{
...
}
}
}
POST /schools/_delete_by_query
{
"query":{
"match":{
"_id":2
}
}
}
🏮二、bulk api 批量
支持在一次api中对不同的索引进行操作
支持create index update delete
//1、创建索引users name=aa
//2、删除索引id=2
//3、创建索引users id=2
//4、更新索引 新增了个字段
POST _bulk
{"index":{"_index":"users","_id":"1"}}
{"name":"aa"}
{"delete":{"_index":"users","_id":"2"}}
{"create":{"_index":"users","_id":"2"}}
{"name":"bb"}
{"update":{"_id":"1","_index":"users"}}
{"doc":{"auto_inc":"11"}}
#批量操作
#1.删除5号
#新增8号
#更新2号 name为2号
POST _bulk
{"delete":{"_index":"person1","_id":"5"}}
{"create":{"_index":"person1","_id":"8"}}
{"name":"八号","age":18,"address":"北京"}
{"update":{"_index":"person1","_id":"2"}}
{"doc":{"name":"2号"}}
POST schools_gov/_bulk
{"index":{"_index":"schools_gov","_id":"1"}}
{"name":"Model School","description":"CBSE Affiliation","street":"silk city","city":"Hyderabad","state":"AP","zip":"500030","location":[17.3903703,78.4752129],"fees":200,"tags":["Senior Secondary","beautiful campus"],"rating":"3"}
{"index":{"_index":"schools_gov","_id":"2"}}
{"name":"Government School","description":"State Board Affiliation","street":"Hinjewadi","city":"Pune","state":"MH","zip":"411057","location":[18.599752,73.6821995],"fees":500,"tags":["Great Sports"],"rating":"4"}
🏮三、CRUD的练习例子
GET movies/_search
查询movies的数据
GET movies/_count
查询movies的总数
GET _cat/indices
查看所有的索引
GET movies/_doc/24
查询id为24的数据
POST users/_doc/1
POST users/_doc/1
{
"firstname": "will",
"lastname": "smith"
}
#添加id为1的文档 ,如果没有指定id,ES会自动生成
POST users/_create/2
{
"firstname": "will",
"lastname": "smith"
}
#创建id为2的文档,如果索引中已存在相同id,会报错;
POST users/_update/2
{
"doc": {
"age": 30
}
}
在id位2的文档中添加一个age属性,修改结构
DELETE users/_doc/2
删除id为2的文档
DELETE users
删除 users 索引
PUT users/_doc/1
{
"firstname": "Jack",
"lastname": "ma"
}
创建或者修改文档
PUT users/_create/2
{
"firstname": "will",
"lastname": "smith"
}
批量查询多个指定的id的数据,也可以批量查询
POST users/_bulk
{"index":{"_id":3}}
{"firstname":"a","lastname":"A"}
{"index":{"_id":2}}
{"firstname":"a","lastname":"A"}
{"index":{"_id":3}}
{"firstname":"a","lastname":"A"}
{"index":{"_id":4}}
{"firstname":"a","lastname":"A"}
{"index":{"_id":5}}
{"firstname":"a","lastname":"A"}
批量插入数据
版权归原作者 程序员一灯 所有, 如有侵权,请联系我们删除。