如何使用无痛脚本执行弹性搜索_update_by_query - 用于复杂条件

时间:2018-05-27 11:12:21

标签: elasticsearch elasticsearch-painless

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

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

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

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

1 个答案:

答案 0 :(得分:0)

创建映射:

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

插入第一个doc:

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
                }
            }
        ]
    }
}