微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Elasticsearch中更新和剥离数组字段的第一个字符

如何解决如何在Elasticsearch中更新和剥离数组字段的第一个字符

我在弹性搜索的索引中包含以下内容,列为json文档

当前

{
"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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?