跟着官网走,能干99。一年几次变,次次不一样。刚部署好ES-6.8,又买阿里云Es-7.10.0根本忙不完。
做为JDK1.8最后一个版本。今天就拿新技术部署一套。致辞:大家以后就用这套好了。别轻易触发springboot3.0了
学习无止境:Dependency Versions
一springboot干货:
pom文件配置:
<elasticsearch.version>7.10.0</elasticsearch.version><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.version}</version></dependency>
一。直接干底层模版接口代码:
/*** description: Es基础功能组件**/
public interface ElasticsearchTemplate<T,M> {/*** 通过Low Level REST Client 查询* @param request 原生查询对象*/public Response request(Request request) throws Exception;/*** 新增索引* @param t 索引pojo*/public boolean save(T t) throws Exception;/*** 新增索引(路由方式)* @param t 索引pojo* @param routing 路由信息(默认路由为索引数据_id)*/public boolean save(T t,String routing) throws Exception;/*** 新增索引集合* @param list 索引pojo集合*/public BulkResponse save(List<T> list) throws Exception;/*** 新增索引集合(分批方式,提升性能,防止es服务内存溢出,每批默认5000条数据)* @param list 索引pojo集合*/public BulkResponse[] saveBatch(List<T> list) throws Exception;/*** 更新索引集合* @param list 索引pojo集合* @return* @throws Exception*/public BulkResponse bulkUpdate(List<T> list) throws Exception;
}
二。模版接口实现类:
@Component
public class ElasticsearchTemplateImpl<T, M> implements ElasticsearchTemplate<T, M> {private Logger logger = LoggerFactory.getLogger(this.getClass());@AutowiredRestHighLevelClient client;@AutowiredElasticsearchIndex elasticsearchIndex;@Overridepublic Response request(Request request) throws Exception {Response response = client.getLowLevelClient().performRequest(request);return response;}@Overridepublic boolean save(T t) throws Exception {return save(t,null);}@Overridepublic boolean save(T t, String routing) throws Exception {MetaData metaData = elasticsearchIndex.getMetaData(t.getClass());String indexname = metaData.getIndexname();String id = Tools.getESId(t);IndexRequest indexRequest=new IndexRequest(indexname);;if (StringUtils.hasText(id)) {indexRequest.id(id);}JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(t));indexRequest.source(jsonObject);if(StringUtils.hasText(routing)){indexRequest.routing(routing);}IndexResponse indexResponse = null;indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {logger.info("INDEX CREATE SUCCESS");elasticsearchIndex.rollover(t.getClass(),true);} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {logger.info("INDEX UPDATE SUCCESS");} else {return false;}return true;}
}
三 。对外客户端提供接口:
/*** program: 对客户端提供api* description:**/
public interface ESCRepository<T,M> {/*** 通过Low Level REST Client 查询*/public Response request(Request request) throws Exception;/*** 新增索引* @param t*/public boolean save(T t) throws Exception;/*** 新增索引集合* @param list*/public BulkResponse save(List<T> list) throws Exception;/*** 按照有值字段更新索引* @param t*/public boolean update(T t) throws Exception;
}
四。对ES提供实现类:
/*** program: ESCRepository对外提供统一接口**/
public class SimpleESCRepository<T,M> implements ESCRepository<T,M> {private Class<T> domainClass;private Class<M> idClass;private ApplicationContext applicationContext;private ElasticsearchTemplate elasticsearchTemplate = null;public SimpleESCRepository(ApplicationContext applicationContext){this.applicationContext = applicationContext;}private ElasticsearchTemplate getElasticsearchTemplate(){return applicationContext.getBean(ElasticsearchTemplate.class);}@Overridepublic Response request(Request request) throws Exception {return getElasticsearchTemplate().request(request);}@Overridepublic boolean save(T o) throws Exception {return getElasticsearchTemplate().save(o);}@Overridepublic BulkResponse save(List<T> list) throws Exception {return getElasticsearchTemplate().save(list);}
}@Overridepublic boolean update(T t) throws Exception {return getElasticsearchTemplate().update(t);}@Overridepublic boolean updateCover(T t) throws Exception {return getElasticsearchTemplate().updateCover(t);}
写到现在终于把底层封装完毕。客户端如何调用
客户端实现接口:
public interface IndexRepository extends ESCRepository<IndexEntity,String> {
}
这就完了,你答对了,至此springboot+ES已经封装完成
直接controller接口测试:
@Resourceprivate IndexRepository indexRepository;@GetMapping("/demo/add")public String add() throws Exception {IndexEntity indexEntity = new IndexEntity ();indexEntity .setProposal_no("1");indexEntity .setAppli_name("a1");indexEntity .setRisk_code("aa1");indexEntity .setSum_premium(1);indexDemoRepository.save(indexEntity );return "新增成功";}@GetMapping("/demo/query")public List<IndexDemo> query() throws Exception {List<IndexDemo> search = indexDemoRepository.search(QueryBuilders.matchAllQuery());return search;}
一套掌法打出。查询结果到手
[{"proposal_no": "1","risk_code": "aa1","risk_name": null,"business_nature": null,"business_nature_name": null,"appli_code": null,"appli_name": "a1","insured_code": null,"insured_name": null,"operate_date": null,"operate_date_format": null,"start_date": null,"end_date": null,"sum_amount": 0.0,"sum_premium": 1.0,"com_code": null}
]
能欣赏到这里说明你对ES的理解钢圈了。需要源码的欢迎加我V。10元