1. 新建springboot项目,引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 配置 yaml
elasticsearch:
host: 112.124.7.63
port: 8092
index-name: index-test
3. 编写 ES配置类
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class RestClientConfig extends AbstractElasticsearchConfiguration {
private String host;
private Integer port;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
return new RestHighLevelClient(RestClient.builder(
new HttpHost(host,port)
));
}
}
4. 使用 junit5 测试
@SpringBootTest
public class RestClientTest {
@Value("${elasticsearch.index-name}")
private String indexName;
@Autowired
private RestHighLevelClient client;
@Test
void getIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest(indexName);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
void delete() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName+"-"+20210812);
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete);
}
@Test
void test() throws IOException {
IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.source(XContentType.JSON,
"1","1",
"2","2"
);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index);
}
@Test
void test() throws IOException {
BulkRequest request = new BulkRequest();
request.add(new IndexRequest(indexName)
.source(XContentType.JSON,"1","1","2","2","3","3","4","4"));
request.add(new IndexRequest(indexName)
.source(XContentType.JSON,"11","11","22","22","33","33","44","44"));
request.add(new IndexRequest(indexName)
.source(XContentType.JSON,"111","111","222","222","333","333","444","444"));
BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(bulk);
}
@Test
void reindex() throws IOException {
ReindexRequest reindexRequest = new ReindexRequest();
reindexRequest.setSourceIndices(indexName");
reindexRequest.setDestIndex(indexName+"20210811");
BulkByScrollResponse reindex = client.reindex(reindexRequest, RequestOptions.DEFAULT);
}
}