SpringData ElasticSearch 整合Elastic Search
1. Spring Data ElasticSearch 介绍
Spring Data Elastic Search 基于Spring data API 简化ElasticSearch操作,将原始操作Elastic Search的客户端API进行封装。Srping Data为Elastic Search项目提供集成搜索引擎。Spring Data Elastic Search POJO的关键功能区域为中心的模型与Elastic Search交互文档和轻松地编写一个存储索引库数据访问层。
2. Spring Data Elastic Search版本比较
3. 环境搭建
3.1 引入依赖
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.6.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
3.2 application.yml增加配置文件
elasticsearch:
host: 127.0.0.1
port: 9200
3.3 ES配置类
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
public class ElasticsearchConfiguration extends AbstractElasticsearchConfiguration {
@Override
public RestHighLevelClient elasticsearchClient() {
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
return restHighLevelClient;
}
}
4. ES 索引的操作
4.1 索引创建
@Test
public void createIndex(){
//创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
}
4.2 删除索引
@Test
public void deleteIndex(){
boolean delResult = restTemplate.deleteIndex(Product.class);
log.info("删除索引状态:{}",delResult);
}
5. 文档的基本操作
引入ProductDao配置
@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}
5.1 新增文档
@Test
public void testSave(){
Product product =new Product();
product.setId(2L);
product.setTitle("华为手机");
product.setCategory("手机");
product.setPrice(2999.0);
product.setImages("http://www.atguigu/hw.jpg");
productDao.save(product);
}
5.2 修改文档
@Test
public void testUpdate(){
Product product = new Product();
product.setId(2L);
product.setTitle("小米 2 手机");
product.setCategory("手机");
product.setPrice(9999.0);
product.setImages("http://www.atguigu/xm.jpg");
productDao.save(product);
}
5.3 根据Id查找文档
@Test
public void testFindById(){
Product product = productDao.findById(2L).get();
System.out.println(product);
}
5.4 查询所有文档
@Test
public void testFindAll(){
Iterable<Product> products = productDao.findAll();
for (Product product : products) {
System.out.println(product);
}
}
5.5 删除文档
@Test
public void testDelete(){
Product product = new Product();
product.setId(2L);
productDao.delete(product);
}
5.6 批量新增文档
@Test
public void testBatchSave(){
List<Product> productList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Product product = new Product();
product.setId(Long.valueOf(i));
product.setTitle("["+i+"]小米手机");
product.setCategory("手机");
product.setPrice(1999.0+i);
product.setImages("http://www.atguigu/xm.jpg");
productList.add(product);
}
productDao.saveAll(productList);
}
5.7 批量删除
@Test
public void testBatchDelete(){
List<Product> productList = new ArrayList<>();
for(int i=0;i<10;i++){
Product product =new Product();
product.setId(Long.valueOf(i));
productList.add(product);
}
productDao.deleteAll(productList);
}
5.8 分页查询
@Test
public void findByPageable(){
//设置排序(排序方式,正序还是倒序,排序的 id)
Sort sort = Sort.by(Sort.Direction.ASC,"id");
int currentPage=0;//当前页,第一页从0开始,1表示第二页
int pageSize = 5;//每页显示多少条
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort); //分页查询
Page<Product> productPage = productDao.findAll(pageRequest);
for (Product Product : productPage.getContent()) {
System.out.println(Product);
}
}
5.9 文档搜索
@Test
public void termQuery(){
// TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", " 手机");
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category.keyword", "手机");
Iterable<Product> products = productDao.search(termQueryBuilder);
for (Product product : products) {
System.out.println(product);
}
}
在使用termQuery的时候,发现QueryBuilders.termQuery("fieldName","vallue") 并没有搜索输出结果。后改成Field.keyword,可以正常搜索出来结果。原因见: es之termQuery精确查询失效问题解决
5.10 分页+搜索文档
@Test
public void termQueryByPage(){
int currentPage= 0 ;
int pageSize = 5;
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category.keyword", "手机");
Iterable<Product> products = productDao.search(termQueryBuilder,pageRequest);
for (Product product : products) {
System.out.println(product);
}
}