一、插入/更新数据
1、单条插入/更新:指定_id
如果_id存在,就更新
如果_id不存在,就插入
1)POST 方法
如果_doc后面指定了_id,就用指定的_id,否则系统默认分配_id
POST /test_001/_doc/1
{
"id": "1",
"goodsName": "test",
"shopId": 1,
"putSaleTime": "2020-09-08 14:29:06",
"isDelete": false,
"couponIds": "1,2"
}
2)PUT 方法
注意
:
1、PUT方法,必须指定_id,否则就会报错
2、PUT方法,修改时,其他属性值丢失,不是建议使用这种方式
PUT test_001/_doc/10
{
"id": "10",
"goodsName": "PUT指定_id=10",
"shopId": 1,
"putSaleTime": "2020-09-08 14:29:06",
"isDelete": false,
"couponIds": "1,2"
}
2、单条插入/更新:默认_id
POST方法,_doc后面没有指定_id,系统默认分配_id
POST /test_001/_doc
{
"id": "1",
"goodsName": "默认_id",
"shopId": 1,
"putSaleTime": "2020-09-08 14:29:06",
"isDelete": false,
"couponIds": "1,2"
}
3、批量插入/更新
如果_id存在,就更新
如果_id不存在,就插入
POST _bulk
{"index":{"_index":"test_001","_id":2}}
{"id":1,"goodsName":"test2","shopId":110,"putSaleTime":"2020-09-09 14:29:06","isDelete":false,"couponIds":"3"}
{"index":{"_index":"test_001","_id":3}}
{"id":3,"goodsName":"test3","shopId":220,"putSaleTime":"2020-02-22 12:29:06","isDelete":false,"couponIds":"1,3"}
{"index":{"_index":"test_001","_id":4}}
{"id":4,"goodsName":"test4","shopId":330,"putSaleTime":"2020-11-01 14:29:06","isDelete":false,"couponIds":"4"}
二、删除数据
1、删除指定_id
DELETE /test_001/_doc/1
{
"id": "1",
"goodsName": "默认_id",
"shopId": 1,
"putSaleTime": "2020-09-08 14:29:06",
"isDelete": false,
"couponIds": "1,2"
}
2、删除指定条件数据
POST test_001/_delete_by_query?pretty
{
"query": {
"term": {
"goodsName": "test2"
}
}
}
3、删除所有数据
POST test_001/_delete_by_query?pretty
{
"query": {
"match_all": {
}
}
}