【问题标题】:How to perform elastic search _update_by_query using painless script - for complex condition如何使用无痛脚本执行弹性搜索 _update_by_query - 用于复杂条件
【发布时间】:2018-05-27 11:12:21
【问题描述】:

您能否建议如何根据条件字段更新文档(使用脚本 - 我猜是无痛的)?

其目的是在文档中添加/或删除值

所以如果我有这些输入文件:

doc //1st
{
   "Tags":["foo"],
   "flag":"true"
}


doc //2nd
{
   "flag":"true"
}

doc //3rd
{
   "Tags": ["goo"],
   "flag":"false"
}

我想执行这样的操作:

更新所有具有“flag=true”的文档:

  • 添加了标签:“我”、“一个”
  • 已删除标签:“goo”、“foo”

所以预期的结果应该是这样的:

doc //1st
{
   "Tags":["me","one"],
   "flag":"true"
}


doc //2nd
{
   "Tags":["me","one"],
   "flag":"true"
}

doc //3rd
{
   "Tags": ["goo"],
   "flag":"false"
}

【问题讨论】:

    标签: elasticsearch elasticsearch-painless


    【解决方案1】:

    创建映射:

    PUT documents
    {
        "mappings": {
            "document": {
                "properties": {
                    "tags": {
                        "type": "keyword",
                        "index": "not_analyzed"
                    },
                    "flag": {
                        "type": "boolean"
                    }
                }
            }
        }
    }
    

    插入第一个文档:

    PUT documents/document/1
    {
        "tags":["foo"],
        "flag": true
    }
    

    插入第二个文档(请记住,对于空标签,我指定了空标签数组,因为如果您根本没有字段,则需要检查脚本是否存在字段):

    PUT documents/document/2
    {
        "tags": [],
        "flag": true
    }
    

    添加第三个文档:

    PUT documents/document/3
    {
        "tags": ["goo"],
        "flag": false
    }
    

    然后运行 ​​_update_by_query,它有两个数组作为参数,一个用于添加元素,一个用于删除元素:

    POST documents/_update_by_query 
    {
        "script": {
            "inline": "for(int i = 0; i < params.add_tags.size(); i++) { if(!ctx._source.tags.contains(params.add_tags[i].value)) { ctx._source.tags.add(params.add_tags[i].value)}} for(int i = 0; i < params.remove_tags.size(); i++) { if(ctx._source.tags.contains(params.remove_tags[i].value)){ctx._source.tags.removeAll(Collections.singleton(params.remove_tags[i].value))}}",
            "params": {
                "add_tags": [
                    {"value": "me"},
                    {"value": "one"}
                ],
                "remove_tags": [
                    {"value": "goo"},
                    {"value": "foo"}
                ]
            }
        },
        "query": {
            "bool": {
                "must": [
                    {"term": {"flag": true}}
                ]
            }
        }
    }
    

    如果您随后进行以下搜索:

    GET documents/_search
    

    你会得到以下结果(我认为这是你想要的):

    {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 3,
            "max_score": 1,
            "hits": [{
                    "_index": "documents",
                    "_type": "document",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "flag": true,
                        "tags": [
                            "me",
                            "one"
                        ]
                    }
                },
                {
                    "_index": "documents",
                    "_type": "document",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "flag": true,
                        "tags": [
                            "me",
                            "one"
                        ]
                    }
                },
                {
                    "_index": "documents",
                    "_type": "document",
                    "_id": "3",
                    "_score": 1,
                    "_source": {
                        "tags": [
                            "goo"
                        ],
                        "flag": false
                    }
                }
            ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-19
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 2021-12-02
      • 1970-01-01
      相关资源
      最近更新 更多