系统中需要使用到ElasticSearch进行内容检索,因此需要搭建SpringBoot + ElasticSearch的环境。
1、pom.xml文件中的引用
<?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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<elastic-client.version>7.17.6</elastic-client.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 注意:此方式已过期,会报错-->
<!-- <dependency>-->
<!-- <groupId>org.elasticsearch.client</groupId>-->
<!-- <artifactId>elasticsearch-rest-high-level-client</artifactId>-->
<!-- <version>${elasticsearch.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<!-- elasticsearch.version = 7.17.6-->
</dependency>
<!-- 一定要添加此引用,否则会报错 -->
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 用于快速创建Model -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2、修改application.yml
# .....
spring:
elasticsearch:
uris: http://<<your-elasticsearch-ip-address>>:<<your-port, default is 9200>>
password: .....
username: user
# ....
3、修改Application.java
@SpringBootApplication
@Configuration
public class HeatSearchEngineApplication {
public static void main(String[] args) {
SpringApplication.run(HeatSearchEngineApplication.class, args);
}
@Bean("restClient")
public ElasticsearchClient elasticsearchClient() {
RestClient httpClient = RestClient.builder(new HttpHost("192.168.193.66", 9200)).build();
// 不引用jakarta.json-api时,此行会报错
ElasticsearchTransport transport = new RestClientTransport(httpClient, new JacksonJsonpMapper());
ElasticsearchClient esClient = new ElasticsearchClient(transport);
return esClient;
}
}
4、创建File的Model
package com.xxxx.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@Document(indexName = "all-files")
@JsonIgnoreProperties(ignoreUnknown = true) // Model中需要添加此行代码,否则检索时可能会报错
public class File {
@Id
private Long id;
@Field(type = FieldType.Text)
private String name;
@Field(type = FieldType.Text)
private String nameEn;
@Override
public String toString() {
return "File {" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", nameEn='" + nameEn + '\'' +
'}';
}
}
5、创建FileRepository
package com.xxxxx.dao;
import com.grid.heat.search.model.File;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FileRepository extends ElasticsearchRepository<File, Long> {
}
6、编写一个API用于测试
@RestController()
@RequestMapping("/test")
public class TestController {
@Autowired
public RestHighLevelClient restClient;
@Autowired
public FileRepository fileRepository;
@GetMapping("/add")
public boolean add() {
File file = new File();
file.setId(UuidUtil.getId());
file.setName("aaaaat");
file.setNameEn("test1");
fileRepository.save(file);
return true;
}
// 请自行编写其它测试方法
}
附、UuidUtil工具类的实现如下:
public class UuidUtil {
private static AtomicLong id;
/**
* 生成Long 类型唯一ID
*/
public synchronized static Long getId() {
//如果需要更长 或者更大冗余空间, 只需要 time * 10^n 即可
//当前可保证1毫秒 生成 10000条不重复
Long time = Long.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()))*10000;
if (id == null) {
id = new AtomicLong(time);
return id.get();
}
if (time <= id.get()) {
id.addAndGet(1);
} else {
id = new AtomicLong(time);
}
return id.get();
}
}