1.安装7.6.2的es
访问地址+端口显示如下表示安装成功。
2.加入maven依赖,在properties中制定版本号
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<elasticsearch.version>7.6.2</elasticsearch.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
3.创建es的连接配置类
@Configuration
public class ElasticsearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client= new RestHighLevelClient(
RestClient.builder(new HttpHost("***",9200,"http")));
return client;
}
}
4.一些简单的es操作测试
注解注入es类
@Autowired
private RestHighLevelClient restHighLevelClient;
- 添加索引值
@Test
void testIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("d_index");
CreateIndexResponse createIndexResponse= restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
- 判断索引是否添加成功,返回true代表存在索引。
@Test
void testExitIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("d_index");
boolean exits=
restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exits);
}
- 删除索引
@Test
void testdelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("d_index");
AcknowledgedResponse delete =restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
- 添加文档内容