博客
关于我
SpringBoot之ElasticsearchRestTemplate常用示例
阅读量:795 次
发布时间:2023-03-02

本文共 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 的高效管理和文档存储。

转载地址:http://hqtfk.baihongyu.com/

你可能感兴趣的文章
PHP命名空间带来的干扰
查看>>
PHP和MySQL Web开发从新手到高手,第1天-搭建PHP开发环境
查看>>
php商店管理系统,基于PHP的商店管理系统.doc
查看>>
PHP四大主流框架的优缺点总结
查看>>
PHP图片处理—PNG透明缩放并生成灰图
查看>>
php在liunx系统中设置777权限不起作用解决方法
查看>>
PHP基于openssl实现的非对称加密操作
查看>>
php基本符号大全
查看>>
php基础篇-二维数组排序 array_multisort
查看>>
php基础配置环境变量
查看>>
php增删改查封装方法
查看>>
php多条件筛选功能的实现
查看>>
php多线程
查看>>
PHP大数组循环-避免产生Notice或者是Warning
查看>>
PHP大数组过滤元素、修改元素性能分析
查看>>
PHP大文件切片下载代码
查看>>
PHP如何下载远程文件到指定目录
查看>>
php如何做表格,新手怎么制作表格
查看>>
php如何定义的数位置,php如何实现不借助IDE快速定位行数或者方法定义的文件和位置...
查看>>
RabbitMQ集群 - 普通集群搭建、宕机情况
查看>>