setp1 POM文件
版本约定:springboot2.1.6.RELEASE + es7.6.2
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>esLearn</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<elasticsearch.version>7.6.2</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
</project>
step2 yaml配置
es:
host: {your es host}
port: {your es port}
step3 注入工具类
@Configuration
public class EsConfig {
@Value("${es.host}")
private String esHost;
@Value("${es.port}")
private int esPort;
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(RestClient.builder(new HttpHost(esHost, esPort, "http")));
}
}
step4 使用
@Service
public class EsService {
@Resource
private RestHighLevelClient restHighLevelClient;
public void es() throws IOException {
// 创建索引(不恰当的类似于mysql的table)
CreateIndexRequest create = new CreateIndexRequest("x_index");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(create, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.toString());
// 获取索引(只能判读是否存在)
GetIndexRequest getIndexRequest = new GetIndexRequest("x_index");
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
// 删除索引
DeleteIndexRequest xu_index = new DeleteIndexRequest("x_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(xu_index, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
// 文档添加数据
User user = new User(1, "xiaohong", 12);
IndexRequest docAdd = new IndexRequest("x_index");
docAdd.id("1");
docAdd.timeout(TimeValue.timeValueSeconds(1));
docAdd.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(docAdd, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
// 文档查询数据
GetRequest getReq = new GetRequest("x_index", "1");
boolean exists = restHighLevelClient.exists(getReq, RequestOptions.DEFAULT);
if(exists) {
GetResponse documentFields = restHighLevelClient.get(getReq, RequestOptions.DEFAULT);
}
System.out.println(documentFields.getSourceAsString());
}
}