springboot整合es客户端操作elasticsearch
Posted 菩提树下的韦小宝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
成功:
调用删除接口,删除得是数据第一条:
接口:
可以看到已经删除了。所以对文档得增加,删除,修改,查询已经完成了,下个随便将会写对文档得精确,模糊,分数查询
以上是关于springboot整合es客户端操作elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合SpringDataElasticSearch操作ES
ElasticSearch04_elasticsearch-Rest-Client整合SpringBoot中使用保存数据利用JAVA代码操作es