Update

ES本身还是一个倾向于查询检索的框架,对于这种更新的操作,太过频繁是不好的。

将旧文档标记为删除状态,然后添加新的文档,旧的文档不会立刻消失,但是也不可访问

本篇文章包含使用Script对所有的文档执行更新操作,也可以使用doc对部分文档执行更新,也可以使用upsert对不存在的文档执行添加操作。

全部更新 PUT

  1. curl -XPUT localhost:9200/test/type1/1 -d '{
  2. "counter" : 1,
  3. "tags" : ["red"]
  4. }'

部分更新 POST

  1. POST index/_doc/id/_update
  2. {
  3. "doc" : {
  4. "callBackStatus" : 1,
  5. "callBackCno":"1045",
  6. "callBackCname":"zhaojy"
  7. }
  8. }

脚本更新

可以通过ctx来获得_source、``_index_type_id_version_parent_timestamp_ttl等字段信息。

脚本教程

https://blog.csdn.net/u013613428/article/details/78134170

1. 计数增加

  1. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  2. "script" : {
  3. "inline": "ctx._source.counter += params.count",
  4. "params" : {
  5. "count" : 4
  6. }
  7. }
  8. }'

2. 增加标签

  1. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  2. "script" : {
  3. "inline": "ctx._source.tags += params.tag",
  4. "params" : {
  5. "tag" : "blue"
  6. }
  7. }
  8. }'

3. 存在则更新

  1. POST test/_doc//_update
  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. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  2. "script" : "ctx._source.name_of_new_field = \"value_of_new_field\""
  3. }'

移除字段:

  1. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  2. "script" : "ctx._source.remove(\"name_of_field\")"
  3. }'

upsert

存在就更新,不存在插入

  1. curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  2. "script" : {
  3. "inline": "ctx._source.counter += count",
  4. "params" : {
  5. "count" : 4
  6. }
  7. },
  8. "upsert" : {
  9. "counter" : 1
  10. }
  11. }'

在上面的例子中,当文档存在时,执行脚本;当文档不存在时,upsert中的内容就会插入到对应的文档中。
如果你想无论文档是否存在都执行脚本操作,那么可以使用参数scripted_upsert为true。
注意:ctx._source.counter字段不能是空,否则报空指针异常

  1. curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{
  2. "scripted_upsert":true,
  3. "script" : {
  4. "id": "my_web_session_summariser",
  5. "params" : {
  6. "pageViewEvent" : {
  7. "url":"foo.com/bar",
  8. "response":404,
  9. "time":"2014-01-01 12:32"
  10. }
  11. }
  12. },
  13. "upsert" : {}
  14. }'

update_by_query

  1. POST /user_bigtable/_doc/_update_by_query
  2. {
  3. "script": {
  4. "source": "ctx._source['saleaccount']='lixiaona10029'"
  5. },
  6. "query": {
  7. "bool": {
  8. "must_not": [
  9. {
  10. "exists": {
  11. "field": "area"
  12. }
  13. }
  14. ],
  15. "must": [
  16. {
  17. "term":{
  18. "classid":217007
  19. }
  20. },
  21. {
  22. "term":{
  23. "periodid":90001
  24. }
  25. }
  26. ]
  27. }
  28. }
  29. }

查询更新
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html

Delete

将旧文档标记为删除状态,然后添加新的文档,旧的文档不会立刻消失,但是也不可访问
curl -XDELETE master:9200/bigdata/product/4—->根据指定的索引id进行删除

  1. POST index_*/_delete_by_query
  2. {
  3. "query": {
  4. "match": {
  5. "userId": XXX
  6. }
  7. }
  8. }

更多文章:

  1. https://www.cnblogs.com/xing901022/p/5330778.html
  2. https://my.oschina.net/u/1027043/blog/1648749?from=mail-notify
  3. https://blog.csdn.net/weixin_39800144/article/details/80524373
  4. 常用 api: https://www.bbsmax.com/A/E35pnaWbzv/

参考文章: