redis_search_springboot
第一步:使用Docker–compose创建redis–stach
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
docker-compose up -d
第二步: 创建一个SpringBoot项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
@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;
}
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吧
原文地址:https://blog.csdn.net/Benpeng_/article/details/128248992
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_44896.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。