Spring Data Redis(Repositories-Secondary Indexes)

Secondary Indexes
二级索引

二级索引用来使得查询操作可以基于原生的Redis 结构。保存数据时值依据对应的索引被写入,当删除或过期时则被移除。

1. Simple Property Index

对于简单的Person 实体来说,我们可以通过添加@Indexed 注解来对firstname 创建一个索引。

Example 13. Annotation driven indexing

@RedisHash("persons")
public class Person {

  @Id String id;
  @Indexed String firstname;
  String lastname;
  Address address;
}

索引针对真实的属性值建立。保存两个Person 如”rand” 和”aviendha” ,会建立如下索引:

SADD persons:firstname:rand e2c7dcee-b8cd-4424-883e-736ce564363e
SADD persons:firstname:aviendha a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56

给嵌套元素创建索引也是可能的。假如Address 有一个添加了@Indexed 注解的属性city 。这种情况下,一旦person.address.city 不为空,则每个city 都会有一个索引。

SADD persons:address.city:tear e2c7dcee-b8cd-4424-883e-736ce564363e

通过编程还可以为map 的键和list 的属性创建索引。

@RedisHash("persons")
public class Person {

  // ... other properties omitted

  Map<String,String> attributes;      
  Map<String, Person> relatives;       
  List<Address> addresses;            
}
SADD persons:attributes.map-key:map-value e2c7dcee-b8cd-4424-883e-736ce564363e
SADD persons:relatives.map-key.firstname:tam e2c7dcee-b8cd-4424-883e-736ce564363e
SADD persons:addresses.city:tear e2c7dcee-b8cd-4424-883e-736ce564363e
Indexes will not be resolved on References. 索引不能应用在引用上。

和keyspaces 一样,也可以配置索引,而不使用注解。

Example 14. Index Setup via @EnableRedisRepositories

@Configuration
@EnableRedisRepositories(indexConfiguration = MyIndexConfiguration.class)
public class ApplicationConfig {

  //... RedisConnectionFactory and RedisTemplate Bean definitions omitted

  public static class MyIndexConfiguration extends IndexConfiguration {

    @Override
    protected Iterable<IndexDefinition> initialConfiguration() {
      return Collections.singleton(new SimpleIndexDefinition("persons", "firstname"));
    }
  }
}

Example 15. Programmatic Index setup

@Configuration
@EnableRedisRepositories
public class ApplicationConfig {

  //... RedisConnectionFactory and RedisTemplate Bean definitions omitted

  @Bean
  public RedisMappingContext keyValueMappingContext() {
    return new RedisMappingContext(
      new MappingConfiguration(
        new KeyspaceConfiguration(), new MyIndexConfiguration()));
  }

  public static class MyIndexConfiguration extends IndexConfiguration {

    @Override
    protected Iterable<IndexDefinition> initialConfiguration() {
      return Collections.singleton(new SimpleIndexDefinition("persons", "firstname"));
    }
  }
}

2. Geospatial Index

假如Address 拥有一个Point 类型的location 属性,该属性标识了特定地址的位置坐标。当属性拥有@GeoIndexed 注解时,其值将会使用Redis GEO 命令来添加。

@RedisHash("persons")
public class Person {

  Address address;

  // ... other properties omitted
}

public class Address {

  @GeoIndexed Point location;

  // ... other properties omitted
}

public interface PersonRepository extends CrudRepository<Person, String> {

  List<Person> findByAddressLocationNear(Point point, Distance distance);     
  List<Person> findByAddressLocationWithin(Circle circle);                    
}

Person rand = new Person("rand", "al'thor");
rand.setAddress(new Address(new Point(13.361389D, 38.115556D)));

repository.save(rand);                                                        

repository.findByAddressLocationNear(new Point(15D, 37D), new Distance(200)); 
Query method declaration on nested property using Point and Distance.
Query method declaration on nested property using Circle to search within.
GEOADD persons:address:location 13.361389 38.115556 e2c7dcee-b8cd-4424-883e-736ce564363e
GEORADIUS persons:address:location 15.0 37.0 200.0 km

上面的示例中,lon/lat 的值被存储,使用的是GEOADD 命令,并使用对象的id 作为成员的name 。查询方法允许使用Circle 、Point和Distance 组合来查询值。

It is not possible to combine near/within with other criteria.
near/within 不能和其他条件进行组合式使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值