SpringBoot2.3集成ElasticSearch7.6

avatar 2021年11月20日18:55:13 6 3477 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

最近在做基于web的svn平台,需要把文件名称等等信息存储到数据库,查询提供查询接口,为了提高查询性能,我们考虑把数据库信息也缓存到ES中一份。

本文介绍springBoot2集成ES7.6。

完整代码demo

1、pom.xml

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

2、application.yml

  1. #spring配置
  2. spring:
  3. # ES 设置
  4. elasticsearch:
  5. rest:
  6. uris: ["http://127.0.0.1:9200"]
  7. connection-timeout: 10s

3、实体类

  1. import lombok.Data;
  2. import org.springframework.data.elasticsearch.annotations.DateFormat;
  3. import org.springframework.data.elasticsearch.annotations.Document;
  4. import org.springframework.data.elasticsearch.annotations.Field;
  5. import org.springframework.data.elasticsearch.annotations.FieldType;
  6. import org.springframework.stereotype.Component;
  7. import javax.persistence.Id;
  8. import java.io.Serializable;
  9. import java.util.Date;
  10. /**
  11. * 文件类对应索引
  12. */
  13. @Component
  14. @Document(indexName = "common_file", indexStoreType = "common_file", shards = 1, replicas = 0, refreshInterval = "10")
  15. @Data
  16. public class EsCommonFile implements Serializable
  17. {
  18. /**
  19. * @Field 文档说明
  20. * @Field(type=FieldType.Text, analyzer="ik_max_word") 表示该字段是一个文本,并作最大程度拆分,默认建立索引
  21. * @Field(type=FieldType.Text,index=false) 表示该字段是一个文本,不建立索引
  22. * @Field(type=FieldType.Date) 表示该字段是一个文本,日期类型,默认不建立索引
  23. * @Field(type=FieldType.Long) 表示该字段是一个长整型,默认建立索引
  24. * @Field(type=FieldType.Keyword) 表示该字段内容是一个文本并作为一个整体不可分,默认建立索引
  25. * @Field(type=FieldType.Float) 表示该字段内容是一个浮点类型并作为一个整体不可分,默认建立索引
  26. * date 、float、long都是不能够被拆分的
  27. */
  28. /**
  29. * ID
  30. */
  31. @Id
  32. private Long id;
  33. /**
  34. * 父ID
  35. */
  36. @Field(type = FieldType.Keyword)
  37. private Long parentId;
  38. /**
  39. * 编码
  40. */
  41. @Field(type = FieldType.Keyword)
  42. private String longCode;
  43. /**
  44. * 文件名称
  45. */
  46. @Field(type = FieldType.Text, analyzer = "simple")
  47. private String fileName;
  48. /**
  49. * 文件类型(1 file,2 folder)
  50. */
  51. @Field(type = FieldType.Keyword)
  52. private Integer fileType;
  53. /**
  54. * 文件资源url
  55. */
  56. @Field(type = FieldType.Text, analyzer = "ik_max_word")
  57. private String fileUrl;
  58. /**
  59. * 文件版本
  60. */
  61. @Field(type = FieldType.Keyword)
  62. private Long revision;
  63. /**
  64. * 创建人账号
  65. */
  66. @Field(type = FieldType.Keyword)
  67. private String creatorName;
  68. /**
  69. * 修改人账号
  70. */
  71. @Field(type = FieldType.Keyword)
  72. private String updaterName;
  73. /**
  74. * 创建人ID
  75. */
  76. @Field(type = FieldType.Keyword)
  77. private String creatorId;
  78. /**
  79. * 创建时间
  80. * 时间format参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
  81. * basic_date_time_no_millis = yyyyMMdd'T'HHmmssZ
  82. */
  83. @Field(type = FieldType.Date, format = DateFormat.basic_date_time_no_millis)
  84. private Date createTime;
  85. /**
  86. * 更新人ID
  87. */
  88. @Field(type = FieldType.Keyword)
  89. private String updaterId;
  90. /**
  91. * 更新时间
  92. */
  93. @Field(type = FieldType.Date, format = DateFormat.basic_date_time_no_millis)
  94. private Date updateTime;
  95. }

 

4、Repostory接口

  1. import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
  2. import org.springframework.stereotype.Repository;
  3. import java.util.List;
  4. /**
  5. * ES文件dao层
  6. *
  7. * @author liuyanzhao
  8. */
  9. @Repository
  10. public interface EsCommonFileRepository extends ElasticsearchRepository<EsCommonFile, Long>
  11. {
  12. /**
  13. * 根据文件名称查询
  14. *
  15. * @param fileName
  16. * @return
  17. */
  18. List<EsCommonFile> findByFileName(String fileName);
  19. }

 

5、SpringBoot启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
  4. @SpringBootApplication
  5. @EnableElasticsearchRepositories("com.xxx.x.repository") // es repository所在包名
  6. public class Application
  7. {
  8. public static void main(String[] args)
  9. {
  10. SpringApplication.run(Application.class, args);
  11. }
  12. }

 

6、控制器测试类

  1. // 部分包自己导入
  2. import io.swagger.annotations.Api;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. /**
  14. * 测试类
  15. * @author lyz
  16. */
  17. @Slf4j
  18. @RestController
  19. @RequestMapping("/esCommonFile")
  20. @Api(tags = "es测试")
  21. @ApiResource(resName = "es测试")
  22. public class EsCommonFileController
  23. {
  24. @Autowired
  25. private EsCommonFileRepository esCommonFileRepository;
  26. /**
  27. * 测试查询所有
  28. *
  29. * @return
  30. */
  31. @GetMapping("/findAll")
  32. public Msg findAll()
  33. {
  34. Iterable<EsCommonFile> iterable = esCommonFileRepository.findAll();
  35. List<EsCommonFile> list = new ArrayList<>();
  36. iterable.forEach(p -> list.add(p));
  37. return Msg.getSuccess(list);
  38. }
  39. /**
  40. * 测试查询所有
  41. *
  42. * @return
  43. */
  44. @GetMapping("/findByFileName")
  45. public Msg findByFileName(String fileName)
  46. {
  47. List<EsCommonFile> list = esCommonFileRepository.findByFileName(fileName);
  48. return Msg.getSuccess(list);
  49. }
  50. /**
  51. * 测试添加
  52. *
  53. * @return
  54. */
  55. @PostMapping("/save")
  56. public Msg save()
  57. {
  58. EsCommonFile esCommonFile = new EsCommonFile();
  59. esCommonFile.setId(100L);
  60. esCommonFile.setFileName("test123.txt");
  61. esCommonFile.setFileUrl("http://DESKTOP-E7NQ281/svn/demo/trunk/test123.txt");
  62. esCommonFile.setFileType(1);
  63. esCommonFile.setLongCode("100000001");
  64. esCommonFile.setParentId(1L);
  65. esCommonFile.setRevision(1L);
  66. esCommonFile.setUpdaterId("6c5e98d5-6977-4047-b1ff-b08a40d3c8b7");
  67. esCommonFile.setCreatorId("6c5e98d5-6977-4047-b1ff-b08a40d3c8b7");
  68. esCommonFile.setUpdaterName("言曌");
  69. esCommonFile.setUpdaterName("言曌");
  70. esCommonFile.setCreatorName("言曌");
  71. esCommonFile.setCreateTime(new Date());
  72. esCommonFile.setUpdateTime(new Date());
  73. esCommonFileRepository.save(esCommonFile);
  74. return Msg.getSuccessMsg("成功");
  75. }
  76. /**
  77. * 测试删除
  78. *
  79. * @param id
  80. * @return
  81. */
  82. @PostMapping("/deleteById")
  83. public Msg deleteById(Long id)
  84. {
  85. esCommonFileRepository.deleteById(id);
  86. return Msg.getSuccessMsg("成功");
  87. }
  88. }

 

 

二、ES开启远程控制

这里只介绍windows的设置

在 elasticsearch.yml 添加

  1. network.host: 0.0.0.0
  2. http.port: 9200
  3. transport.host: localhost
  4. transport.tcp.port: 9300

重启ES

 

然后可以使用 elasticsearch-head 访问

然后就可以远程访问其他机器上的ES了

ES安装过于简单,这里就不介绍了

 

关于使用分词器创建索引的可以看这篇文章

https://liuyanzhao.com/1466610788520300545.html

 

  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0