问题描述
当前
{
"bar": {
"bar": [{
"bar": [{
"bar": [{
"foo": "Y111111111111"
}
]
}
]
}
} }
需要更新
{
"bar": {
"bar": [{
"bar": [{
"bar": [{
"foo": "111111111111"
}
]
}
]
}
} }
我该如何更新索引以将字符串的第一个字符剥去等于bar?
我尝试了适用于单个字段的以下语法,但是在数组字段上运行时收到异常
{
"error": {
"root_cause": [
{
"type": "script_exception","reason": "runtime error","script_stack": [
"ctx._source.foo = ctx._source.foo.substring(1);"," ^---- HERE"
],"script": "ctx._source.foo = ctx._source.foo.substring(1);","lang": "painless"
}
],"type": "script_exception","script_stack": [
"ctx._source.foo = ctx._source.foo.substring(1);"," ^---- HERE"
],"lang": "painless","caused_by": {
"type": "illegal_argument_exception","reason": "Illegal list shortcut value [foo]."
}
},"status": 400
}
POST test/_update_by_query
{
"query": {
"prefix": {
"foo": "Y"
}
},"script": {
"source": "ctx._source.foo = ctx._source.foo.substring(1);"
}
映射
{
"TEST": {
"mappings": {
"properties": {
"bar": {
"properties": {
"bar": {
"properties": {
"bar: {
"properties": {
"bar": {
"properties": {
"foo": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
}
}
}
}
解决方法
我将按照以下步骤进行操作。查找所有foo
字段以Y
开头的文档,然后通过去除第一个字符来更新所有foo
字段:
POST test/_update_by_query
{
"query": {
"prefix": {
"bar.bar.bar.bar.foo": "Y"
}
},"script": {
"source": "ctx._source.bar.bar[0].bar[0].bar[0].foo = ctx._source.bar.bar[0].bar[0].bar[0].foo.substring(1);"
}
}
PS:查询将取决于您的bar
字段是否嵌套,但如果不嵌套,则上述查询应该起作用。