赞
踩
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;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。