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

标签 elasticsearch elasticsearch-scripting

我在以json doc形式列出的 flex 搜索索引中包含以下内容
当前

{
"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字段是否嵌套,但如果不嵌套,则上面的查询应该起作用。

关于elasticsearch - 如何在Elasticsearch中更新和剥离数组字段的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64456334/

相关文章:

elasticsearch - Kibana:将脚本字段设置为时间字段名称

elasticsearch - 如何强制对Elasticsearch进行 float 浇铸?

elasticsearch - 在 Elasticsearch 中的一个查询中搜索父级及其所有子级

elasticsearch - Elasticsearch轻松更新唯一元素列表

elasticsearch - 如何在嵌套 script_score 的过滤器之间添加?

elasticsearch - 包含动态数据/nested_objects的聚合

elasticsearch - 如何在 Elasticsearch 查询中使用更新API来更新数据集中的数据

elasticsearch - 当至少一个对象不包含必要字段时,按对象数组选择文档 Elasticsearch

node.js - 结合使用AWS Lambda和Elastic Search,从搜索客户端获取未定义