简单属性索引
给定前面所示的示例实体,我们可以通过使用 注释属性来创建索引,如以下示例所示:Personfirstname@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
也可以对嵌套元素进行索引。
假设具有用 批注的属性。
在这种情况下,once is not ,我们每个城市都有 Sets,如以下示例所示:Addresscity@Indexedperson.address.citynull
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"));
    }
  }
}
| 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 | 
| 无法在引用上解析索引。 | 
地理空间索引
假定该类型包含一个属性,该属性包含特定地址的地理坐标。
通过使用 注释属性,Spring Data Redis 使用 Redis 命令添加这些值,如以下示例所示:AddresslocationPoint@GeoIndexedGEO
@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 | 
在前面的示例中,经度和纬度值通过使用对象作为成员的名称来存储。
finder 方法允许使用或组合来查询这些值。GEOADDidCirclePoint, Distance
不可能与其他标准结合使用。nearwithin | 
| 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 | 
不可能与其他标准结合使用。nearwithin |