| 此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Data Redis 3.5.3! | 
二级索引
简单属性索引
给定示例Person实体,我们可以为firstname通过使用@Indexed,如以下示例所示:
示例 1.注释驱动的索引
@RedisHash("people")
public class Person {
  @Id String id;
  @Indexed String firstname;
  String lastname;
  Address address;
}索引是为实际属性值建立的。 保存两个人(例如,“rand”和“aviendha”)会导致设置类似于以下内容的索引:
SADD people:firstname:rand e2c7dcee-b8cd-4424-883e-736ce564363e
SADD people:firstname:aviendha a9d4b3a0-50d3-4538-a2fc-f7fc2581ee56也可以在嵌套元素上设置索引。
假设Address有一个city用@Indexed.
在这种情况下,一旦person.address.city莫null,我们为每个城市设置了 Sets,如以下示例所示:
SADD people:address.city:tear e2c7dcee-b8cd-4424-883e-736ce564363e此外,编程设置允许您在映射键和列表属性上定义索引,如以下示例所示:
@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 | 
| 无法解析引用上的索引。 | 
与密钥空间一样,您可以配置索引,而无需注释实际的域类型,如以下示例所示:
示例 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类型包含一个location类型的属性Point保存特定地址的地理坐标。
通过使用@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 | 对嵌套属性进行查询方法声明,使用 Point和Distance. | 
| 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作为成员的姓名。
查找器方法允许使用Circle或Point, Distance用于查询这些值的组合。
| 无法组合 near和within与其他标准。 |