二级索引

二级索引 用于支持基于原生 Redis 结构的查找操作。在每次保存时,值会被写入相应的索引;当对象被删除或 过期 时,值会被移除。

简单属性索引

根据前面展示的 Person 实体示例,我们可以通过使用 @Indexed 注解属性来为 firstname 创建索引,如下所示:

示例 1. 注解驱动的索引
@RedisHash("people")
public class Person {

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

索引是针对实际属性值构建的。保存两个 Person 对象(例如,“rand” 和 “aviendha”)会建立类似于以下的索引:

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

还可以为嵌套元素创建索引。假设 Address 有一个用 @Indexed 注解的 city 属性。在这种情况下,一旦 person.address.city 不为 null,我们就会为每个城市创建一个 Set,如下所示:

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

此外,通过编程方式的设置,你可以在 map 键和 list 属性上定义索引,如下所示:

@RedisHash("people")
public class Person {

  // ... other properties omitted

  Map<String, String> attributes;     (1)
  Map<String, Person> relatives;      (2)
  List<Address> addresses;            (3)
}
1 SADD people:attributes.map-key:map-value e2c7dcee-b8cd-4424-883e-736ce564363e
2 SADD people:relatives.map-key.firstname:tam e2c7dcee-b8cd-4424-883e-736ce564363e
3 SADD people:addresses.city:tear e2c7dcee-b8cd-4424-883e-736ce564363e
无法在 引用(References) 上解析索引。

与键空间类似,你无需注解实际的域类型即可配置索引,如下所示:

示例 2. 使用 @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("people", "firstname"));
    }
  }
}

同样,与键空间类似,你也可以通过编程方式配置索引,如下所示:

示例 3. 编程方式设置索引
@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("people", "firstname"));
    }
  }
}

地理空间索引

假设 Address 类型包含一个 Point 类型的 location 属性,用于存储特定地址的地理坐标。通过使用 @GeoIndexed 注解该属性,Spring Data Redis 会使用 Redis GEO 命令添加这些值,如下所示:

@RedisHash("people")
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);     (1)
  List<Person> findByAddressLocationWithin(Circle circle);                    (2)
}

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

repository.save(rand);                                                        (3)

repository.findByAddressLocationNear(new Point(15D, 37D), new Distance(200, Metrics.KILOMETERS)); (4)
1 在嵌套属性上的查询方法声明,使用 PointDistance
2 在嵌套属性上的查询方法声明,使用 Circle 进行范围搜索。
3 GEOADD people:address:location 13.361389 38.115556 e2c7dcee-b8cd-4424-883e-736ce564363e
4 GEORADIUS people:address:location 15.0 37.0 200.0 km

在前面的示例中,经度和纬度值通过 GEOADD 命令存储,使用对象的 id 作为成员名称。查找方法允许使用 CirclePoint, Distance 的组合来查询这些值。

不能nearwithin 与其他条件组合使用。