中使用ElasticSearch8.1版本和'org.springframework.boot:spring-boot-starter-data-elasticsearch‘的
计划
我们的项目。Repository.save()抛出以下异常。
java.base/java.util.Objects.requireNonNull(Objects.java:221) at org.elasticsearch.action.DocWriteResponse.(DocWriteResponse.java:116) at org.elasticsearch.action.index.IndexResponse.(IndexResponse.java:43)的java.lang.NullPointerException: null
与Elasticsearch 7.15.2相同的代码运行良好。我在这里看到了支持的矩阵,https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.requirements在哪里可以看到数据插件的路线图?的8.1版本的插件支持。
Elasticsearch?提前感谢
发布于 2022-03-31 03:05:03
添加以下标头解决了问题。
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
发布于 2022-07-24 12:18:40
更改用于创建客户端的代码会更好。
package com.search.elasticsearchapp.config;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.http.HttpHeaders;
@Configuration
@EnableElasticsearchRepositories(basePackages = "*")
public class TestClient {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.protocol}")
private String protocol;
@Value("${elasticsearch.username}")
private String userName;
@Value("${elasticsearch.password}")
private String password;
@Bean(destroyMethod = "close")
public RestHighLevelClient restClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, protocol))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.setDefaultHeaders(compatibilityHeaders());
return new RestHighLevelClient(builder);
}
private Header[] compatibilityHeaders() {
return new Header[]{new BasicHeader(HttpHeaders.ACCEPT, "application/vnd.elasticsearch+json;compatible-with=7"), new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.elasticsearch+json;compatible-with=7")};
}
}
https://stackoverflow.com/questions/71681568
复制相似问题