开始 Solr 入门示例

This commit is contained in:
YunaiV
2020-06-18 19:16:20 +08:00
parent 11fd613579
commit 558c49526d
9 changed files with 131 additions and 13 deletions

View File

@@ -65,19 +65,24 @@
## 数据访问
* [《芋道 Spring Boot Redis 入门》](http://www.iocoder.cn/Spring-Boot/Redis/?github) 对应 [lab-11](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-11)
* [《芋道 Spring Boot 缓存 Cache 入门》](http://www.iocoder.cn/Spring-Boot/Cache/?github) 对应 [lab-21](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-21)
**关系数据库**
* [《芋道 Spring Boot 数据库连接池入门》](http://www.iocoder.cn/Spring-Boot/datasource-pool/?github) 对应 [lab-19](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-19)
* [《芋道 Spring Boot MyBatis 入门》](http://www.iocoder.cn/Spring-Boot/MyBatis/?github) 对应 [lab-12](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-12)
* [《芋道 Spring Boot JPA 入门》](http://www.iocoder.cn/Spring-Boot/JPA/?github) 对应 [lab-13](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-13)
* [《芋道 Spring Boot JdbcTemplate 入门》](http://www.iocoder.cn/Spring-Boot/JdbcTemplate/?github) 对应 [lab-14](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-14)
* [《芋道 Spring Boot Elasticsearch 入门》](http://www.iocoder.cn/Spring-Boot/Elasticsearch/?github) 对应 [lab-15](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-15)
* 《芋道 Spring Boot Solr 入门》计划中...
* [《芋道 Spring Boot MongoDB 入门》](http://www.iocoder.cn/Spring-Boot/MongoDB/?github) 对应 [lab-16](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-16)
* [《芋道 Spring Boot 多数据源(读写分离)入门》](http://www.iocoder.cn/Spring-Boot/dynamic-datasource/?github) 对应 [lab-17](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-17)
* [《芋道 Spring Boot 分库分表入门》](http://www.iocoder.cn/Spring-Boot/sharding-datasource/?github) 对应 [lab-18](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-18)
* [《芋道 Spring Boot 数据库版本管理入门》](http://www.iocoder.cn/Spring-Boot/database-version-control/?github) 对应 [lab-20](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-20)
**非关系数据库**
* [《芋道 Spring Boot Redis 入门》](http://www.iocoder.cn/Spring-Boot/Redis/?github) 对应 [lab-11](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-11)
* [《芋道 Spring Boot 缓存 Cache 入门》](http://www.iocoder.cn/Spring-Boot/Cache/?github) 对应 [lab-21](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-21)
* [《芋道 Spring Boot MongoDB 入门》](http://www.iocoder.cn/Spring-Boot/MongoDB/?github) 对应 [lab-16](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-16)
* [《芋道 Spring Boot Elasticsearch 入门》](http://www.iocoder.cn/Spring-Boot/Elasticsearch/?github) 对应 [lab-15](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-15)
* [《芋道 Spring Boot Solr 入门》](http://www.iocoder.cn/Spring-Boot/Solr/?github) 对应 [lab-66](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-66)
## 事务管理
* [《芋道 Spring Boot 分布式事务 Seata 入门》](http://www.iocoder.cn/Spring-Boot/Seata/?github) 对应 [lab-52](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-52)

View File

@@ -11,27 +11,28 @@ public class SolrProductDO {
* ID 主键
*/
@Id
@Indexed(name = "id", type = "int")
private Integer id;
/**
* SPU 名字
*/
@Indexed(value = "name")
@Indexed(name = "name", type = "string")
private String name;
/**
* 描述
*/
@Indexed(value = "description")
@Indexed(name = "description", type = "string")
private String description;
/**
* 分类编号
*/
@Indexed(value = "cid")
@Indexed(name = "cid", type = "cid")
private Integer cid;
/**
* 分类名
*/
@Indexed(value = "category_name")
@Indexed(name = "category_name", type = "string")
private String categoryName;
public Integer getId() {

View File

@@ -0,0 +1,12 @@
package cn.iocoder.springboot.lab15.springdatasolr.repository;
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
import org.springframework.data.solr.repository.SolrCrudRepository;
public interface ProductRepository02 extends SolrCrudRepository<SolrProductDO, Integer> {
SolrProductDO findByName(String name);
// Page<SolrProductDO> findByNameLike(String name, Pageable pageable);
}

View File

@@ -0,0 +1,17 @@
package cn.iocoder.springboot.lab15.springdatasolr.repository;
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;
import java.util.List;
public interface ProductRepository03 extends SolrCrudRepository<SolrProductDO, Integer> {
/**
* 根据 name 匹配商品名或者商品分类,获得符合的商品列表
*/
@Query("name:?0 OR category_name:?0")
List<SolrProductDO> findByCustomQuery(String name);
}

View File

@@ -1,6 +1,5 @@
spring:
data:
# Spring Data Solr 配置项,对应 SolrProperties 配置类
solr:
host: 'http://127.0.0.1:8983/solr'
repositories:
enabled: true
host: 'http://127.0.0.1:8983/solr' # Solr 服务器地址

View File

@@ -0,0 +1,57 @@
package cn.iocoder.springboot.lab15.springdatasolr.repository;
import cn.iocoder.springboot.lab15.springdatasolr.Application;
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
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.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ProductRepository02Test {
@Autowired
private ProductRepository02 productRepository;
@Test // 根据名字获得一条记录
public void testFindByName() {
SolrProductDO product = productRepository.findByName("芋道源码");
System.out.println(product);
}
// @Test // 使用 name 模糊查询,分页返回结果
// public void testFindByNameLike() {
// // 根据情况,是否要制造测试数据
// if (false) {
// testInsert();
// }
//
// // 创建排序条件
// Sort sort = Sort.by(Sort.Direction.DESC, "id"); // ID 倒序
// // 创建分页条件。
// Pageable pageable = PageRequest.of(0, 10, sort);
// // 执行分页操作
// Page<SolrProductDO> page = productRepository.findByNameLike("芋道", pageable);
// // 打印
// System.out.println(page.getTotalElements());
// System.out.println(page.getTotalPages());
// }
//
// /**
// * 为了给分页制造一点数据
// */
// private void testInsert() {
// for (int i = 1; i <= 100; i++) {
// SolrProductDO product = new SolrProductDO();
// product.setId(i); // 一般 ES 的 ID 编号,使用 DB 数据对应的编号。这里,先写死
// product.setName("芋道源码:" + i);
// product.setDescription("我只是一个描述");
// product.setCid(1);
// product.setCategoryName("技术");
// productRepository.save(product);
// }
// }
}

View File

@@ -0,0 +1,26 @@
package cn.iocoder.springboot.lab15.springdatasolr.repository;
import cn.iocoder.springboot.lab15.springdatasolr.Application;
import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO;
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.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ProductRepository03Test {
@Autowired
private ProductRepository03 productRepository;
@Test
public void testFindByCustomQuery() {
List<SolrProductDO> products = productRepository.findByCustomQuery("技术");
System.out.println(products.size());
}
}

View File

@@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Solr/?github>

View File

@@ -76,6 +76,7 @@
<!-- <module>lab-63</module>-->
<!-- <module>lab-64</module>-->
<!-- <module>lab-65</module>-->
<!-- <module>lab-66</module>-->
<!-- Spring Cloud 示例 -->
<!-- <module>labx-01</module>-->
@@ -109,7 +110,6 @@
<!-- <module>labx-28</module>-->
<!-- <module>labx-29</module>-->
<!-- <module>labx-30</module>-->
<module>lab-66</module>
</modules>
</project>