五分钟带你玩转Elasticsearch(十二)企业实战——整合spring boot

举报
小鲍侃java 发表于 2021/09/09 23:07:19 2021/09/09
【摘要】 楼主使用es版本为7.6,使用的spring boot提供的start  1.pom文件 <!-- elasticsearch --> <dependency> <groupId>org.springframework.boot<...

楼主使用es版本为7.6,使用的spring boot提供的start 

1.pom文件


  
  1. <!-- elasticsearch -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>

2.application.yml


  
  1. spring:
  2. elasticsearch:
  3. rest:
  4. uris: ip:9200
  5. username: xxxx
  6. password: xxxx
  7. data:
  8. elasticsearch:
  9. repositories:
  10. enabled: true
  11. client:
  12. reactive:
  13. # username: xxx 这种写法是不对的
  14. # password: xxx 这种写法是不对的
  15. # endpoints: ip:9200 这种写法是不对的
  16. use-ssl: false

注意:

     1.配置es地址要写在楼主这个位置,否则会默认为localhost:9200

      2.与旧版本的配置不同 开启的端口号为9200

3.config文件


  
  1. @Configuration
  2. public class ElasticSearchConfig {
  3. /**
  4. * 防止netty的bug
  5. * java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
  6. */
  7. @PostConstruct
  8. void init() {
  9. System.setProperty("es.set.netty.runtime.available.processors", "false");
  10. }
  11. }

4.接口编写


  
  1. @Autowired
  2. ElasticsearchRestTemplate elasticsearchTemplate;
  3. @Log(operationName = "日志-查询登录日志")
  4. @PostMapping("/selectLoginLog")
  5. public Result<List<LoginLogVO>> selectLoginLog(@RequestBody LoginLogInputVO loginLogInputVO) throws ParseException {
  6. // 日期格式化
  7. SimpleDateFormat sd = new SimpleDateFormat(DateFormatEnum.YYYY_MM_DD_HH_MM_SS.getFormat());
  8. // 返回对象
  9. Result<List<LoginLogVO>> result = new Result();
  10. NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
  11. BoolQueryBuilder bool = QueryBuilders.boolQuery();
  12. // 用户名不为空
  13. if (!CommonUtil.isEmpty(loginLogInputVO.getUserName())) {
  14. bool.must(QueryBuilders.wildcardQuery("userName", "*" + loginLogInputVO.getUserName() + "*"));
  15. }
  16. // 时间不为空
  17. if (!CommonUtil.isEmpty(loginLogInputVO.getBeginTime()) && !CommonUtil.isEmpty(loginLogInputVO.getEndTime())) {
  18. List<QueryBuilder> filters = bool.filter();
  19. filters.add(QueryBuilders.rangeQuery("time")
  20. .gte(sd.parse(loginLogInputVO.getBeginTime() + DateFormatEnum.BEGIN_HH_MM_SS.getFormat()))
  21. .lte(sd.parse(loginLogInputVO.getEndTime() + DateFormatEnum.END_HH_MM_SS.getFormat())));
  22. }
  23. // 分页查询
  24. if (!CommonUtil.isEmpty(loginLogInputVO.getPageSize()) && !CommonUtil.isEmpty(loginLogInputVO.getPageNum())) {
  25. // 从第0页开始
  26. builder.withPageable(PageRequest.of(loginLogInputVO.getPageNum() - 1, loginLogInputVO.getPageSize()));
  27. }
  28. builder.withSort(SortBuilders.fieldSort("time").order(SortOrder.ASC));
  29. // 构造查询条件
  30. builder.withQuery(bool);
  31. NativeSearchQuery query = builder.build();
  32. IndexCoordinates indexCoordinates = IndexCoordinates.of("XXXX-*");
  33. SearchHits<LoginLogVO> resultIter = elasticsearchTemplate.search(query, LoginLogVO.class, indexCoordinates);
  34. // 格式化输出
  35. List<LoginLogVO> resultList = new ArrayList<>();
  36. List<SearchHit<LoginLogVO>> SearchHitList = resultIter.getSearchHits();
  37. for (SearchHit<LoginLogVO> loginLogVOSearchHit : SearchHitList) {
  38. resultList.add(loginLogVOSearchHit.getContent());
  39. }
  40. // 分页返回
  41. if (!CommonUtil.isEmpty(loginLogInputVO.getPageSize()) && !CommonUtil.isEmpty(loginLogInputVO.getPageNum())) {
  42. result.setTotal(resultIter.getTotalHits());
  43. result.setPageNum(loginLogInputVO.getPageNum());
  44. result.setPageSize(loginLogInputVO.getPageSize());
  45. }
  46. result.setData(resultList);
  47. return result;
  48. }

5.POJO

因为楼主前后端约定的时间类型为string,所以需要转换类型然后传回。


  
  1. @Document(indexName = "loginlog-*", shards = 1, replicas = 0)
  2. public class LoginLogVO {
  3. @Id
  4. private String id;
  5. /**
  6. * 信息
  7. */
  8. @Field(type = FieldType.Keyword, analyzer = "ik_max_word")
  9. private String message;
  10. @Field(type = FieldType.Date, store = true, format = DateFormat.date_time)
  11. private Date time;
  12. }
  13. public class LoginLogInputVO extends BaseEntity {
  14. /**
  15. * 用户账号
  16. */
  17. private String userName;
  18. /**
  19. * 开始时间
  20. */
  21. private String beginTime;
  22. /**
  23. * 结束时间
  24. */
  25. private String endTime;
  26. }
  27. public class LoginLogOutputVO extends BaseEntity {
  28. private String id;
  29. /**
  30. * 信息
  31. */
  32. private String message;
  33. /**
  34. * 执行时间
  35. */
  36. private String time;
  37. }

文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。

原文链接:baocl.blog.csdn.net/article/details/112780482

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。