当前位置:   article > 正文

SpringBoot整合Redis Search入门_springboot redissearch

springboot redissearch

redis_search_springboot

第一步:使用Docker-compose创建redis-stach

  1. 创建一个 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  1. 使用下面的脚本启动redis-stack
docker-compose up -d
  • 1
  1. 使用redis-insight连接redis-stack

第二步: 创建一个SpringBoot项目

  1. springboot version
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. 引入redis依赖
    <repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
        </repository>
    </repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
    <dependencies>
        <dependency>
            <groupId>com.redis.om</groupId>
            <artifactId>redis-om-spring</artifactId>
            <version>0.3.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
@Data
public class Province {

    @Indexed
    private String proName;

    @Indexed
    private Integer area;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  1. 创建 country repository 和 country service, 并确保添加了mapper扫描路径
public interface CountryRepository extends RedisDocumentRepository<Country, String> {

    Iterable<Country> findByProvince_ProName(String name);

    Iterable<Country> findByFlag(Set<String> flag);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
@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());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
@EnableRedisDocumentRepositories(basePackages = "com.pengo.redis.search.*")
@SpringBootApplication
public class RedisSearchSpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisSearchSpringbootApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  1. 你可以打开redis-insight,并且点击刷新按钮. Bingo~
    在这里插入图片描述

最后,尽情享受Redis吧

  1. 运行方法,你会得到如下结果
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sI9iF5Oh-1670553682962)(picture/result.png)]
    源码地址:
    https://github.com/WangBenpeng/redis_search_springboot
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/606649
推荐阅读
相关标签
  

闽ICP备14008679号