springboot整合es客户端操作elasticsearch(三)
继续上个随笔:
那么我们只需要修改controller中文件就可以完成相关操作
本次主要是对文档得操作:
更新文档:
package com.cxy.elasticsearch.controller; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.SnapshotShardsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.xml.transform.Source; import java.io.IOException; import java.util.HashMap; import java.util.Map; @RestController public class EsindexController { @Autowired RestHighLevelClient client; @Autowired RestClient restClient; @RequestMapping(value = "/updatedoc",method = RequestMethod.GET) public String updatedoc(){ UpdateRequest updateRequest = new UpdateRequest("chenxuyou3","doc","ekBpCG8BAmmLmqjtgRwU"); HashMap<String, String> stringStringHashMap = new HashMap<>(); stringStringHashMap.put("name","Ealsticseach学习实战"); updateRequest.doc(stringStringHashMap); UpdateResponse update =null; try { update= client.update(updateRequest, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } RestStatus status = update.status(); System.err.println(status); return "ok" ; } @RequestMapping(value = "/deletedoc",method = RequestMethod.GET) public String deletedoc(){ String id ="e0CECG8BAmmLmqjt6hwW"; DeleteRequest deleteRequest = new DeleteRequest("chenxuyou3", "doc", id); DeleteResponse delete = null; try { delete = client.delete(deleteRequest, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } RestStatus status = delete.status(); System.err.println(status); return "ok" ; } }
调用接口:
localhost:8085/updatedoc
成功:
调用删除接口,删除得是数据第一条:
接口:
可以看到已经删除了。所以对文档得增加,删除,修改,查询已经完成了,下个随便将会写对文档得精确,模糊,分数查询
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29