删除
delete 单条删除
DELETE indexName/_doc/1
#查看索引统计信息
POST indexName/_refresh
POST indexName/_flush
GET indexName/_status
URL参数
- if_seq_no:并发顺序号
- if_primary_term :并发更新主分片顺序号
- version: 并发版本号
- version_type:并发版本类型
- op_type:数据写入类型,新增还是 新增或更新
- refresh:是否立即刷新,默认false,取值 true/false/wait_for
- routing:路由
- wait_for_active_shards:写入数据等待分片数响应
-
_delete_by_query
条件删除 根据条件筛选数据并删除数据
删除是异步操作
POST indexName/_delete_by_query
{
"query":{
xxx
}
}
URL参数
scroll_size:删除时采用快照查询,控制查询数量,默认1000
- requests_per_second:每秒处理删除数据条数,默认-1,无限制,海量日志建议控制在500-1000
- conflicts:删除冲突标记,默认是中断,可选值proceed,自动处理
- slices:控制分片并行度,并行度过高容易造成IO异常,默认1
更新
单条更新默认是覆盖的形式
DELETE ckiss-company-001
PUT ckiss-company-001
{
"settings": {
"refresh_interval": "1s",
"number_of_shards": 1
},
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"userId": {
"type": "integer"
},
"userName": {
"type": "keyword"
},
"ragDate": {
"type": "date"
}
}
}
}
PUT ckiss-company-001/_doc/1
{
"companyId":2
}
PUT ckiss-company-001/_doc/1
{
"companyId":2,
"userId": 123
}
PUT ckiss-company-001/_doc/1
{
"companyId":2,
"userId": 123,
"userName": "测试数据"
}
PUT ckiss-company-001/_doc/1
{
"regDate": "2022-04-10"
}
GET ckiss-company-001/_doc/1
partial
局部更新
- 局部更新实际是局部覆盖式更新,只是数据的局部合并由ES完成
- 依然会产生并发冲突 ```json PUT ckiss-company-001/_doc/1 { “companyId”:2, “userId”: 123, “userName”: “测试数据”, “regDate”: “2022-04-10” }
局部更新,doc_as_upsert没有就插入
POST ckiss-company-001/_update/2 { “doc”: { “userName”: “测试Data” }, “doc_as_upsert”: true }
GET ckiss-company-001/_doc/1
<a name="upQut"></a>
### `script`更新方式
- lang:脚本语言
- painless:脚本语言
- upsert: 脚本插入式更新
```json
POST <INDEX>/_update/<_id>
{
"script": {
"source": "ctx_source.tags.add(params.tag)",
"lang": "painless",
"params": {
"tag": "blue"
}
}
}
DELETE ckiss-company-001
PUT ckiss-company-001
{
"settings": {
"refresh_interval": "1s",
"number_of_shards": 1
},
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"userId": {
"type": "integer"
},
"userName": {
"type": "keyword"
},
"ragDate": {
"type": "date"
},
"companyName":{
"type": "text"
}
}
}
}
PUT ckiss-company-001/_doc/1
{
"companyId":2,
"userId": 123,
"userName": "测试数据",
"regDate": "2022-04-10",
"companyName": "ckiss国际集团"
}
# 脚本1
POST ckiss-company-001/_update/1
{
"script": {
"source": "ctx._source.companyId=15",
"params": {},
"lang": "painless"
}
}
# 脚本2 - 带参数
POST ckiss-company-001/_update/1
{
"script": {
"source": "ctx._source.companyId=params.companyId",
"params": {
"companyId": 21
},
"lang": "painless"
}
}
# 脚本3 - 不存在的字段可以更新
POST ckiss-company-001/_update/1
{
"script": {
"source": "ctx._source.birDate='2022-04-10'",
"params": {},
"lang": "painless"
}
}
scripted_upsert
POST ckiss-company-001/_update/2
{
"script": {
"source": "ctx._source.income += params.get('arg');",
"params": {
"arg": 1000
},
"lang": "painless"
},
"upsert": {
"income": "1000"
},
"scripted_upsert": true
}
案例:删除集合中某个元素,类似java的List,仅仅删除一个
PUT my-index-000001/_doc/1
{
"counter" : 1,
"tags" : ["red", "red", "green", "blue", "red"]
}
POST my-index-000001/_update/1
{
"script": {
"source": "if (ctx._source.tags.contains(params['tag'])) { ctx._source.tags.remove(ctx._source.tags.indexOf(params['tag'])) }",
"lang": "painless",
"params": {
"tag": "red"
}
}
}
GET my-index-000001/_doc/1
响应:
"_source" : {
"counter" : 1,
"tags" : [
"red",
"green",
"blue",
"red"
]
}
注意:它需要删除的元素的索引。为避免可能的运行时错误,首先需要确保标签存在。如果列表包含重复的标记,则此脚本仅删除一次出现。
删除包含某个标签的索引文档,如果包含则删除,不包含则不做操作
POST my-index-000001/_update/1
{
"script": {
"source": "if (ctx._source.tags.contains(params['tag'])) { ctx.op = 'delete' } else { ctx.op = 'none' }",
"lang": "painless",
"params": {
"tag": "green"
}
}
}
脚本删除/新增单个字段
POST my-index-000001/_update/1
{
"script": "ctx._source.remove('new_field')"
}
POST my-index-000001/_update/1
{
"script": "ctx._source.new_field = 'value_of_new_field'"
}
_update_by_query
条件批量更新
POST <indexName>/_update_by_query
{
"query": {
...
},
"script":{
...
}
}
POST my-index-000001/_update_by_query
{
"script": {
"source": "ctx._source.count++",
"lang": "painless"
},
"query": {
"term": {
"user.id": "kimchy"
}
}
}
POST my-index-000001/_update_by_query
{
"script": {
"source": "if(ctx._source.containsKey('tag')){ctx._source.tag=ctx._source.tag+1;}else{ctx._source.tag=1;}",
"lang": "painless"
},
"query": {
"term": {
"user.id": "kimchy"
}
}
}