Elasticsearch 无痛脚本不使用 if 条件替换嵌套对象字段值

2023-12-12

我刚刚开始使用 ES,仍在学习该行业的技巧! 我需要替换/覆盖嵌套对象类型的字段之一。 这是示例文档:

{
"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 is "test1“我想要新的count to be 500。 我想出了下面的无痛脚本用于改变count值,它执行得很好,没有任何错误,但我没有看到该值正在更新。

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}

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

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


我已经提到了以下问题。 (Bulk update查询和per document更新查询)

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

我建议您仔细检查逻辑,因为它是可以自我解释的。基本上,脚本会迭代嵌套文档,并根据您所说的条件,它会更新count因此。

批量更新 - 使用按查询更新

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

  }}
}

每个文档更新 - 使用文档编号

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

  }
}
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Elasticsearch 无痛脚本不使用 if 条件替换嵌套对象字段值 的相关文章

随机推荐