前言:

网上很多人说spring-boot-data-elasticsearch支持es版本过低不推荐使用,我在官网只找到如下版本对应说明,没有关于spring-boot-data对应版本说明就点开本地pom看了下

spring-data-elasticsearch elasticsearch
3.1.x 6.2.2
3.0.x 5.5.0
2.1.x 2.4.0
2.0.x 2.2.0
1.3.x 1.5.2

本地pom

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.8.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  11. </dependency>
  12. </dependencies>

点击spring-boot-starter-data-elasticsearch查看对应pom

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. <version>2.1.8.RELEASE</version>
  6. <scope>compile</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework.data</groupId>
  10. <artifactId>spring-data-elasticsearch</artifactId>
  11. <version>3.1.10.RELEASE</version>
  12. <scope>compile</scope>
  13. <exclusions>
  14. <exclusion>
  15. <artifactId>jcl-over-slf4j</artifactId>
  16. <groupId>org.slf4j</groupId>
  17. </exclusion>
  18. <exclusion>
  19. <artifactId>log4j-core</artifactId>
  20. <groupId>org.apache.logging.log4j</groupId>
  21. </exclusion>
  22. </exclusions>
  23. </dependency>
  24. </dependencies>

可以看出spring-boot-data-es是对spring-data-es进行了封装, 本地springboot2.1.8对应spring-data-es3.1.10对应es版本为6.2.2


pom配置

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.8.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  11. </dependency>
  12. </dependencies>

配置文件

  1. spring.application.name=es
  2. server.port=8080
  3. server.servlet.context-path=/es
  4. # Elasticsearch cluster name.
  5. spring.data.elasticsearch.cluster-name=my-es
  6. # Comma-separated list of cluster node addresses.
  7. spring.data.elasticsearch.cluster-nodes=47.11.11.11:9200
  8. # 开启 Elasticsearch 仓库(默认值:true)
  9. spring.data.elasticsearch.repositories.enabled=true

实体类

  1. @Document(indexName = "book", type = "_doc")
  2. public class BookBean {
  3. @Id
  4. private String id;
  5. private String title;
  6. private String author;
  7. private String postDate;
  8. public BookBean(){}
  9. public BookBean(String id, String title, String author, String postDate){
  10. this.id=id;
  11. this.title=title;
  12. this.author=author;
  13. this.postDate=postDate;
  14. }
  15. public String getId() {
  16. return id;
  17. }
  18. public void setId(String id) {
  19. this.id = id;
  20. }
  21. public String getTitle() {
  22. return title;
  23. }
  24. public void setTitle(String title) {
  25. this.title = title;
  26. }
  27. public String getAuthor() {
  28. return author;
  29. }
  30. public void setAuthor(String author) {
  31. this.author = author;
  32. }
  33. public String getPostDate() {
  34. return postDate;
  35. }
  36. public void setPostDate(String postDate) {
  37. this.postDate = postDate;
  38. }
  39. @Override
  40. public String toString() {
  41. return "BookBean{" +
  42. "id='" + id + '\'' +
  43. ", title='" + title + '\'' +
  44. ", author='" + author + '\'' +
  45. ", postDate='" + postDate + '\'' +
  46. '}';
  47. }
  48. }

继承es接口

  1. public interface BookRepository extends ElasticsearchRepository<BookBean, String> {
  2. //Optional<BookBean> findById(String id);
  3. List<BookBean> findByAuthor(String author);
  4. List<BookBean> findByTitle(String title);
  5. }

调用

  1. public class BookServiceTest extends EsApplicationTests {
  2. @Resource
  3. private BookRepository bookRepository;
  4. @Test
  5. public void save(){
  6. BookBean book = new BookBean();
  7. book.setId("1");
  8. book.setAuthor("yu");
  9. book.setPostDate("2019-09-02");
  10. book.setTitle("123");
  11. bookRepository.save(book);
  12. }
  13. @Test
  14. public void findByAuthor(){
  15. List<BookBean> yu = bookRepository.findByAuthor("yu");
  16. System.out.println(yu);
  17. }
  18. }

出现的异常

  1. org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{3H-4Xug_QMC2lFQpqPJhVw}]

这个异常初步判断为本地调用远程es不通, 使用http://47.xx.xx.xx:9200/调用显示正常

  1. {
  2. name: "node-1",
  3. cluster_name: "my-es",
  4. cluster_uuid: "WHiWU6ekREq9xoqXFIbjBg",
  5. version: {
  6. number: "6.6.2",
  7. build_flavor: "default",
  8. build_type: "tar",
  9. build_hash: "3bd3e59",
  10. build_date: "2019-03-06T15:16:26.864148Z",
  11. build_snapshot: false,
  12. lucene_version: "7.6.0",
  13. minimum_wire_compatibility_version: "5.6.0",
  14. minimum_index_compatibility_version: "5.0.0"
  15. },
  16. tagline: "You Know, for Search"
  17. }

经查阅资料es需要使用9200-9400的端口号(具体用途不清楚)而不是只有9200
打开阿里云安全组进行配置后就可以正常使用了
image.png