当前位置:   article > 正文

es java 增删改查_Springboot2+Elasticsearch7基本增删改查

详解springboot2集成es7版本updatequery数据修改

importjava.io.IOException;importjava.util.HashMap;importjava.util.LinkedList;importjava.util.List;importjava.util.Map;importorg.elasticsearch.action.delete.DeleteRequest;importorg.elasticsearch.action.delete.DeleteResponse;importorg.elasticsearch.action.get.GetRequest;importorg.elasticsearch.action.get.GetResponse;importorg.elasticsearch.action.index.IndexRequest;importorg.elasticsearch.action.index.IndexResponse;importorg.elasticsearch.action.search.SearchRequest;importorg.elasticsearch.action.search.SearchResponse;importorg.elasticsearch.action.update.UpdateRequest;importorg.elasticsearch.action.update.UpdateResponse;importorg.elasticsearch.client.RequestOptions;importorg.elasticsearch.client.RestHighLevelClient;importorg.elasticsearch.index.query.QueryBuilders;importorg.elasticsearch.search.SearchHit;importorg.elasticsearch.search.builder.SearchSourceBuilder;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cglib.beans.BeanMap;importorg.springframework.stereotype.Service;importcom.alibaba.fastjson.JSONObject;importcom.wulei.entity.User;

@Servicepublic classESService {//索引名称 (可以理解成数据库的表名)

private static final String index_name = "user";//es对象

@AutowiredprivateRestHighLevelClient client;public intadd() {int count = 0;try{//从数据库拿到数据

List list =User.getAll();for(User u : list) {

addIndex(u, String.valueOf(u.getId()));

count+= 1;

}

}catch(Exception e) {//TODO: handle exception

}returncount;

}/*** 添加索引

*@paramuser

*@paramid

*@throwsIOException*/

public void addIndex(User user, String id) throwsIOException{//创建一个索引,指定文档id,source是文档内容

IndexRequest request = newIndexRequest(index_name).id(id).source(beanToMap(user));

IndexResponse response=client.index(request, RequestOptions.DEFAULT);

System.out.println(response);

}/*** 查询

*@paramid

*@return*@throwsIOException*/

public Object get(String id) throwsIOException{

GetRequest getRequest= newGetRequest(index_name,id);

GetResponse response=client.get(getRequest,RequestOptions.DEFAULT);

Map source =response.getSource();

System.out.println(response);returnJSONObject.toJSON(source);

}/*** 删除

*@paramid

*@return*@throwsIOException*/

public boolean del(String id) throwsIOException{

DeleteRequest request= newDeleteRequest(index_name,id);

DeleteResponse res=client.delete(request, RequestOptions.DEFAULT);

System.out.println(res);return true;

}/*** 改

*@paramid

*@return*@throwsIOException*/

public boolean edit(User user,String id) throwsIOException{

UpdateRequest request= newUpdateRequest(index_name,id).doc(beanToMap(user));

UpdateResponse name=client.update(request,RequestOptions.DEFAULT);

System.out.println(name);return true;

}/*** 条件搜索

*@paramkey

*@paramvalue

*@return*@throwsIOException*/

public List select(String key,String value,int start, int end,int type) throwsIOException {//指定索引,类似于数据库的表

SearchRequest searchRequest = newSearchRequest(index_name);//创建查询对象,相当于写查询sql

SearchSourceBuilder searchSourceBuilder = newSearchSourceBuilder();if(type==1) {//matchQuery是模糊查询,会对key进行分词

searchSourceBuilder.query(QueryBuilders.matchQuery(key,value));

}else if(type==2) {//termQuery是精准查询

searchSourceBuilder.query(QueryBuilders.termQuery(key,value));

}//指定从哪条开始查询

searchSourceBuilder.from(start);//需要查出的总记录条数

searchSourceBuilder.size(end);

searchRequest.source(searchSourceBuilder);

SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);

System.out.println(JSONObject.toJSON(response));

SearchHit[] hits=response.getHits().getHits();

List list = new LinkedList<>();for(SearchHit hit: hits){

User user= JSONObject.parseObject(hit.getSourceAsString(),User.class);

list.add(user);

}returnlist;

}/*** 对象转map

*@parambean

*@param

*@return

*/

public MapbeanToMap(T bean) {

Map map = new HashMap<>();if (bean != null) {

BeanMap beanMap=BeanMap.create(bean);for(Object key : beanMap.keySet()) {if(beanMap.get(key) != null){

map.put(key+ "", beanMap.get(key));

}

}

}returnmap;

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/68811
推荐阅读
相关标签
  

闽ICP备14008679号