如何解决如何在Elasticsearch中更新和剥离数组字段的第一个字符
当前
{
"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
字段是否嵌套,但如果不嵌套,则上述查询应该起作用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。