参考官方教程
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.2.0</version>
</dependency>
package com.example.restclient;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class DeleteIndex {
static RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
public static void main(String[] agrs) throws IOException {
// 1、创建请求参数
DeleteIndexRequest request = new DeleteIndexRequest("test");
// 2. 执行删除操作
AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
// 3、处理响应
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println("acknowledged = " + acknowledged);
//4.关闭RestHighLevelClient
client.close();
}
}
报错,找不到方法
Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.action.support.master.AcknowledgedResponse.fromXContent(Lorg/elasticsearch/common/xcontent/XContentParser;)Lorg/elasticsearch/action/support/master/AcknowledgedResponse;
at org.elasticsearch.client.IndicesClient.delete(IndicesClient.java:97)
at com.example.restclient.DeleteIndex.main(DeleteIndex.java:26)
国外用户也遇到类似的错误 https://stackoverflow.com/questions/53755092/i-got-a-java-lang-nosuchmethoderror-when-using-deleteindexrequest
解决办法,
增加一个依赖即可
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.2.0</version>
</dependency>
看来Elasticsearch 7.x的官方文档坑还是很多的。