redis_search_springboot
第一步:使用Docker-compose创建redis-stach
- 创建一个 docker-compose.yml文件
version: "3.9"
services:
redis:
image: "redis/redis-stack:edge"
ports:
- "6379:6379"
environment:
- "REDIS_ARGS=--appendonly yes"
volumes:
- ./data:/data
deploy:
replicas: 1
restart_policy:
condition: on-failure
- 使用下面的脚本启动redis-stack
docker-compose up -d
- 使用redis-insight连接redis-stack
第二步: 创建一个SpringBoot项目
- springboot version
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/>
</parent>
- 引入redis依赖
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.redis.om</groupId>
<artifactId>redis-om-spring</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 创建 Country 和 Province 实体
@Document
@Data
public class Country {
@Id
@Indexed
private String id;
@Indexed
private String name;
@Indexed
private Province province;
@Indexed
private Set<String> flag;
}
@Data
public class Province {
@Indexed
private String proName;
@Indexed
private Integer area;
}
- 创建 country repository 和 country service, 并确保添加了mapper扫描路径
public interface CountryRepository extends RedisDocumentRepository<Country, String> {
Iterable<Country> findByProvince_ProName(String name);
Iterable<Country> findByFlag(Set<String> flag);
}
@Service
public class CountryService {
@Autowired
EntityStream entityStream;
public Iterable<Country> findAllCountry() {
return entityStream.of(Country.class)
.limit(10)
.collect(Collectors.toList());
}
public Iterable<Country> findByName(String name) {
return entityStream.of(Country.class)
.filter(Country$.NAME.eq(name))
.limit(10)
.collect(Collectors.toList());
}
}
@EnableRedisDocumentRepositories(basePackages = "com.pengo.redis.search.*")
@SpringBootApplication
public class RedisSearchSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(RedisSearchSpringbootApplication.class, args);
}
}
- 创建测试方法初始化数据
@SpringBootTest
class RedisSearchSpringbootApplicationTests {
@Autowired
private CountryRepository countryRepository;
@Autowired
private CountryService countryService;
@Test
void initCountry() {
countryRepository.deleteAll();
Country c1 = new Country();
Province p1 = new Province();
p1.setProName("shanghai");
p1.setArea(100);
c1.setName("china");
c1.setProvince(p1);
c1.setFlag(Set.of("a", "b", "c"));
Country c2 = new Country();
Province p2 = new Province();
p2.setProName("beijing");
p2.setArea(200);
c2.setName("china2");
c2.setProvince(p2);
c2.setFlag(Set.of("d", "e", "f"));
countryRepository.saveAll(List.of(c1, c2));
}
@Test
void testCountry() {
Iterable<Country> shanghai = countryRepository.findByProvince_ProName("shanghai");
System.out.println(shanghai);
System.out.println("------");
Iterable<Country> a = countryRepository.findByFlag(Set.of("a"));
System.out.println(a);
System.out.println("------");
Iterable<Country> allCountry = countryService.findAllCountry();
System.out.println(allCountry);
System.out.println("------");
Iterable<Country> china = countryService.findByName("china");
System.out.println(china);
}
}
- 你可以打开redis-insight,并且点击刷新按钮. Bingo~

最后,尽情享受Redis吧
- 运行方法,你会得到如下结果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sI9iF5Oh-1670553682962)(picture/result.png)]](https://i-blog.csdnimg.cn/blog_migrate/3c68420cb13b1a9bfedc1fbcf09ff9af.png)
源码地址:
https://github.com/WangBenpeng/redis_search_springboot