//CURD 对应 post put get delete
//索引指数据库 type即表 文档指表中一条记录 但ES7后废除了type即表的概念
//创建索引
PUT /student
//查看索引 结构
GET /student
//删除索引
DELETE /student1
===========插入数据===================
//PUT插入数据 当2号数据已存在是 _version+1 索引不存在时自动创建
PUT student/_doc/4
{
"user": "laf",
"uid": 1,
"city": "weihai",
"province": "shgnda",
"country": "China"
}
//POST 插入数据 类似
POST student/_doc/5
{
"user": "dkdk",
"uid": 1,
"city": "GuiZhou",
"province": "Guangdong",
"country": "China"
}
//如果数据存在 不允许更改 可使用一下两种方法
PUT student/_create/1
{
"user": "GB",
"uid": 1,
"city": "Shenzhen",
"province": "Guangdong",
"country": "China"
}
PUT student/_create/1?op_type=create
{
"user": "GB",
"uid": 1,
"city": "Shenzhen",
"province": "Guangdong",
"country": "China"
}
//批量插入数据 使用index
POST _bulk
{ "index" : { "_index" : "twitter", "_id": 1} }
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{ "index" : { "_index" : "twitter", "_id": 2 }}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{ "index" : { "_index" : "twitter", "_id": 3} }
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{ "index" : { "_index" : "twitter", "_id": 4} }
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{ "index" : { "_index" : "twitter", "_id": 5} }
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{ "index" : { "_index" : "twitter", "_id": 6} }
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}
//或者 使用create 区别在于 当数据重复时 前者覆盖 后者失败
POST _bulk
{ "create" : { "_index" : "twitter", "_id": 1} }
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{ "index" : { "_index" : "twitter", "_id": 2 }}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{ "index" : { "_index" : "twitter", "_id": 3} }
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{ "index" : { "_index" : "twitter", "_id": 4} }
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{ "index" : { "_index" : "twitter", "_id": 5} }
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{ "index" : { "_index" : "twitter", "_id": 6} }
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}
==============删除============================
//删除索引
DELETE student
//刪除文档
DELETE /student/_doc/1
//不知道ID 的前提下 根据条件删除文档 _delete_by_query
POST student/_delete_by_query
{
"query": {
"match": {
"user": "lsg"
}
}
}
============查询数据===================
//查询所有文档
GET student/_search
POST student/_search
//查询文档总数
GET student/_count
//查看单个文档 两种方式
GET /student/_source/4
GET /student/_doc/1
//检查文档是否存在
HEAD /student/_source/1
//根据索引获取多个文档的数据
GET _mget
{
"docs": [
{
"_index": "bank",
"_id": 1
},
{
"_index": "bank",
"_id": 2
}
]
}
//简写
GET student/_mget
{
"ids": ["3", "2"]
}
//根据id获取多个文档的部分属性
GET _mget
{
"docs": [
{
"_index": "bank",
"_id": 1,
"_source":["age", "city"]
},
{
"_index": "student",
"_id": 2,
"_source":["user", "city"]
}
]
}
=============修改文档PUT=================
PUT student/_doc/1
{
"user": "kkkk",
"uid": 1,
"city": "GuiZhou",
"province": "Guangdong",
"country": "China"
}
//查看
GET /student/_doc/1
//修改文档某一项其他项不变 使用POST 与_update
POST /student/_update/1
{
"doc":{
"user":"lsg"
}
}
//当不知道ID的时候 修改 使用POST与_update_by_query
POST student/_update_by_query
{
"query": {
"match": {
"user": "lsg"
}
},
"script": {
"source": "ctx._source.city = params.city;ctx._source.province = params.province;ctx._source.country = params.country",
"lang": "painless",
"params": {
"city": "广西",
"province": "贵港",
"country": "中国"
}
}
}
//若要更新的文档 不存在的话 会报错
//使用 doc_as_upsert 避免 表示 存在则更新否则插入
POST /student/_update/1
{
"doc": {
"user": "lsg",
"city": "广西壮族自治区"
},
"doc_as_upsert": true
}