基于现有文档值的elasticsearch脚本更新
elasticsearch script update based on existing doc value
从 elastic.co 文档中我了解到使用 _search
API 上的脚本很容易搜索文档,例如在这里我可以使用 现有 文档值,同时制作脚本条件。
GET /_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"source": "doc['num1'].value > 1",
"lang": "painless"
}
}
}
}
}
}
要使用 Update API
更新文档,我们可以这样做,
POST test/_doc/1/_update
{
"script" : "ctx._source.new_field = 'value_of_new_field'"
}
我期望的 ES 查询可能吗? (我尝试过但没有成功)
POST test/_doc/1/_update
{
"script" : "ctx._source.image_count = doc['images'].length;"
}
为什么我需要?实际上,我的文档中有 2 个字段。一个名为 images
,具有多个值,例如 ["1.jpg","2.jpg","3.png"],另一个名为 image_count
和 3 基于 images
字段的长度。在这里,我可以在使用 _search
API 时获取 images
字段的长度。
- 但我只想知道在 ES 上是否有任何我可以使用的工具
通过动态计算
images
来更新每个文档
长度并设置 image_count
字段的值?
或
- 是否有任何批量更新 API 可以轻松完成该任务而无需
在
_update
API? 上指定每个文档 ID
您要找的是_update_by_queryAPI。如果您要更新大量文档,请小心,您可以使用切片滚动方法。
这在最新版本中有效,可能需要根据您的版本进行调整
POST /images_index/_update_by_query
{
"script":{
"source": "ctx._source['images_count'] = ctx._source['images'].length"
}
}
在此处查看文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html
从 elastic.co 文档中我了解到使用 _search
API 上的脚本很容易搜索文档,例如在这里我可以使用 现有 文档值,同时制作脚本条件。
GET /_search
{
"query": {
"bool" : {
"must" : {
"script" : {
"script" : {
"source": "doc['num1'].value > 1",
"lang": "painless"
}
}
}
}
}
}
要使用 Update API
更新文档,我们可以这样做,
POST test/_doc/1/_update
{
"script" : "ctx._source.new_field = 'value_of_new_field'"
}
我期望的 ES 查询可能吗? (我尝试过但没有成功)
POST test/_doc/1/_update
{
"script" : "ctx._source.image_count = doc['images'].length;"
}
为什么我需要?实际上,我的文档中有 2 个字段。一个名为 images
,具有多个值,例如 ["1.jpg","2.jpg","3.png"],另一个名为 image_count
和 3 基于 images
字段的长度。在这里,我可以在使用 _search
API 时获取 images
字段的长度。
- 但我只想知道在 ES 上是否有任何我可以使用的工具
通过动态计算
images
来更新每个文档 长度并设置image_count
字段的值?
或
- 是否有任何批量更新 API 可以轻松完成该任务而无需
在
_update
API? 上指定每个文档 ID
您要找的是_update_by_queryAPI。如果您要更新大量文档,请小心,您可以使用切片滚动方法。 这在最新版本中有效,可能需要根据您的版本进行调整
POST /images_index/_update_by_query
{
"script":{
"source": "ctx._source['images_count'] = ctx._source['images'].length"
}
}
在此处查看文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html