【问题标题】:How can I search a redis hash table for entities that have a particular values using spring data?如何使用 spring 数据在 redis 哈希表中搜索具有特定值的实体?
【发布时间】:2020-06-28 23:12:39
【问题描述】:

我有一个 RedisHash 表,我在 spring 数据中建模,如下所示:

@RedisHash("Entity")
@Data
@AllArgsConstructor
public class Entity implements Serializable {
  private String id;
  private String status;
  private String name;
}

我有一个这样的 EntityRepository:

@Repository
public interface EntityRepository extends CrudRepository<Entity, String> {}

然后我有一个像这样的 EntityService:

@Service 
public class EntityService {
  @Autowired
  private EntityRepository entityRepository;

  public List<Entity> getAllByName(String name) {
    // Code that gets all Entities stored in my redis table that have a certain name
  }

  public List<Entity> getAllByStatus(String status) {
    // Code that gets all Entities stored in my redis table that have a certain status
  }

如何在 redis 中搜索所有具有特定名称/具有特定状态的实体?

【问题讨论】:

  • 你检查过 RediSearch 吗?
  • 不,我没用过。如果可能的话,我想在 redis 中执行此操作。
  • RediSearch 是 Redis 的模块扩展,允许您在 Redis 中索引/搜索哈希

标签: java redis spring-data spring-data-redis


【解决方案1】:

我遵循文档here 并能够解决我的问题。我将 QueryByExampleExecutor 接口添加到我的存储库中,如下所示:

@Repository
public interface EntityRepository extends CrudRepository<Entity, String>, QueryByExampleExecutor<Entity> {}

使用示例类,我实现了 getAllByName,如下所示:

public List<Entity> getAllByName(String name) {
    Entity entity = new Entity();
    entity.setName(name);

    Example<Entity> example = Example.of(entity);
    entityRepository.findAll(example);
    //...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 2016-06-27
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2015-07-21
    • 2019-04-23
    相关资源
    最近更新 更多