1.创建SpringBoot项目,pom文件如下:
<?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 https://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.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liu</groupId>
<artifactId>esapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>esapi</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.13.3</elasticsearch.version>
</properties>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.创建ES客户端配置类,注意使用@Bean时需要指定bean的名字,因为ES默认会自动初始化客户端对象,否则在使用@Autowired依赖注入时,会有多个同一类型的bean,导入注入失败。
@Configuration
public class ClientConfig {
@Bean("client")
public RestHighLevelClient getRestHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
);
return client;
}
}
3.创建实体类,在插入文档时需要
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private Integer age;
private String address;
}
4.索引和文档的CRUD单元测试
@SpringBootTest
@Slf4j
class EsapiApplicationTests {
@Autowired
private RestHighLevelClient client;
@Test
public void createIndex() throws Exception{
CreateIndexRequest createIndexRequest = new CreateIndexRequest("index_one");
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
log.info(createIndexResponse.index());
log.info(Boolean.toString(createIndexResponse.isAcknowledged()));
log.info(Boolean.toString(createIndexResponse.isShardsAcknowledged()));
}
@Test
public void getIndex() throws Exception{
GetIndexRequest getIndexRequest = new GetIndexRequest("index_one");
boolean flag = client.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
if(flag){
log.info("index_one索引库存在");
}else{
log.info("index_one索引库不存在");
}
}
@Test
public void deleteIndex() throws Exception{
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("index_one");
AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
if(acknowledgedResponse.isAcknowledged()){
log.info("删除成功");
}else{
log.info("删除失败");
}
}
@Test
public void createDocument() throws Exception{
User user = new User("张文文",20,"山西省太原市");
IndexRequest indexRequest = new IndexRequest("index_one");
indexRequest.id("2");
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest,RequestOptions.DEFAULT);
log.info("------对应kibana中的API请求返回结果");
log.info(indexResponse.getIndex());
log.info(indexResponse.getType());
log.info(indexResponse.getId());
log.info(String.valueOf(indexResponse.getVersion()));
log.info(indexResponse.getResult().getLowercase());
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
log.info(String.valueOf(shardInfo.getSuccessful()));
indexResponse.status();
}
@Test
public void getDocument() throws Exception{
GetRequest getRequest = new GetRequest("index_one","1");
if(client.exists(getRequest,RequestOptions.DEFAULT)){
GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
Map<String,Object> map = getResponse.getSource();
log.info(String.valueOf(map.get("name")));
log.info(String.valueOf(map.get("age")));
log.info(String.valueOf(map.get("address")));
}else{
log.info("当前文档不存在");
}
}
@Test
public void updateDocument() throws Exception{
UpdateRequest updateRequest = new UpdateRequest("index_one","1");
User user = new User();
user.setAge(10000);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);
log.info("更新结果:" + updateResponse.getResult().getLowercase());
}
@Test
public void deleteDocument() throws Exception{
DeleteRequest deleteRequest = new DeleteRequest("index_one","2");
DeleteResponse deleteResponse = client.delete(deleteRequest,RequestOptions.DEFAULT);
log.info("删除结果:" + deleteResponse.status().name());
log.info("删除结果:" + deleteResponse.getResult().getLowercase());
}
}
最后顺便给大家解释下索引和文档的含义: 1.索引:又叫索引库,相当于MySQL中的表 2.文档:相当于MySQL表中的数据 一个索引库中有多个文档,也就是相当于表中有多条数据, 文档中的每个字段可以称之为域(lucene中的叫法),域类型和域值
ElasticSearch的数据可视化客户端可以选择 ElasticSearch Head如下,非常清晰好用 
|