SpringBoot之ElasticsearchRestTemplate常用示例
发布日期:2025-05-05 08:37:29 浏览次数:4 分类:精选文章

本文共 5534 字,大约阅读时间需要 18 分钟。

Elasticsearch 应用开发指南

1. 引入 POM 依赖

在项目根目录的 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

2. 应用配置

在 application.properties 中配置 Elasticsearch 的基础设置。

spring:
elasticsearch:
rest:
uris: 127.0.0.1:9200
username:
password:
connection-timeout: 1000
read-timeout: 1000

3. JavaBean 配置及 ES 注解

使用 Spring Data Elasticsearch 注解来定义索引和字段映射。

常用数据类型

  • 简单类型:
    • text: 分词字段,支持聚合
    • keyword: 不分词字段,支持聚合
    • date: 日期类型
    • boolean: 布尔值
  • 复杂类型:
    • Nested: 嵌套对象
    • Object: JSON 对象
    • Array: 数组
    • List: 列表

Student 实体类

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;
}

4. 启动类配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class, args);
}
}

5. ElasticsearchRestTemplate 新增

5.1 createIndex & putMapping

通过 RestTemplate 创建索引及映射。

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Service
public class ElasticsearchSaveService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
public boolean createIndexAndMapping(Class
classType) {
return elasticsearchRestTemplate.indexExists(classType)
&& elasticsearchRestTemplate.createIndex(classType)
&& elasticsearchRestTemplate.putMapping(classType);
}
}

5.2 save 添加文档

通过 RestTemplate 批量或单独添加文档。

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Service
public class ElasticsearchSaveService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
public Iterable
save(Iterable
entities) {
return elasticsearchRestTemplate.save(entities);
}
public T save(T entity) {
return elasticsearchRestTemplate.save(entity);
}
}

6. ElasticsearchRestTemplate 删除

6.1 deleteIndex

通过 RestTemplate 删除索引。

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Service
public class ElasticsearchDeleteService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
public boolean deleteIndex(Class
clazz) {
return elasticsearchRestTemplate.deleteIndex(clazz);
}
}

6.2 delete 删除文档

通过 RestTemplate 删除文档。

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Service
public 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);
}
}

7. 搜索功能

通过 RestTemplate 执行复杂查询。

import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Service
public class ElasticsearchQueryService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
public List
search(Query query) {
return elasticsearchRestTemplate.query(query, T.class);
}
}

8. 搜索优化

  • 分词器设置: 使用 ik_smartik_max_word 分词器。
  • 日期格式: 建议使用标准日期格式。
  • 聚合操作: 合理使用聚合操作,避免过度聚合。

通过以上配置和操作,可以实现对 Elasticsearch 的高效管理和文档存储。

上一篇:ping 全网段CMD命令
下一篇:Pillow lacks the JPEG 2000 plugin

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2026年06月18日 20时13分46秒