Skip to content

Commit 558c495

Browse files
author
YunaiV
committed
开始 Solr 入门示例
1 parent 11fd613 commit 558c495

File tree

9 files changed

+131
-13
lines changed

9 files changed

+131
-13
lines changed

README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,24 @@
6565

6666
## 数据访问
6767

68-
* [《芋道 Spring Boot Redis 入门》](http://www.iocoder.cn/Spring-Boot/Redis/?github) 对应 [lab-11](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-11)
69-
* [《芋道 Spring Boot 缓存 Cache 入门》](http://www.iocoder.cn/Spring-Boot/Cache/?github) 对应 [lab-21](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-21)
68+
**关系数据库**
69+
7070
* [《芋道 Spring Boot 数据库连接池入门》](http://www.iocoder.cn/Spring-Boot/datasource-pool/?github) 对应 [lab-19](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-19)
7171
* [《芋道 Spring Boot MyBatis 入门》](http://www.iocoder.cn/Spring-Boot/MyBatis/?github) 对应 [lab-12](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-12)
7272
* [《芋道 Spring Boot JPA 入门》](http://www.iocoder.cn/Spring-Boot/JPA/?github) 对应 [lab-13](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-13)
7373
* [《芋道 Spring Boot JdbcTemplate 入门》](http://www.iocoder.cn/Spring-Boot/JdbcTemplate/?github) 对应 [lab-14](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-14)
74-
* [《芋道 Spring Boot Elasticsearch 入门》](http://www.iocoder.cn/Spring-Boot/Elasticsearch/?github) 对应 [lab-15](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-15)
75-
* 《芋道 Spring Boot Solr 入门》计划中...
76-
* [《芋道 Spring Boot MongoDB 入门》](http://www.iocoder.cn/Spring-Boot/MongoDB/?github) 对应 [lab-16](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-16)
7774
* [《芋道 Spring Boot 多数据源(读写分离)入门》](http://www.iocoder.cn/Spring-Boot/dynamic-datasource/?github) 对应 [lab-17](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-17)
7875
* [《芋道 Spring Boot 分库分表入门》](http://www.iocoder.cn/Spring-Boot/sharding-datasource/?github) 对应 [lab-18](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-18)
7976
* [《芋道 Spring Boot 数据库版本管理入门》](http://www.iocoder.cn/Spring-Boot/database-version-control/?github) 对应 [lab-20](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-20)
8077

78+
**非关系数据库**
79+
80+
* [《芋道 Spring Boot Redis 入门》](http://www.iocoder.cn/Spring-Boot/Redis/?github) 对应 [lab-11](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-11)
81+
* [《芋道 Spring Boot 缓存 Cache 入门》](http://www.iocoder.cn/Spring-Boot/Cache/?github) 对应 [lab-21](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-21)
82+
* [《芋道 Spring Boot MongoDB 入门》](http://www.iocoder.cn/Spring-Boot/MongoDB/?github) 对应 [lab-16](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-16)
83+
* [《芋道 Spring Boot Elasticsearch 入门》](http://www.iocoder.cn/Spring-Boot/Elasticsearch/?github) 对应 [lab-15](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-15)
84+
* [《芋道 Spring Boot Solr 入门》](http://www.iocoder.cn/Spring-Boot/Solr/?github) 对应 [lab-66](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-66)
85+
8186
## 事务管理
8287

8388
* [《芋道 Spring Boot 分布式事务 Seata 入门》](http://www.iocoder.cn/Spring-Boot/Seata/?github) 对应 [lab-52](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-52)

lab-66/lab-66-spring-data-solr/src/main/java/cn/iocoder/springboot/lab15/springdatasolr/dataobject/SolrProductDO.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,28 @@ public class SolrProductDO {
1111
* ID 主键
1212
*/
1313
@Id
14+
@Indexed(name = "id", type = "int")
1415
private Integer id;
1516

1617
/**
1718
* SPU 名字
1819
*/
19-
@Indexed(value = "name")
20+
@Indexed(name = "name", type = "string")
2021
private String name;
2122
/**
2223
* 描述
2324
*/
24-
@Indexed(value = "description")
25+
@Indexed(name = "description", type = "string")
2526
private String description;
2627
/**
2728
* 分类编号
2829
*/
29-
@Indexed(value = "cid")
30+
@Indexed(name = "cid", type = "cid")
3031
private Integer cid;
3132
/**
3233
* 分类名
3334
*/
34-
@Indexed(value = "category_name")
35+
@Indexed(name = "category_name", type = "string")
3536
private String categoryName;
3637

3738
public Integer getId() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.iocoder.springboot.lab15.springdatasolr.repository;
2+
3+
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
4+
import org.springframework.data.solr.repository.SolrCrudRepository;
5+
6+
public interface ProductRepository02 extends SolrCrudRepository<SolrProductDO, Integer> {
7+
8+
SolrProductDO findByName(String name);
9+
10+
// Page<SolrProductDO> findByNameLike(String name, Pageable pageable);
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.lab15.springdatasolr.repository;
2+
3+
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
4+
import org.springframework.data.solr.repository.Query;
5+
import org.springframework.data.solr.repository.SolrCrudRepository;
6+
7+
import java.util.List;
8+
9+
public interface ProductRepository03 extends SolrCrudRepository<SolrProductDO, Integer> {
10+
11+
/**
12+
* 根据 name 匹配商品名或者商品分类,获得符合的商品列表
13+
*/
14+
@Query("name:?0 OR category_name:?0")
15+
List<SolrProductDO> findByCustomQuery(String name);
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
spring:
22
data:
3+
# Spring Data Solr 配置项,对应 SolrProperties 配置类
34
solr:
4-
host: 'http://127.0.0.1:8983/solr'
5-
repositories:
6-
enabled: true
5+
host: 'http://127.0.0.1:8983/solr' # Solr 服务器地址
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cn.iocoder.springboot.lab15.springdatasolr.repository;
2+
3+
import cn.iocoder.springboot.lab15.springdatasolr.Application;
4+
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
@RunWith(SpringRunner.class)
12+
@SpringBootTest(classes = Application.class)
13+
public class ProductRepository02Test {
14+
15+
@Autowired
16+
private ProductRepository02 productRepository;
17+
18+
@Test // 根据名字获得一条记录
19+
public void testFindByName() {
20+
SolrProductDO product = productRepository.findByName("芋道源码");
21+
System.out.println(product);
22+
}
23+
24+
// @Test // 使用 name 模糊查询,分页返回结果
25+
// public void testFindByNameLike() {
26+
// // 根据情况,是否要制造测试数据
27+
// if (false) {
28+
// testInsert();
29+
// }
30+
//
31+
// // 创建排序条件
32+
// Sort sort = Sort.by(Sort.Direction.DESC, "id"); // ID 倒序
33+
// // 创建分页条件。
34+
// Pageable pageable = PageRequest.of(0, 10, sort);
35+
// // 执行分页操作
36+
// Page<SolrProductDO> page = productRepository.findByNameLike("芋道", pageable);
37+
// // 打印
38+
// System.out.println(page.getTotalElements());
39+
// System.out.println(page.getTotalPages());
40+
// }
41+
//
42+
// /**
43+
// * 为了给分页制造一点数据
44+
// */
45+
// private void testInsert() {
46+
// for (int i = 1; i <= 100; i++) {
47+
// SolrProductDO product = new SolrProductDO();
48+
// product.setId(i); // 一般 ES 的 ID 编号,使用 DB 数据对应的编号。这里,先写死
49+
// product.setName("芋道源码:" + i);
50+
// product.setDescription("我只是一个描述");
51+
// product.setCid(1);
52+
// product.setCategoryName("技术");
53+
// productRepository.save(product);
54+
// }
55+
// }
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.iocoder.springboot.lab15.springdatasolr.repository;
2+
3+
import cn.iocoder.springboot.lab15.springdatasolr.Application;
4+
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import java.util.List;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest(classes = Application.class)
15+
public class ProductRepository03Test {
16+
17+
@Autowired
18+
private ProductRepository03 productRepository;
19+
20+
@Test
21+
public void testFindByCustomQuery() {
22+
List<SolrProductDO> products = productRepository.findByCustomQuery("技术");
23+
System.out.println(products.size());
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<http://www.iocoder.cn/Spring-Boot/Solr/?github>

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<!-- <module>lab-63</module>-->
7777
<!-- <module>lab-64</module>-->
7878
<!-- <module>lab-65</module>-->
79+
<!-- <module>lab-66</module>-->
7980

8081
<!-- Spring Cloud 示例 -->
8182
<!-- <module>labx-01</module>-->
@@ -109,7 +110,6 @@
109110
<!-- <module>labx-28</module>-->
110111
<!-- <module>labx-29</module>-->
111112
<!-- <module>labx-30</module>-->
112-
<module>lab-66</module>
113113
</modules>
114114

115115
</project>

0 commit comments

Comments
 (0)