赞
踩
Kp k1=new Kp()Kp k2=new Kp()List<Kp> kps=new ArrayList<Kp>()kps.add(kp1)kps.add(kp2)//==========================Quest quest=new Quest()quest.setKps(kps)List<Quest> quests=new ArrayList<Quest>()quests.add(quest)//==========================User user=new User()user.setUid("001")user.setQuests(quests)//组装好Java Bean后,转成json向es服务端进行索引String json=JSON.toJSON(user).toString();//这里用的FastJson框架IndexResponse response=client.prepareIndex("Index", "type",user.getUid()).setSource(json).execute().actionGet();
_ StringBuffer sb_json = new StringBuffer("ctx._source.quests += quest");//脚本主体 HashMap<String, Object> params = new java.util.HashMap<String, Object>()//Map组装 params.put("quest", JSON.toJSON(user.getQuests))//此处不能用JSON.toJSON(user.getQuests).toString方法,quest必须是一个对象,否则会报错 Script script = new Script(sb_json.toString(), ScriptService.ScriptType.INLINE, "groovy", params)//组装脚本 client.prepareUpdate(dynamicIndexName, typeName, uid).setScript(script).execute().get() //发送请求
- StringBuffer sb_json = new Stri ngBuffer("ctx._source.quests.removeAll{it.qid == remove_id}"); java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>(); params.put("remove_id", "qid2");//此处不能用JSON.toJSON(user.getQuests).toString方法,quest必须是一个对象,否则会报错 Script script = new Script(sb_json.toString(), ScriptService.ScriptType.INLINE, "groovy", params); client.prepareUpdate(dynamicIndexName, typeName, uid).setScript(script).execute().get();
- StringBuffer sb_json = new StringBuffer("ctx._source.quests.findAll { if(it.qid==qid2||it.qid==qid3) { it.kps += kp5 } } "); java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>(); params.put("qid2", "qid2"); params.put("qid3", "qid3"); params.put("kp5",JSON.toJSON(kp1)); Script script = new Script(sb_json.toString(), ScriptService.ScriptType.INLINE, "groovy", params); client.prepareUpdate(dynamicIndexName, typeName, uid).setScript(script).execute().get();
- StringBuffer sb_json = new StringBuffer("ctx._source.quests.findAll { if(it.qid==qid2||it.qid==qid3) { it.kps.removeAll {it.kid==kid} } } "); //删除第三层数据 java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>(); params.put("qid2", "qid2"); params.put("qid3", "qid3"); params.put("kid", "kid3"); Script script = new Script(sb_json.toString(), ScriptService.ScriptType.INLINE, "groovy", params); client.prepareUpdate(dynamicIndexName, typeName, uid).setScript(script).execute().get();
- StringBuffer sb_json = new StringBuffer("ctx._source.quests.findAll { if(it.qid==qid3) { it.kps.findAll{ if(it.kid==kid){ it.kname=kname;it.kmd=kmd } } } } "); //更新第三层数据 java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>(); params.put("qid2", "qid2"); params.put("kname","地球的引力"); params.put("kid","kid5"); params.put("kmd",0.78); Script script = new Script(sb_json.toString(), ScriptService.ScriptType.INLINE, "groovy", params); client.prepareUpdate(dynamicIndexName, typeName, uid).setScript(script).execute().get();
- StringBuffer sb_json = new StringBuffer("ctx._source.quests.findAll { if(it.qid==qid) { it.qtime=qtime } } "); //更新第三层数据 java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>(); params.put("qid", "qid1");//此处不能用JSON.toJSON(user.getQuests).toString方法,quest必须是一个对象,否则会报错 params.put("qtime",5558) Script script = new Script(sb_json.toString(), ScriptService.ScriptType.INLINE, "groovy", params); client.prepareUpdate(dynamicIndexName, typeName, uid).setScript(script).execute().get();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。