redis_search_springboot

一步使用Dockercompose创建redisstach

  1. 创建一个 dockercompose.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
  1. 使用下面的脚本启动redis-stack
docker-compose up -d
  1. 使用redis-insight连接redis-stack

第二步: 创建一个SpringBoot项目

  1. springboot version
    <parent&gt;
    <groupId&gt;org.springframework.boot</groupId&gt;
    <artifactId&gt;spring-boot-starter-parent</artifactId&gt;
    <version&gt;2.6.4</version&gt;
    <relativePath/&gt; <!-- lookup parent from repository -->
    </parent> 
  1. 引入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>
  1. 创建 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;

}
  1. 创建 country repositorycountry 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);
    }

}
  1. 创建测试方法初始化数据
@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);
    }
}
  1. 可以打开redis-insight,并且点击刷新按钮. Bingo~
    在这里插入图片描述

最后,尽情享受Redis吧

  1. 运行方法,你会得到如下结果
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sI9iF5Oh-1670553682962)(picture/result.png)]
    源码地址
    https://github.com/WangBenpeng/redis_search_springboot

原文地址:https://blog.csdn.net/Benpeng_/article/details/128248992

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_44896.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注