赞
踩
Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的
开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持 map-reduce 框架和云计
算数据服务。 Spring Data 可以极大的简化 JPA(Elasticsearch„)的写法,可以在几乎不用
写实现的情况下,实现对数据的访问和操作。除了 CRUD 外,还包括如分页、排序等一些
常用的功能。
Spring Data 的官网: https://spring.io/projects/spring-data
Spring Data 常用的功能模块如下:
Spring Data Elasticsearch 基于 spring data API 简化 Elasticsearch 操作,将原始操作
Elasticsearch 的客户端 API 进行封装 。 Spring Data 为 Elasticsearch 项目提供集成搜索引擎。
Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻
松地编写一个存储索引库数据访问层。
官方网站: https://spring.io/projects/spring-data-elasticsearch
Spring boot2.3.x 一般可以兼容 Elasticsearch7.x
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> </dependencies>
在 resources 目录中增加 application.properties 文件
server.port=8080
# es 服务地址
elasticsearch.host=127.0.0.1
# es 服务端口
elasticsearch.port=9200
@SpringBootApplication
public class EsJestApplication {
public static void main(String[] args) {
SpringApplication.run(EsJestApplication.class, args);
}
}
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; /** * @author Dongguo * @date 2021/10/30 0030-16:53 * @description: */ @Data @NoArgsConstructor @AllArgsConstructor @ToString public class Product { private Long id;//商品唯一标识 private String title;//商品名称 private String category;//分类名称 private Double price;//商品价格 private String images;//图片地址 }
ElasticsearchRestTemplate 是 spring-data-elasticsearch 项目中的一个类,和其他 spring 项目中的 template
类似。
在新版的 spring-data-elasticsearch 中, ElasticsearchRestTemplate 代替了原来的 ElasticsearchTemplate。
原因是 ElasticsearchTemplate 基于 TransportClient, TransportClient 即将在 8.x 以后的版本中移除。所
以,我们推荐使用 ElasticsearchRestTemplate。
ElasticsearchRestTemplate 基 于 RestHighLevelClient 客 户 端 的 。 需 要 自 定 义 配 置 类 , 继 承
AbstractElasticsearchConfiguration,并实现 elasticsearchClient()抽象方法,创建 RestHighLevelClient 对
象。
import lombok.Data; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; /** * @author Dongguo * @date 2021/10/30 0030-16:58 * @description: */ @ConfigurationProperties(prefix = "elasticsearch") @Configuration @Data public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { private String host ; private Integer port ; //重写父类方法 @Override public RestHighLevelClient elasticsearchClient() { RestClientBuilder builder = RestClient.builder(new HttpHost(host, port)); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder); return restHighLevelClient; } }
import com.es.springdataes.pojo.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
/**
* @author Dongguo
* @date 2021/10/30 0030-17:27
* @description:
*/
@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; /** * @author Dongguo * @date 2021/10/30 0030-16:53 * @description: */ @Data @NoArgsConstructor @AllArgsConstructor @ToString @Document(indexName = "product", shards = 3, replicas = 1) public class Product { //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id" @Id private Long id;//商品唯一标识 /** * type : 字段数据类型 * analyzer : 分词器类型 * index : 是否索引(默认:true) * Keyword : 短语,不进行分词 */ @Field(type = FieldType.Text, analyzer = "ik_max_word") private String title;//商品名称 @Field(type = FieldType.Keyword) private String category;//分类名称 @Field(type = FieldType.Double) private Double price;//商品价格 @Field(type = FieldType.Keyword, index = false) private String images;//图片地址 }
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.test.context.junit4.SpringRunner; /** * @author Dongguo * @date 2021/10/30 0030-17:31 * @description: */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESIndexTest { //注入 ElasticsearchRestTemplate @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; }
先查看当前有哪些索引
GET _cat/indices?v
返回响应
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana 9XivAxMzR3mS7PjJmD9SPA 1 1 1 0 4kb 4kb
yellow open my_index sgoDsJkzSxOXh0dEqz2FEw 1 1 0 0 208b 208b
yellow open shopping Tcd3un3PSUyeBnqdO-ZAxQ 1 1 1 0 5.2kb 5.2kb
//创建索引并增加映射配置
//启动时判断没有product索引,会自动创建
@Test
public void createIndex(){
//创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
}
启动
查看kibana
GET _cat/indices?v
返回响应
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open product IBQV4_ctSR6rMBdftIKAeQ 3 1 0 0 624b 624b
yellow open .kibana 9XivAxMzR3mS7PjJmD9SPA 1 1 18 0 41.3kb 41.3kb
yellow open my_index sgoDsJkzSxOXh0dEqz2FEw 1 1 0 0 208b 208b
yellow open shopping Tcd3un3PSUyeBnqdO-ZAxQ 1 1 1 0 5.2kb 5.2kb
product索引已经自动创建了
@Test
public void deleteIndex(){
//创建索引,系统初始化会自动创建索引
boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);
System.out.println("删除索引 = " + flg);
}
运行
使用kibana查看
GET _cat/indices?v
返回响应
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana 9XivAxMzR3mS7PjJmD9SPA 1 1 20 1 55.2kb 55.2kb
yellow open my_index sgoDsJkzSxOXh0dEqz2FEw 1 1 0 0 208b 208b
yellow open shopping Tcd3un3PSUyeBnqdO-ZAxQ 1 1 1 0 5.2kb 5.2kb
product索引已经被删除。
package com.es.esjest.springdataes.test; import com.es.esjest.springdataes.dao.ProductDao; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * @author Dongguo * @date 2021/10/30 0030-18:07 * @description: */ @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESProductDaoTest { @Autowired private ProductDao productDao; }
/**
* 新增
*/
@Test
public void save(){
Product product = new Product();
product.setId(2L);
product.setTitle("华为手机");
product.setCategory("手机");
product.setPrice(2999.0);
product.setImages("http://www.dongguo/hw.jpg");
productDao.save(product);
}
运行
kibana查看
GET product/_doc/2
返回响应
{ "_index" : "product", "_type" : "_doc", "_id" : "2", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "id" : 2, "title" : "华为手机", "category" : "手机", "price" : 2999.0, "images" : "http://www.dongguo/hw.jpg" } }
再创建一个
/**
* 新增
*/
@Test
public void save(){
Product product = new Product();
product.setId(1L);
product.setTitle("小米 2 手机");
product.setCategory("手机");
product.setPrice(9999.0);
product.setImages("http://www.dongguo/xm.jpg");
productDao.save(product);
}
/**
* 修改
*/
@Test
public void update(){
Product product = new Product();
product.setId(2L);
product.setTitle("华为荣耀");
product.setCategory("手机");
product.setPrice(4999.0);
product.setImages("http://www.dongguo/hw.jpg");
productDao.save(product);
}
运行
/**
* 根据 id 查询
*/
@Test
public void findById(){
Product product = productDao.findById(1L).get();
System.out.println(product);
}
运行
/**
* 查询所有
*/
@Test
public void findAll(){
Iterable<Product> products = productDao.findAll();
for (Product product : products) {
System.out.println(product);
}
}
启动
/**
* 删除
*/
@Test
public void delete(){
Product product = new Product();
product.setId(1L);
productDao.delete(product);
}
启动
在kibana中查看,数据已经不存在了
将id为2的也删除掉
@Test
public void delete(){
Product product = new Product();
product.setId(2L);
productDao.delete(product);
}
/** * 批量新增 */ @Test public void saveAll(){ 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.dongguo/xm.jpg"); productList.add(product); } productDao.saveAll(productList); }
启动
查看kibana
GET product/_search
{
"query": {
"match_all": {}
}
}
返回响应
{ "took" : 97, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 10, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "product", "_type" : "product", "_id" : "5", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 5, "title" : "[5]小米手机", "category" : "手机", "price" : 2004.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "7", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 7, "title" : "[7]小米手机", "category" : "手机", "price" : 2006.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "0", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 0, "title" : "[0]小米手机", "category" : "手机", "price" : 1999.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "2", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 2, "title" : "[2]小米手机", "category" : "手机", "price" : 2001.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "3", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 3, "title" : "[3]小米手机", "category" : "手机", "price" : 2002.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "4", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 4, "title" : "[4]小米手机", "category" : "手机", "price" : 2003.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "1", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 1, "title" : "[1]小米手机", "category" : "手机", "price" : 2000.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "6", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 6, "title" : "[6]小米手机", "category" : "手机", "price" : 2005.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "8", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 8, "title" : "[8]小米手机", "category" : "手机", "price" : 2007.0, "images" : "http://www.dongguo/xm.jpg" } }, { "_index" : "product", "_type" : "product", "_id" : "9", "_score" : 1.0, "_source" : { "_class" : "com.es.esjest.springdataes.pojo.Product", "id" : 9, "title" : "[9]小米手机", "category" : "手机", "price" : 2008.0, "images" : "http://www.dongguo/xm.jpg" } } ] } }
也可以执行查询所有
/** * 分页查询 */ @Test public void findByPageable(){ //设置排序(排序方式,正序还是倒序,排序的 id) Sort sort = Sort.by(Sort.Direction.DESC,"id"); //当前页,第一页从 0 开始, 1 表示第二页 int from=0; //每页显示多少条 int size = 5; //设置查询分页 PageRequest pageRequest = PageRequest.of(from, size,sort); //分页查询 Page<Product> productPage = productDao.findAll(pageRequest); for (Product Product : productPage.getContent()) { System.out.println(Product); } }
运行
package com.es.esjest.springdataes.test; import com.es.esjest.springdataes.dao.ProductDao; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.PageRequest; import org.springframework.test.context.junit4.SpringRunner; /** * @author Dongguo * @date 2021/10/30 0030-21:36 * @description: */ @RunWith(SpringRunner.class) public class SpringDataESSearchTest { @Autowired private ProductDao productDao; }
/**
* term 查询
* search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
*/
@Test
public void termQuery() {
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
Iterable<Product> products = productDao.search(termQueryBuilder);
for (Product product : products) {
System.out.println(product);
}
}
运行
/**
* term 查询加分页
*/
@Test
public void termQueryByPage() {
int currentPage = 0;
int pageSize = 5;
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
Iterable<Product> products = productDao.search(termQueryBuilder, pageRequest);
for (Product product : products) {
System.out.println(product);
}
}
运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。