我搜索了很多帖子,但我找不到將 Elastic Search 與 Spring Boot 應用程式一起使用的正確方法,因為我對 Elastic Search 完全陌生。
我唯一的依賴是:org.springframework.boot spring-boot-starter-data-elasticsearch 2.7.3
我的配置類是:
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.backend.repository.elasticsearchrepository")
@ComponentScan(basePackages = {"com.backend.model.elasticsearchmodel"})
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
@Value("${spring.elasticsearch.url}")
public String elasticsearchUrl;
@Value("${spring.elasticsearch.username}")
public String username;
@Value("${spring.elasticsearch.password}")
public String password;
@Bean
@Override
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration config = ClientConfiguration.builder()
.connectedTo(elasticsearchUrl)
.withBasicAuth(username, password)
.build();
return RestClients.create(config).rest();
}
}
此處 RestHighLevelClient 顯示為已棄用。我的存盤庫類是:
package com.backend.repository.elasticsearchrepository;
import com.backend.model.elasticsearchmodel.EsOffice;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.UUID;
public interface ESOfficeRepository extends ElasticsearchRepository<EsOffice, UUID> {
}
當我呼叫此存盤庫的方法時,它可以正常作業,但是在存盤資料時它會回傳錯誤訊息,即使它成功添加了資料。
2022-10-15 00:00:15.608 ERROR 51607 --- [nio-8080-exec-2] c.a.a.exception.GlobalExceptionHandler : Unable to parse response body for Response{requestLine=POST /office/_doc?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}; nested exception is java.lang.RuntimeException: Unable to parse response body for Response{requestLine=POST /office/_doc?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}
我應該使用哪個 POM 依賴項 什么樣的存盤庫以及如何在我的組態檔中配置它?我需要這三個相互兼容的嗎?
uj5u.com熱心網友回復:
Spring Data Elasticsearch 4.4(由 Spring Boot 2.7.3 引入)是使用版本 7.17 中的 Elasticsearch 庫構建的,在版本 8 中針對 Elasticsearch 集群運行時會出現問題。您基本上有兩種選擇:
如果可能,將您的集群降級到版本 7.17.6(當前可用的最新 7.17)。
您可以嘗試查看是否設定了兼容性標頭(有關更多資訊,請參閱Spring Data Elasticsearch 檔案第 5.3.1 節)。這應該可行,但我遇到了集群回應仍然無法使用 7.17 客戶端讀取的情況。- 我在 Elasticsearch 中打開了一些問題并解決了,但仍然可能存在隱藏的陷阱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516197.html
上一篇:使用axios卡在補丁請求上
