本文共 5534 字,大约阅读时间需要 18 分钟。
在项目根目录的 pom.xml 中添加必要的依赖,确保能够正常使用 Elasticsearch。
org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-web cn.hutool hutool-core 5.7.13 org.projectlombok lombok org.springframework.boot spring-boot-starter-test junit junit
在 application.properties 中配置 Elasticsearch 的基础设置。
spring: elasticsearch: rest: uris: 127.0.0.1:9200 username: password: connection-timeout: 1000 read-timeout: 1000
使用 Spring Data Elasticsearch 注解来定义索引和字段映射。
text: 分词字段,支持聚合keyword: 不分词字段,支持聚合date: 日期类型boolean: 布尔值Nested: 嵌套对象Object: JSON 对象Array: 数组List: 列表import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import java.util.Date;import java.util.List;@Data@NoArgsConstructor@AllArgsConstructor@Document(indexName = "student", type = "_doc", replicas = 1, shards = 1, createIndex = true)public class Student { @Id @Field(index = true, store = true, type = FieldType.Keyword) private String sId; @Field(index = true, store = true, type = FieldType.Keyword) private String sName; @Field(index = true, store = true, type = FieldType.Text, analyzer = "ik_smart") private String sAddress; @Field(index = false, store = true, type = FieldType.Integer) private Integer sAge; @Field(index = false, store = true, type = FieldType.Date, format = DateFormat.basic_date_time) private Date sCreateTime; @Field(index = false, store = true, type = FieldType.Object) private Headmaster sHeadmaster; @Field(index = true, store = false, type = FieldType.Keyword) private String[] sCourseList; @Field(index = true, store = false, type = FieldType.Keyword) private List sColorList; @Field(index = true, store = false, type = FieldType.Nested) private List sTeacherList;} import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ElasticsearchApplication { public static void main(String[] args) { SpringApplication.run(ElasticsearchApplication.class, args); }} 通过 RestTemplate 创建索引及映射。
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;@Servicepublic class ElasticsearchSaveService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; public boolean createIndexAndMapping(Class classType) { return elasticsearchRestTemplate.indexExists(classType) && elasticsearchRestTemplate.createIndex(classType) && elasticsearchRestTemplate.putMapping(classType); }} 通过 RestTemplate 批量或单独添加文档。
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;@Servicepublic class ElasticsearchSaveService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; public Iterable save(Iterable entities) { return elasticsearchRestTemplate.save(entities); } public T save(T entity) { return elasticsearchRestTemplate.save(entity); }} 通过 RestTemplate 删除索引。
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;@Servicepublic class ElasticsearchDeleteService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; public boolean deleteIndex(Class clazz) { return elasticsearchRestTemplate.deleteIndex(clazz); }} 通过 RestTemplate 删除文档。
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;@Servicepublic class ElasticsearchDeleteService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; public Boolean delete(String id, Class entityType) { return elasticsearchRestTemplate.delete(id, entityType); } public Boolean delete(Object entity) { return elasticsearchRestTemplate.delete(entity); }} 通过 RestTemplate 执行复杂查询。
import org.springframework.data.elasticsearch.core.query.Query;import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;@Servicepublic class ElasticsearchQueryService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; public List search(Query query) { return elasticsearchRestTemplate.query(query, T.class); }} ik_smart 或 ik_max_word 分词器。通过以上配置和操作,可以实现对 Elasticsearch 的高效管理和文档存储。
转载地址:http://hqtfk.baihongyu.com/