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;@SpringBootApplicationpublic 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;@Servicepublic 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;@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); }} 6. ElasticsearchRestTemplate 删除
6.1 deleteIndex
通过 RestTemplate 删除索引。
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;@Servicepublic 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;@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); }} 7. 搜索功能
通过 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); }} 8. 搜索优化
- 分词器设置: 使用
ik_smart或ik_max_word分词器。 - 日期格式: 建议使用标准日期格式。
- 聚合操作: 合理使用聚合操作,避免过度聚合。
通过以上配置和操作,可以实现对 Elasticsearch 的高效管理和文档存储。
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2026年06月18日 20时13分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP正则表达式获取武汉市的实时pm2.5数据并邮件发送phpmailer
2023-03-01
RabbitMQ + JMeter组合,优化你的中间件处理方式!
2023-03-01
PHP水仙花问题解法之一
2023-03-01
php没有解析是怎么回事,linux下php文件没有被剖析怎么办?_后端开发
2023-03-01
php注册页面实现注册后跳转页面
2023-03-01
PHP消息队列的实现方式与详解,值得一看
2023-03-01
PHP混合Go协程并发
2023-03-01
php源码中如何添加滚动公告,给WordPress网站添加滚动公告的方法
2023-03-01
PHP源码安装后如何新增模块
2023-03-01
php源码详细安装步骤,linux下php源码安装步骤
2023-03-01
php漏洞tips
2023-03-01
php版Zencoding之 phpstorm
2023-03-01
PHP版本升级5.4手记
2023-03-01
php版本升级总结
2023-03-01
php版本微信公众号开发
2023-03-01
php版的微信公众号开发演示
2023-03-01
php生成html文件的多种方法介绍
2023-03-01
php生成二维码到图片上
2023-03-01
php生成二维码并下载图片(适应于框架)
2023-03-01
PHP生成及获取JSON文件的方法
2023-03-01