我在使用Java RESTHIGHEGELLEVELCLIENT in elasticsearch 和我的 create IndexexResponse 对象是null。
我实际上能够创建索引,稍后可以确认该索引,但是当我创建索引时,我会得到此例外。这里我的代码:
`CreateIndexRequest request = new CreateIndexRequest("myindex");
CreateIndexResponse createIndexResponse = client.indices().create(request);`
elasticsearch返回成功的消息:
`HTTP 200 Success
{
"acknowledged": true,
"shards_acknowledged": true
}`
,我实际上可以稍后用get呼叫检索索引,但是当 ResthighLevelClient 尝试使用以下内部呼叫:
尝试解析响应。//Type of the response converter: CheckedFunction<Req, Request, IOException> requestConverter
responseConverter.apply(response);
以下例外发生:
java.io.IOException: Unable to parse response body for
Response{requestLine=PUT /myindex?master_timeout=30s&timeout=30s HTTP/1.1,
host=http://localhost:9200, response=HTTP/1.1 200 OK}
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:507)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:474)
at org.elasticsearch.client.IndicesClient.create(IndicesClient.java:77)
at hello.client.HelloClient.createSynch(HelloClient.java:84)
at hello.main.Main.main(Main.java:25)
Caused by: java.lang.IllegalArgumentException: Required [index]
基本上说的是不能解析以下响应,但是对我来说,它看起来很可简化:
Response{requestLine=PUT /myindex?master_timeout=30s&timeout=30s HTTP/1.1,
host=http://localhost:9200, response=HTTP/1.1 200 OK}
为什么它告诉我索引缺失了?我错误地使用了Java客户端吗?这是版本:
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.1</version>
</dependency>
</dependencies>`
事先感谢您的帮助!
您需要更新版本的依赖项或添加兼容性标头(https://docs.spring.io/spring-data/elasticsearch/elasticsearch/docs/current/current/referent/referent/referent/referent/reference/html/(对于我的情况,即使是最新版本的Spring-DATA弹性搜索也不支持弹性搜索版本8 。必须这样配置我的客户:
@Configuration
@EnableElasticsearchRepositories(basePackages = "*")
public class ElasticsearchClientConfig {
@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")};
}
}
这里需要考虑两件事。以下示例包括用于连接到Elasticsearch 8.5群集的代码。
-
添加自定义标头
@Bean(name = "client", destroyMethod = "close") public RestHighLevelClient client() { String uri = applicationConfig.getElasticSearchDB(); String[] hosts = uri.split(Constants.COMMA); List<HttpHost> httpHosts = new ArrayList<>(); Arrays.stream(hosts).forEach(a -> httpHosts.add(new HttpHost(String.valueOf(a.split(Constants.COLON)[0]), Integer.parseInt(a.split(Constants.COLON)[1]), "http"))); HttpHost[] esHosts = new HttpHost[httpHosts.size()]; httpHosts.toArray(esHosts); return new RestHighLevelClient(RestClient .builder(esHosts) .setDefaultHeaders(compatibilityHeaders())); } 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") }; }
-
验证依赖模块
-
如果将
spring-boot-starter-data-elasticsearch
用作依赖关系,请使用https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-boot-boot-starter-parter-nater-data-data-elasticsearchch/pecle https://mvnrepository.com/artifact/org.springframework-> -
就我而言,因为我使用的是Elasticsearch版本8.5,所以我必须在POM中使用以下内容。您可以检查上面网页中的
Compile Dependencies
部分以检查需要使用的版本。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>3.0.5</version> </dependency>
我使用opensearch遇到了相同的问题,对我来说 - 我升级了jar依赖项版本:https://opensearch.org/docs/latest/clients/clients/java/。然后问题消失了。