文章目錄

  • 九、Spring Boot 3與Jakarta EE命名空間問題
  • 1. 舊版客户端與Spring Boot 3不兼容
  • 十、Elasticsearch連接超時問題
  • 1. 啓動時ES服務未完全就緒
  • 十一、中文分詞配置問題
  • 1. 未安裝IK分詞器導致啓動失敗
  • 十二、索引映射配置錯誤
  • 1. 實體類字段類型與索引映射不匹配
  • 十三、版本兼容性問題
  • 1. Spring Boot與Spring Data Elasticsearch版本不匹配
  • 十四、依賴衝突問題
  • 1. 與Netty依賴衝突
  • 十五、配置文件錯誤
  • 1. 未正確配置Elasticsearch連接
  • 十六、索引自動創建問題
  • 1. 索引自動創建導致映射衝突

九、Spring Boot 3與Jakarta EE命名空間問題

1. 舊版客户端與Spring Boot 3不兼容

報錯內容

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/Unmarshaller

原因

  • Spring Boot 3使用Jakarta EE命名空間(jakarta.*包),不再支持Java EE(javax.*包)
  • 依賴了舊版Elasticsearch客户端(基於Java EE)

解決方案

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<properties>
    <elasticsearch.version>8.9.0</elasticsearch.version>
</properties>

十、Elasticsearch連接超時問題

1. 啓動時ES服務未完全就緒

報錯內容

org.elasticsearch.client.ResponseException: method [GET], host [http://localhost:9200], URI [/_cluster/health/], status line [HTTP/1.1 503 Service Unavailable]

原因

  • Elasticsearch服務啓動較慢,應用啓動時ES服務未完全就緒
  • 默認連接超時時間過短

解決方案

spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200
      connection-timeout: 10000 # 10秒
      socket-timeout: 30000 # 30秒

十一、中文分詞配置問題

1. 未安裝IK分詞器導致啓動失敗

報錯內容

org.elasticsearch.index.mapper.MapperParsingException: failed to parse [content]

原因

  • 未安裝IK分詞器插件
  • 未在實體類中正確配置分詞器

解決方案

  1. 安裝IK分詞器插件:
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.9.0/elasticsearch-analysis-ik-8.9.0.zip
  1. 在實體類中正確配置:
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String content;

十二、索引映射配置錯誤

1. 實體類字段類型與索引映射不匹配

報錯內容

org.elasticsearch.index.mapper.MapperParsingException: failed to parse [dateField]

原因

  • 實體類中日期字段類型與ES索引映射不匹配
  • 未正確配置@Field註解

解決方案

// 正確配置日期字段
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
private LocalDateTime dateField;

十三、版本兼容性問題

1. Spring Boot與Spring Data Elasticsearch版本不匹配

報錯內容

Caused by: java.lang.NoSuchMethodError: 
org.springframework.data.elasticsearch.core.ElasticsearchRestClient.getRestHighLevelClient()Lorg/elasticsearch/client/RestHighLevelClient;

原因

  • Spring Boot版本與Spring Data Elasticsearch版本不兼容

Spring Boot與Spring Data Elasticsearch版本映射關係

Spring Boot版本

Spring Data Elasticsearch版本

支持的Elasticsearch服務器版本

2.7.x

4.4.x

7.17.x

3.0.x ~ 3.1.x

5.1.x

8.7.x

3.2.x

5.2.x

8.9.x

3.3+

5.3+

8.10+

解決方案

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<properties>
    <elasticsearch.version>8.9.0</elasticsearch.version>
</properties>

十四、依賴衝突問題

1. 與Netty依賴衝突

報錯內容

availableProcessors is already set to [8], rejecting [8]

原因

  • Spring Data Elasticsearch與Webflux組件(基於Netty)發生衝突

解決方案

@Configuration
public class EsConfig {
    @PostConstruct
    void init() {
        System.setProperty("es.set.netty.runtime.available.processors", "false");
    }
}

十五、配置文件錯誤

1. 未正確配置Elasticsearch連接

報錯內容

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'elasticsearchRestClient' defined in class path resource 
[org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchRestClientAutoConfiguration.class]: 
Cannot create a ElasticsearchRestClient for the given configuration.

原因

  • 配置了錯誤的連接屬性(如使用spring.data.elasticsearch.cluster-nodes而不是spring.elasticsearch.rest.uris

正確配置

spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200

十六、索引自動創建問題

1. 索引自動創建導致映射衝突

報錯內容

ElasticsearchStatusException: MapperParsingException[failed to parse]

原因

  • Spring Data Elasticsearch在第一次操作時自動創建索引,但映射不符合預期

解決方案

  1. 在應用啓動時手動創建索引:
@Component
public class IndexInitializer {
    @EventListener(ApplicationReadyEvent.class)
    public void initIndex() {
        ElasticsearchRestTemplate template = ...; // 注入
        if (!template.indexExists(User.class)) {
            template.createIndex(User.class);
            template.putMapping(User.class);
        }
    }
}
  1. 或提前在Kibana中創建索引模板