elasticsearch-updateRequest的应用

#step1:插入数据,全部更新之前的
curl -X PUT 'localhost:9200/test/type1/1' -H 'Content-Type: application/json'  -d '{
    "name":"zhangsan",
    "age":10,
    "sex":"女",
    "phone":"13456789",
    "adress":""
}'
#step2:doc操作,不一样的地方,局部更改
curl -X POST 'localhost:9200/test/type1/1/_update' -H 'Content-Type: application/json'  -d '{
  "doc":{
    "name":"",
    "age":10,
    "sex":"女",
    "phone":"13456789",
    "adress":""
  }   
}'

#使用doc_as_upsert可以在文档不存在的时候,把doc中的内容插入到文档中。(如下2不存在时,作插入操作)

#不加的时候:{"error":{"root_cause":[{"type":"document_missing_exception","reason":"[type1][2]: document missing",报missing_exception
curl -X POST 'localhost:9200/test/type1/2/_update' -H 'Content-Type: application/json'  -d '{
   "doc":{
     "type":"student"
   }
}'

curl -X POST 'localhost:9200/test/type1/2/_update' -H 'Content-Type: application/json'  -d '{
   "doc":{
     "type":"student"
   },
    "doc_as_upsert" : true   
}'
#合并前检查:如果使用doc,那么会自动合并到现有的文档中。如果doc中定义的部分与现在的文档相同,则默认不会执行任何动作。设置detect_noop=false,就会无视是否修改,强制合并到现有的文档。
curl -X POST 'localhost:9200/test/type1/1/_update' -H 'Content-Type: application/json'  -d '{
   "doc":{
     "aa":111
   },
   "detect_noop": true   
}'

#step3:script操作,局部更新,加减、特定删除(字段、或索引)等,支持groovy script脚本,功能更加强大
curl -X POST 'localhost:9200/test/type1/6/_update' -H 'Content-Type: application/json'  -d '{
"script" : {
    "source":"if (ctx._source.adress==null) ctx._source.adress=params.adress;if (ctx._source.name==null) ctx._source.name=params.name;",
    "params" : {
            "flag":"",
            "adress": "3333",
            "name":"wang"
        }
  },
  "upsert":{
    "name":"",
    "age":10,
    "sex":"女",
    "phone":"13456789",
    "adress":""
  }
}'

#代码片段

/*
*script
*/
public static UpdateRequest updateScript(String index, String type, String rowId, JSONObject jsonObject) {

        Map<String, Object> param = new HashMap();
        param.put("adress","333");
        param.put("name","wang");
        UpdateRequest updateRequest = new UpdateRequest(index, type, rowId);
        sc = new StringBuffer("if (ctx._source.adress=='') ctx._source.adress=params.adress;" +
                "if (ctx._source.name=='') ctx._source.name=params.name;");

        Script script = new Script(INLINE, Script.DEFAULT_SCRIPT_LANG, sc.toString(), param);
        updateRequest.script(script);
        //按脚本更新,第一次插入之后,也会执行脚本,没有数据的话,插入upsert的内容
        updateRequest.scriptedUpsert(true);
        //这个必须有不然没有数据的话,会报错
        updateRequest.upsert(param);
        return updateRequest;
    }

/*
*doc
*/
public static UpdateRequest updateDoc(String index, String type, String rowId, HashMap json) {
        UpdateRequest updateRequest = new UpdateRequest(index, type, rowId).doc(json);
        updateRequest.docAsUpsert(true);
        return updateRequest;
    }

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值