Elasticsearch无痛脚本不会使用if条件替换嵌套对象字段值 [英] Elasticsearch painless script not replacing the nested object field value using if condition

查看:990
本文介绍了Elasticsearch无痛脚本不会使用if条件替换嵌套对象字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用ES,但仍然掌握交易技巧!!! 我需要替换/覆盖嵌套对象类型的字段之一. 这是示例文档:

I just started with ES and still leaning the tricks of the trade!!! I need to replace/overwrite one of the fields of a nested object type. Here is the sample doc:

{
"name":"db_ref",
"ref_counter":[
    {"ref_name":"test1","count":1},
    {"ref_name":"test2","count":2},
    {"ref_name":"test3","count":3}
    ]
}

映射上述文档:

{
    "settings": {
        "mappings": {
            "test_doc": {
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "ref_count": {
                        "type": "nested",
                        "ref_name": "string",
                        "count": "long"
                    }
                }
            }
        }
    }
}

我需要为给定的ref_name更新计数字段值.例如,在上述情况下,如果 ref_name 是" test1 ",我希望新的计数 500 . 我想出了以下无痛脚本来更改 count 值,它可以正常执行,没有任何错误,但是我看不到该值正在更新.

I need to update the count field value for a given ref_name. Eg, in the above case, if ref_name is "test1" I want the new count to be 500. I came up with the below painless script for changing the count value, it executes fine without any error but I dont see the value being updated.

curl -XPOST "http://localhost:9200/test_type/test_type/test_db/_update" -d '
{"script": "if (ctx._source.ref_counter.ref_name == cur_ref 
                && ctx._source.ref_counter.count == cur_count)
            {ctx._source.ref_counter.count = new_count };",
"params": {"cur_count": 1,"new_count": 500, "cur_ref": "test1"}}}'

以下是响应:

{"_index":"test_index","_type":"test_type","_id":"test_db","_version":2}

但是当我看到文档时,它仍然具有旧值.

But when I see the document it still has the old values.

有人可以帮助我将计数值更改为新值吗.

Can someone please help me in changing the count value to a new value.

推荐答案

我提到了以下查询. (Bulk update查询和per document更新查询)

I have mentioned the below queries. (Bulk update query and per document update query)

核心逻辑在script中,这在两个查询中都是相同的.

Core piece of logic is in script, which is same in both queries.

我建议您按照逻辑进行自我解释.基本上,脚本会遍历嵌套文档,并根据您指定的条件来相应地更新count.

I'd suggest you to go through the logic as it's self explainable. Basically the script iterates through the nested document and depending on the condition you've stated, it would update the count accordingly.

POST <your_index_name>/_update_by_query
{
  "query": {
    "match_all": {}
  },
  "script": {
    "lang": "painless",
    "source": """
    if(ctx._source.ref_counter.contains(params.cur_data)){

      for(int i=0; i<ctx._source.ref_counter.size(); i++)
      {
        HashMap myKV = ctx._source.ref_counter.get(i);
        if(myKV.get(params.key_ref)==params.key_ref_value){
          myKV.put(params.key_count, params.key_count_value_new);
        }
      }

      //ctx._source.ref_counter.add(params.new_data); 

    }""",
    "params": {
      "cur_data": { "ref_name": "test2", "count": 2},
      "new_data": { "ref_name": "test2", "count": 22},
      "key_ref": "ref_name",
      "key_ref_value": "test2",
      "key_count": "count",
      "key_count_value_new": 80

  }}
}

每个文档更新-使用文档ID

POST <your_index_name>/<your_mapping>/1/_update
{
  "script": {
    "lang": "painless",
    "source": """
    if(ctx._source.ref_counter.contains(params.cur_data)){

      for(int i=0; i<ctx._source.ref_counter.size(); i++)
      {
        HashMap myKV = ctx._source.ref_counter.get(i);
        if(myKV.get(params.key_ref)==params.key_ref_value){
          myKV.put(params.key_count, params.key_count_value_new);
        }
      }

      //ctx._source.ref_counter.add(params.new_data); 

    }""",
    "params": {
      "cur_data": { "ref_name": "test2", "count": 2},
      "new_data": { "ref_name": "test2", "count": 22},
      "key_ref": "ref_name",
      "key_ref_value": "test2",
      "key_count": "count",
      "key_count_value_new": 80

  }
}
}

我希望这会有所帮助,如果您有任何疑问,请告诉我!

I hope this helps and let me know if you have any queries!

这篇关于Elasticsearch无痛脚本不会使用if条件替换嵌套对象字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆