Elasticsearch数组Array类型增加、删除

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 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/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"
    }
  }
}

二、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...';}}"
  }
}

参考

Elasticsearch局部更新(数组追加)

Elasticsearch 5.6.3 通过script添加、删除数组元素

相关实践学习
利用Elasticsearch实现地理位置查询
本实验将分别介绍如何使用Elasticsearch7.10版本进行全文检索、多语言检索和地理位置查询三个Elasticsearch基础检索子场景的实现。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
2月前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
31 3
|
2月前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
38 3
|
2月前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
2月前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。
|
2月前
|
Ruby
|
10天前
|
存储 安全 算法
C++的内置数组和STL array、STL vector
C++的内置数组和STL array、STL vector
|
2月前
|
JavaScript 前端开发 索引
在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
【4月更文挑战第16天】在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
31 4
|
8月前
|
算法 Python
数组倍增(Array Doubling
数组倍增(Array Doubling)是一种常见的算法技术,用于解决数组相关的查找、插入、删除等问题。该技术的核心思想是将数组的大小乘以 2,新数组的长度是原数组长度的两倍,然后将原数组中的元素复制到新数组中。在某些情况下,这种技术可以提高算法的效率,尤其是对于动态数据结构的问题。
161 1
|
2月前
|
存储 索引 Python
多数pythoneer只知有列表list却不知道python也有array数组
多数pythoneer只知有列表list却不知道python也有array数组
35 0