参考: 脚本如何使用 更新文档的操作

删除

delete 单条删除

  1. DELETE indexName/_doc/1
  2. #查看索引统计信息
  3. POST indexName/_refresh
  4. POST indexName/_flush
  5. 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:写入数据等待分片数响应
  • pipline:数据ETL处理

    _delete_by_query条件删除

  • 根据条件筛选数据并删除数据

  • 删除是异步操作

    1. POST indexName/_delete_by_query
    2. {
    3. "query":{
    4. xxx
    5. }
    6. }

    URL参数

  • scroll_size:删除时采用快照查询,控制查询数量,默认1000

  • requests_per_second:每秒处理删除数据条数,默认-1,无限制,海量日志建议控制在500-1000
  • conflicts:删除冲突标记,默认是中断,可选值proceed,自动处理
  • slices:控制分片并行度,并行度过高容易造成IO异常,默认1

更新

单条更新默认是覆盖的形式

  1. DELETE ckiss-company-001
  2. PUT ckiss-company-001
  3. {
  4. "settings": {
  5. "refresh_interval": "1s",
  6. "number_of_shards": 1
  7. },
  8. "mappings": {
  9. "properties": {
  10. "companyId": {
  11. "type": "integer"
  12. },
  13. "userId": {
  14. "type": "integer"
  15. },
  16. "userName": {
  17. "type": "keyword"
  18. },
  19. "ragDate": {
  20. "type": "date"
  21. }
  22. }
  23. }
  24. }
  25. PUT ckiss-company-001/_doc/1
  26. {
  27. "companyId":2
  28. }
  29. PUT ckiss-company-001/_doc/1
  30. {
  31. "companyId":2,
  32. "userId": 123
  33. }
  34. PUT ckiss-company-001/_doc/1
  35. {
  36. "companyId":2,
  37. "userId": 123,
  38. "userName": "测试数据"
  39. }
  40. PUT ckiss-company-001/_doc/1
  41. {
  42. "regDate": "2022-04-10"
  43. }
  44. 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

  1. <a name="upQut"></a>
  2. ### `script`更新方式
  3. - lang:脚本语言
  4. - painless:脚本语言
  5. - upsert: 脚本插入式更新
  6. ```json
  7. POST <INDEX>/_update/<_id>
  8. {
  9. "script": {
  10. "source": "ctx_source.tags.add(params.tag)",
  11. "lang": "painless",
  12. "params": {
  13. "tag": "blue"
  14. }
  15. }
  16. }
  1. DELETE ckiss-company-001
  2. PUT ckiss-company-001
  3. {
  4. "settings": {
  5. "refresh_interval": "1s",
  6. "number_of_shards": 1
  7. },
  8. "mappings": {
  9. "properties": {
  10. "companyId": {
  11. "type": "integer"
  12. },
  13. "userId": {
  14. "type": "integer"
  15. },
  16. "userName": {
  17. "type": "keyword"
  18. },
  19. "ragDate": {
  20. "type": "date"
  21. },
  22. "companyName":{
  23. "type": "text"
  24. }
  25. }
  26. }
  27. }
  28. PUT ckiss-company-001/_doc/1
  29. {
  30. "companyId":2,
  31. "userId": 123,
  32. "userName": "测试数据",
  33. "regDate": "2022-04-10",
  34. "companyName": "ckiss国际集团"
  35. }
  36. # 脚本1
  37. POST ckiss-company-001/_update/1
  38. {
  39. "script": {
  40. "source": "ctx._source.companyId=15",
  41. "params": {},
  42. "lang": "painless"
  43. }
  44. }
  45. # 脚本2 - 带参数
  46. POST ckiss-company-001/_update/1
  47. {
  48. "script": {
  49. "source": "ctx._source.companyId=params.companyId",
  50. "params": {
  51. "companyId": 21
  52. },
  53. "lang": "painless"
  54. }
  55. }
  56. # 脚本3 - 不存在的字段可以更新
  57. POST ckiss-company-001/_update/1
  58. {
  59. "script": {
  60. "source": "ctx._source.birDate='2022-04-10'",
  61. "params": {},
  62. "lang": "painless"
  63. }
  64. }

scripted_upsert

  1. POST ckiss-company-001/_update/2
  2. {
  3. "script": {
  4. "source": "ctx._source.income += params.get('arg');",
  5. "params": {
  6. "arg": 1000
  7. },
  8. "lang": "painless"
  9. },
  10. "upsert": {
  11. "income": "1000"
  12. },
  13. "scripted_upsert": true
  14. }

案例:删除集合中某个元素,类似java的List,仅仅删除一个

  1. PUT my-index-000001/_doc/1
  2. {
  3. "counter" : 1,
  4. "tags" : ["red", "red", "green", "blue", "red"]
  5. }
  6. POST my-index-000001/_update/1
  7. {
  8. "script": {
  9. "source": "if (ctx._source.tags.contains(params['tag'])) { ctx._source.tags.remove(ctx._source.tags.indexOf(params['tag'])) }",
  10. "lang": "painless",
  11. "params": {
  12. "tag": "red"
  13. }
  14. }
  15. }
  16. GET my-index-000001/_doc/1

响应:

  1. "_source" : {
  2. "counter" : 1,
  3. "tags" : [
  4. "red",
  5. "green",
  6. "blue",
  7. "red"
  8. ]
  9. }

注意:它需要删除的元素的索引。为避免可能的运行时错误,首先需要确保标签存在。如果列表包含重复的标记,则此脚本仅删除一次出现。

删除包含某个标签的索引文档,如果包含则删除,不包含则不做操作

  1. POST my-index-000001/_update/1
  2. {
  3. "script": {
  4. "source": "if (ctx._source.tags.contains(params['tag'])) { ctx.op = 'delete' } else { ctx.op = 'none' }",
  5. "lang": "painless",
  6. "params": {
  7. "tag": "green"
  8. }
  9. }
  10. }

脚本删除/新增单个字段

  1. POST my-index-000001/_update/1
  2. {
  3. "script": "ctx._source.remove('new_field')"
  4. }
  1. POST my-index-000001/_update/1
  2. {
  3. "script": "ctx._source.new_field = 'value_of_new_field'"
  4. }

_update_by_query条件批量更新

  1. POST <indexName>/_update_by_query
  2. {
  3. "query": {
  4. ...
  5. },
  6. "script":{
  7. ...
  8. }
  9. }
  1. POST my-index-000001/_update_by_query
  2. {
  3. "script": {
  4. "source": "ctx._source.count++",
  5. "lang": "painless"
  6. },
  7. "query": {
  8. "term": {
  9. "user.id": "kimchy"
  10. }
  11. }
  12. }
  13. POST my-index-000001/_update_by_query
  14. {
  15. "script": {
  16. "source": "if(ctx._source.containsKey('tag')){ctx._source.tag=ctx._source.tag+1;}else{ctx._source.tag=1;}",
  17. "lang": "painless"
  18. },
  19. "query": {
  20. "term": {
  21. "user.id": "kimchy"
  22. }
  23. }
  24. }

update_by_query官方文档