1. 引入Pom文件的依赖
**注意,如果要用elasticsearch-rest-high-level-client 去操作es,
必须要有这三个依赖**
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.12.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.12.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
2.添加配置文件EsConfig
package com.govindex.web.core.config;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Auther: xxx
* @Date: 2021/6/10 17:21
* @Description:
*/
@Configuration
public class EsConfig {
@Value("${elasticsearch.hostname}")
private String hostname;
@Value("${elasticsearch.port}")
private int port;
/**
* LowLevelRestConfig
*/
@Bean
public RestClient restClient() {
// 如果有多个从节点可以持续在内部new多个HttpHost,参数1是IP,参数2是端口,参数3是通信协议
RestClientBuilder clientBuilder = RestClient.builder(new HttpHost(hostname, port, "http"));
// 设置Header编码
Header[] defaultHeaders = {new BasicHeader("content-type", "application/json")};
clientBuilder.setDefaultHeaders(defaultHeaders);
// 添加其他配置,这些配置都是可选的,详情配置可看https://blog.csdn.net/jacksonary/article/details/82729556
return clientBuilder.build();
}
/**
* HighLevelRestConfig
*/
@Bean
public RestHighLevelClient restHighLevelClient() {
// 如果有多个从节点可以持续在内部new多个HttpHost,参数1是IP,参数2是端口,参数3是通信协议
return new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, "http")));
}
}
3.配置yml文件
#####################################es配置#####################################
elasticsearch:
hostname: 127.0.0.1
port: 9200
4.编写测试类
- controller
package com.govindex.web.controller.es;
import com.govindex.common.core.domain.AjaxResult;
import com.govindex.main.service.IRestHighLevelClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* @Author: xxx
* @Date: 2021/5/24 13:50
* @Description:
*/
@Api(tags = {"es"})
@RestController
@RequestMapping("/es")
public class EsController {
@Autowired
private IRestHighLevelClientService restHighLevelClientService;
/**
* 创建索引
* @return
* @throws IOException
*/
@PostMapping("/createIndex")
@ApiOperation(value = "创建索引", notes = "创建索引")
public AjaxResult createIndex(@RequestParam String indexName) throws IOException {
AjaxResult ajax = AjaxResult.success();
CreateIndexResponse index = restHighLevelClientService.createIndex(indexName);
ajax.put("AjaxResult", index);
return ajax;
}
/**
* 删除索引
* @return
* @throws IOException
*/
@PostMapping("/deleteIndex")
@ApiOperation(value = "删除索引", notes = "删除索引")
public AjaxResult deleteIndex(@RequestParam String indexName) throws IOException {
AjaxResult ajax = AjaxResult.success();
AcknowledgedResponse acknowledgedResponse = restHighLevelClientService.deleteIndex(indexName);
ajax.put("AjaxResult", acknowledgedResponse);
return ajax;
}
}
- service
package com.govindex.main.service;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
/**
* @Author: xxx
* @Date: 2021/5/24 14:03
* @Description:
*/
public interface IRestHighLevelClientService {
CreateIndexResponse createIndex(String indexName) throws IOException;
AcknowledgedResponse deleteIndex(String indexName) throws IOException;
}
- serviceImpl
package com.govindex.main.service.impl;
import com.govindex.main.service.IRestHighLevelClientService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @Author: yanziyu
* @Date: 2021/5/24 14:05
* @Description:
*/
@Service
public class RestHighLevelClientServiceImpl implements IRestHighLevelClientService {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public CreateIndexResponse createIndex(String indexName) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(indexName.toLowerCase());
request.settings(Settings.builder()
.put("index.number_of_shards", 5)
.put("index.number_of_replicas", 0)
);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
return createIndexResponse;
}
@Override
public AcknowledgedResponse deleteIndex(String indexName) throws IOException {
DeleteIndexRequest indexRequest = new DeleteIndexRequest(indexName);
AcknowledgedResponse delete = restHighLevelClient.indices().delete(indexRequest, RequestOptions.DEFAULT);
return delete;
}
}
最后可以启动项目进行测试