更新 Elasticsearch 嵌套索引值
Update Elastic search nested index value
我在弹性搜索中有一个具有嵌套值的索引,我想用 Id 更新嵌套索引中的一个值,我该如何实现。我的索引就像
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "a",
"_type": "a1",
"_id": "my-index1",
"_score": 1.0,
"_source": {
"id": "my-index1",
"name": "firstIndex",
"metals": [
{
"id": "123",
"name": "Ronni",
},
{
"id": "124",
"name": "Ross",
}
]
}
}
]
}
}
如何在 "Id"-124 的帮助下将 "Ross" 更新为 "Monika"?
假设您的 metals
属于 nested
datatype (and not being just a trivially nested array), you can first constrict the docs-to-update using a nested term query and then use a looped script 以执行您的更改:
POST my-index1/_update_by_query
{
"query": {
"nested": {
"path": "metals",
"query": {
"term": {
"metals.id": "124"
}
}
}
},
"script": {
"source": """
for (def i = 0; i < ctx._source.metals.length; i++) {
def group = ctx._source.metals[i];
if (group['id'] == params['old_metal_id']) {
ctx._source.metals[i]['name'] = params['new_metal_name']
}
}
""",
"params": {
"old_metal_id": "124",
"new_metal_name": "Monika"
}
}
}
我在弹性搜索中有一个具有嵌套值的索引,我想用 Id 更新嵌套索引中的一个值,我该如何实现。我的索引就像
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "a",
"_type": "a1",
"_id": "my-index1",
"_score": 1.0,
"_source": {
"id": "my-index1",
"name": "firstIndex",
"metals": [
{
"id": "123",
"name": "Ronni",
},
{
"id": "124",
"name": "Ross",
}
]
}
}
]
}
}
如何在 "Id"-124 的帮助下将 "Ross" 更新为 "Monika"?
假设您的 metals
属于 nested
datatype (and not being just a trivially nested array), you can first constrict the docs-to-update using a nested term query and then use a looped script 以执行您的更改:
POST my-index1/_update_by_query
{
"query": {
"nested": {
"path": "metals",
"query": {
"term": {
"metals.id": "124"
}
}
}
},
"script": {
"source": """
for (def i = 0; i < ctx._source.metals.length; i++) {
def group = ctx._source.metals[i];
if (group['id'] == params['old_metal_id']) {
ctx._source.metals[i]['name'] = params['new_metal_name']
}
}
""",
"params": {
"old_metal_id": "124",
"new_metal_name": "Monika"
}
}
}