elasticsearch - 如何在elasticsearch中更新和剥离数组字段的第一个字符
问题描述
我在弹性搜索的索引中有以下内容,列为 json doc
当前的
{
"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",
"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",
"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
字段是否嵌套,但如果不是,上述查询应该可以工作。
推荐阅读
- python - 关于 CS50 Pset6 DNA,它超过了 large.cvs 的 STR
- python - 打开 binance websocket 并显示不使用特定库的价格变化
- javascript - 有没有办法在地形图上完美对齐geo json文件?React、D3 和三个 js
- ruby-on-rails - 如何在 ruby on rails 的不同位置显示上传的图像?
- java - 应用程序因 'jvm 终止而崩溃。退出代码=1'
- c# - 肥皂服务抛出“响应提前结束,至少
需要额外的字节。”(IOException) - xamarin - 如何在 Xamarin 中创建无限 ListView?
- python - 使用多个字符串动态填充字典
- javascript - React - 从 json 动态渲染组件
- python - Python代码为跨账户的变量创建潜在客户值,账户的最后一个值为空