Elasticsearch数组Array类型增加、删除
        【摘要】     一、Array里边的元素是String 
# 创建一条数据
POST test_index/test_type/1
{
  "tags":["tag1", "tag2", "tag3"]  
}
# 查看数据
GET test_index/test_type/1
# 给 _id=1 的tags增加一个 tag5
POST test_index/test_type/...
    
    
    
    一、Array里边的元素是String
# 创建一条数据
POST test_index/test_type/1
{
  "tags":["tag1", "tag2", "tag3"]  
}
# 查看数据
GET test_index/test_type/1
# 给 _id=1 的tags增加一个 tag5
POST test_index/test_type/1/_update
{ "script" : { "source": "ctx._source.tags.add(params.tag)", "params" : { "tag" : "tag5" } }
}
# _id=1 的tags移除 tag5
POST test_index/test_type/1/_update
{ "script" : { "source": "ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag))", "params" : { "tag" : "tag5" } }
}
# 查询 tag1 in tags 移除tag2
POST test_index/test_type/_update_by_query
{
  "query": { "term": { "tags": "tag1" }
  },
  "script": { "source": "ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag))", "params": { "tag": "tag2" }
  }
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 
二、Array里边包含对象
# 添加一条数据
POST /blog-test/doc/AXiHn9l3CFuw25Pb9kYM
{
  "title": "Invest Money",
  "body": "Please start investing money as soon...",
  "tags": [ "money", "invest"
  ],
  "published_on": "18 Oct 2017",
  "comments": [ { "name": "William", "age": 34, "rating": 8, "comment": "Nice article..", "commented_on": "30 Nov 2017" }, { "name": "John", "age": 38, "rating": 9, "comment": "I started investing after reading this.", "commented_on": "25 Nov 2017" }, { "name": "Smith", "age": 33, "rating": 7, "comment": "Very good post", "commented_on": "20 Nov 2017" }
  ]
}
# 数组添加一条数据
POST /blog-test/doc/AXiHn9l3CFuw25Pb9kYM/_update
{
  "script": { "source": "ctx._source.comments.add(params.new_comment)", "params": { "new_comment": { "name": "xiang", "age": 25, "rating": 18, "comment": "very very good article...", "commented_on": "3 Nov 2018" } }
  }
} # 数组移除一条数据
POST /blog-test/doc/AXiHn9l3CFuw25Pb9kYM/_update
{
  "script": { "lang": "painless", "source": "ctx._source.comments.removeIf(it -> it.name == 'John');"
  }
}
# 数组更新一条数据
POST /blog-test/doc/AXiHn9l3CFuw25Pb9kYM/_update
{
  "script": { "source": "for(e in ctx._source.comments){if (e.name == 'Smith') {e.age = 25; e.comment= 'very very good article...';}}"
  }
}
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 
参考
Elasticsearch局部更新(数组追加)
Elasticsearch 5.6.3 通过script添加、删除数组元素
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/115131727
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)