• springboot-2.7.3+ES-7.10.0


    跟着官网走,能干99。一年几次变,次次不一样。刚部署好ES-6.8,又买阿里云Es-7.10.0根本忙不完。

    做为JDK1.8最后一个版本。今天就拿新技术部署一套。致辞:大家以后就用这套好了。别轻易触发springboot3.0了

    学习无止境:Dependency Versions

    一springboot干货:

    pom文件配置:

    1. 7.10.0
    2. org.elasticsearch
    3. elasticsearch
    4. ${elasticsearch.version}
    5. org.elasticsearch.client
    6. elasticsearch-rest-high-level-client
    7. ${elasticsearch.version}

    一。直接干底层模版接口代码:

    1. /**
    2. * description: Es基础功能组件
    3. **/
    4. public interface ElasticsearchTemplate {
    5. /**
    6. * 通过Low Level REST Client 查询
    7. * @param request 原生查询对象
    8. */
    9. public Response request(Request request) throws Exception;
    10. /**
    11. * 新增索引
    12. * @param t 索引pojo
    13. */
    14. public boolean save(T t) throws Exception;
    15. /**
    16. * 新增索引(路由方式)
    17. * @param t 索引pojo
    18. * @param routing 路由信息(默认路由为索引数据_id)
    19. */
    20. public boolean save(T t,String routing) throws Exception;
    21. /**
    22. * 新增索引集合
    23. * @param list 索引pojo集合
    24. */
    25. public BulkResponse save(List list) throws Exception;
    26. /**
    27. * 新增索引集合(分批方式,提升性能,防止es服务内存溢出,每批默认5000条数据)
    28. * @param list 索引pojo集合
    29. */
    30. public BulkResponse[] saveBatch(List list) throws Exception;
    31. /**
    32. * 更新索引集合
    33. * @param list 索引pojo集合
    34. * @return
    35. * @throws Exception
    36. */
    37. public BulkResponse bulkUpdate(List list) throws Exception;
    38. }

    二。模版接口实现类:

    1. @Component
    2. public class ElasticsearchTemplateImpl implements ElasticsearchTemplate {
    3. private Logger logger = LoggerFactory.getLogger(this.getClass());
    4. @Autowired
    5. RestHighLevelClient client;
    6. @Autowired
    7. ElasticsearchIndex elasticsearchIndex;
    8. @Override
    9. public Response request(Request request) throws Exception {
    10. Response response = client.getLowLevelClient().performRequest(request);
    11. return response;
    12. }
    13. @Override
    14. public boolean save(T t) throws Exception {
    15. return save(t,null);
    16. }
    17. @Override
    18. public boolean save(T t, String routing) throws Exception {
    19. MetaData metaData = elasticsearchIndex.getMetaData(t.getClass());
    20. String indexname = metaData.getIndexname();
    21. String id = Tools.getESId(t);
    22. IndexRequest indexRequest=new IndexRequest(indexname);;
    23. if (StringUtils.hasText(id)) {
    24. indexRequest.id(id);
    25. }
    26. JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(t));
    27. indexRequest.source(jsonObject);
    28. if(StringUtils.hasText(routing)){
    29. indexRequest.routing(routing);
    30. }
    31. IndexResponse indexResponse = null;
    32. indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
    33. if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
    34. logger.info("INDEX CREATE SUCCESS");
    35. elasticsearchIndex.rollover(t.getClass(),true);
    36. } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
    37. logger.info("INDEX UPDATE SUCCESS");
    38. } else {
    39. return false;
    40. }
    41. return true;
    42. }
    43. }

    三 。对外客户端提供接口:

    1. /**
    2. * program: 对客户端提供api
    3. * description:
    4. **/
    5. public interface ESCRepository {
    6. /**
    7. * 通过Low Level REST Client 查询
    8. */
    9. public Response request(Request request) throws Exception;
    10. /**
    11. * 新增索引
    12. * @param t
    13. */
    14. public boolean save(T t) throws Exception;
    15. /**
    16. * 新增索引集合
    17. * @param list
    18. */
    19. public BulkResponse save(List list) throws Exception;
    20. /**
    21. * 按照有值字段更新索引
    22. * @param t
    23. */
    24. public boolean update(T t) throws Exception;
    25. }

    四。对ES提供实现类:

    1. /**
    2. * program: ESCRepository对外提供统一接口
    3. **/
    4. public class SimpleESCRepository implements ESCRepository {
    5. private Class domainClass;
    6. private Class idClass;
    7. private ApplicationContext applicationContext;
    8. private ElasticsearchTemplate elasticsearchTemplate = null;
    9. public SimpleESCRepository(ApplicationContext applicationContext){
    10. this.applicationContext = applicationContext;
    11. }
    12. private ElasticsearchTemplate getElasticsearchTemplate(){
    13. return applicationContext.getBean(ElasticsearchTemplate.class);
    14. }
    15. @Override
    16. public Response request(Request request) throws Exception {
    17. return getElasticsearchTemplate().request(request);
    18. }
    19. @Override
    20. public boolean save(T o) throws Exception {
    21. return getElasticsearchTemplate().save(o);
    22. }
    23. @Override
    24. public BulkResponse save(List list) throws Exception {
    25. return getElasticsearchTemplate().save(list);
    26. }
    27. }
    28. @Override
    29. public boolean update(T t) throws Exception {
    30. return getElasticsearchTemplate().update(t);
    31. }
    32. @Override
    33. public boolean updateCover(T t) throws Exception {
    34. return getElasticsearchTemplate().updateCover(t);
    35. }

    写到现在终于把底层封装完毕。客户端如何调用

    客户端实现接口:

    1. public interface IndexRepository extends ESCRepository {
    2. }

    这就完了,你答对了,至此springboot+ES已经封装完成

    直接controller接口测试

    1. @Resource
    2. private IndexRepository indexRepository;
    3. @GetMapping("/demo/add")
    4. public String add() throws Exception {
    5. IndexEntity indexEntity = new IndexEntity ();
    6. indexEntity .setProposal_no("1");
    7. indexEntity .setAppli_name("a1");
    8. indexEntity .setRisk_code("aa1");
    9. indexEntity .setSum_premium(1);
    10. indexDemoRepository.save(indexEntity );
    11. return "新增成功";
    12. }
    13. @GetMapping("/demo/query")
    14. public List query() throws Exception {
    15. List search = indexDemoRepository.search(QueryBuilders.matchAllQuery());
    16. return search;
    17. }

    一套掌法打出。查询结果到手

    1. [
    2. {
    3. "proposal_no": "1",
    4. "risk_code": "aa1",
    5. "risk_name": null,
    6. "business_nature": null,
    7. "business_nature_name": null,
    8. "appli_code": null,
    9. "appli_name": "a1",
    10. "insured_code": null,
    11. "insured_name": null,
    12. "operate_date": null,
    13. "operate_date_format": null,
    14. "start_date": null,
    15. "end_date": null,
    16. "sum_amount": 0.0,
    17. "sum_premium": 1.0,
    18. "com_code": null
    19. }
    20. ]

    能欣赏到这里说明你对ES的理解钢圈了。需要源码的欢迎加我V。10元

  • 相关阅读:
    TC8:UDP_INTRODUCTION_01-03
    LeetCode 179 最大数
    python之APScheduler
    在Kubernetes环境中有关Nginx Ingress与API Gateway的连接问题
    [ 漏洞复现篇 ] Apache Shiro 身份认证绕过漏洞 (CVE-2022-32532)
    Android 开发学习(二)
    js去除数组对象中的重复对象
    java计算机毕业设计springboot+vue在线选课系统
    第十五届全国大学生数学竞赛初赛试卷解析
    MySQL 基础知识(八)之用户权限管理
  • 原文地址:https://blog.csdn.net/qq_38564282/article/details/134181667