1. 创建测试索引
PUT test
{
"mappings": {
"test1": {
"properties": {
"num1": {
"type": "integer"
},
"num2": {
"type": "integer"
},
"num3": {
"type": "nested",
"properties": {
"num31": {
"type": "integer"
},
"num32": {
"type": "integer"
}
}
}
}
}
}
}
2. 添加测试数据
PUT test/test1/1
{
"num1": 11,
"num2": 12,
"num3": [
{
"num31": 131,
"num32": 132
},
{
"num31": 133,
"num32": 134
}
]
}
PUT test/test1/2
{
"num1": 21,
"num2": 12,
"num3": [
{
"num31": 231,
"num32": 232
},
{
"num31": 233,
"num32": 234
}
]
}
3. 查看数据
GET test/test1/_search
4. 根据id更新数据
POST test/test1/1/_update
{
"doc": {
"num1": 10
}
}
5. 使用 update_by_query 更新数据
POST test/test1/_update_by_query
{
"script": {
"lang": "painless",
"source": "ctx._source.num1=111;ctx._source.num2=222"
},
"query": {
"bool": {
"must": [
{
"term": {
"num2": {
"value": "12"
}
}
}
]
}
}
}
6. 使用 update_by_query 为 nested 类型追加单条数据
POST test/test1/_update_by_query
{
"script": {
"lang": "painless",
"source": "ctx._source.num3.add(params.new_data)",
"params": {
"new_data": {
"num31":1111,
"num32":2222
}
}
},
"query": {
"bool": {
"must": [
{
"term": {
"num2": {
"value": "22"
}
}
}
]
}
}
}
7. 使用 update_by_query 为 nested 类型追加多条数据
POST test/test1/_update_by_query
{
"script": {
"lang": "painless",
"source": "ctx._source.num3.addAll(params.new_datas)",
"params": {
"new_datas": [
{
"num31":1111,
"num32":2222
},
{
"num31":3333,
"num32":4444
},
{
"num31":5555,
"num32":6666
}
]
}
},
"query": {
"bool": {
"must": [
{
"term": {
"num2": {
"value": "12"
}
}
}
]
}
}
}