索引相关操作
创建索引:7版本以上:一个索引中默认只包含一个名为_doc的类型
#默认创建包含一个名为_doc类型的索引
PUT /users
#(一般创建索引时指定mapping,因为包含默认的_doc类型)
PUT /users
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text"
},
"age": {
"type": "short"
},
"address": {
"type": "text"
},
"date": {
"type": "date"
}
}
}
}
查看所有索引
GET /_cat/indices?v
删除索引
#删除指定索引
DELETE /users
#删除所有索引(慎用会删除es系统索引)
DELETE /*
查看索引
#查看指定
GET /users
#查看所有索引
GET /*
类型操作
创建类型:其实就是在创建索引时就默认会创建一个名为_doc的类型
PUT /users
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text"
},
"age": {
"type": "short"
},
"address": {
"type": "text"
},
"date": {
"type": "date"
}
}
}
}
查看类型mapping
GET /users/_mapping
插入数据时指定_id
#插入数据指定_id
PUT /users/_doc/2
{
"id": 1,
"name": "zhangsan",
"age": 20,
"address": "四川省成都市",
"birth": "2021-01-10"
}
自动生成_id
POST/users/_doc
{
"id": 1,
"name": "zhangsan",
"age": 20,
"address": "四川省成都市",
"birth": "2021-01-10"
}
查询数据
#指定_id
GET /users/_doc/1
#查询所有
GET /users/_doc/_search
删除数据
DELETE /users/_doc/2
更新数据
#更新数据,这种会删除原有所有字段
POST /users/_doc/1
{
"name": "lisi"
}
#更新,可以添加新字段(会自动创建mapping)
POST /users/_doc/1/_update
{
"doc":{
"name": "lisi",
"sex": "male"
}
}
批量操作
PUT /users/_bulk
{"index":{"_id":"3"}}#创建指定_id
{"id":1,"name":"zhangsan","age":20,"address":"四川省成都市","birth":"2021-01-10"}
{"index":{}}#不指定_id
{"id":1,"name":"zhangsan","age":20,"address":"四川省成都市","birth":"2021-01-10"}
{"delete":{"_id":"2"}}
{"update":{"_id":"3"}}
{"doc":{"address": "四川省"}}