欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ES更新数据

程序员文章站 2024-03-25 12:59:40
...

最近项目中用到了ES,有这么一个场景:索引中有个nested结构,要更新nested中的值,如果document没有,需要创建document的,并且要批量,这里的批量指两个:
1,一次更新(upinsert)多个document
2、一次更新nested中的多个值
这是公司一个ES的专家给我写的,特此记录一下:

POST ptp_tags/item/_bulk
{ "update" : { "_id" : "70000004"}, "retry_on_conflict":3 }
{ "script" : { "inline": "ctx._source.object_list.removeIf((item) -> params.objects.stream().anyMatch((object)->object.object_id.equals(item.object_id)) );if (params.deleted != true) {ctx._source.object_list.addAll(params.objects);}", "lang" : "painless", "params" : {"deleted":false, "objects" :  [{"object_id":"p70007","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2005.0},{"object_id":"p70008","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2006.0}] } }, "upsert":{"product_type":0,"object_list":[{"object_id":"p70007","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2005.0},{"object_id":"p70008","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2006.0}]}}

如果有多行,可以继续添加。。。。

mapping 结构如下:

"mappings" : {
      "item" : {
        "dynamic" : "false",
        "include_in_all" : false,
        "properties" : {
          "_source_" : {
            "type" : "keyword"
          },
          "id" : {
            "type" : "long"
          },
          "object_list" : {
            "type" : "nested",
            "properties" : {
              "expire_time" : {
                "type" : "date",
                "format" : "yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm:ss'.0'||yyyy-MM-dd||epoch_millis"
              },
              "object_id" : {
                "type" : "keyword"
              },
              "object_type" : {
                "type" : "keyword"
              },
              "object_value" : {
                "type" : "keyword"
              },
              "object_value_num" : {
                "type" : "double"
              }
            }
          },
          "product_type" : {
            "type" : "keyword"
          }
        }
      }
    }