文档
https://www.elastic.co/guide/index.html https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started.html
1.导入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.1</version>
</dependency>
2.构建对象 创建项目 配置基本的项目 问题:一定要保证,我们导入的依赖和我们的es版本一致 编写配置类
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
}
1.创建索引
@SpringBootTest
class EsApiApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//索引的创建 request
@Test
void testCreateIndex() throws IOException {
//1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest("kuan_index");
//2.执行请求
CreateIndexResponse createIndexResponse =
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
}
2.判断索引是否存在
//判断索引是否存在
@Test
void testExistIndex()throws IOException{
//1.创建索引请求
GetIndexRequest request = new GetIndexRequest("kuan_index");
//2.执行请求
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
3.删除索引
//删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("kuan_index");
//删除
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete);
}