基于现有文档值

时间:2018-04-05 01:08:26

标签: elasticsearch lucene kibana

我从elastic.co文档中了解到,使用_search API上的脚本轻松搜索文档,例如,我可以使用现有 doc值制作剧本条件。

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;"
}

我为什么需要?实际上我的文档上有两个字段。一个名为images的多个值,例如 [“1.jpg”,“2.jpg”,“3. png”] ,另一个名为image_count,其中 3 基于images字段的长度。在这里,我可以在使用images API时获得_search字段的长度。

  1. 但我只是想知道ES上有什么设施可以 通过动态计算images来更新每个文档 长度并设置image_count字段的值?
    1. 是否有任何批量更新API可以轻松完成该任务 在_update API上指定每个文档ID?

1 个答案:

答案 0 :(得分:1)

您正在寻找的是_update_by_query API。如果要更新大量文档,请注意,可以使用切片滚动方法。 这是最新的,可能需要调整它取决于您的版本

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