二级索引

二级索引用于基于原生 Redis 结构启用查找操作。 每次保存时,值都会写入相应的索引中,并在对象被删除或过期时移除。spring-doc.cadn.net.cn

简单属性索引

鉴于前面所示的示例 Person 实体,我们可以通过在属性上添加 firstname 注解来为 @Indexed 创建索引,如下例所示:spring-doc.cadn.net.cn

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

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

索引是根据实际的属性值构建的。 保存两个 Person 对象(例如,“rand”和“aviendha”)将生成类似于以下内容的索引:spring-doc.cadn.net.cn

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,如下例所示:spring-doc.cadn.net.cn

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

此外,编程式配置还允许您在 Map 的键和列表属性上定义索引,如下例所示:spring-doc.cadn.net.cn

@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
无法在引用上解析索引。

与键空间(keyspaces)一样,您可以配置索引而无需对实际的领域类型添加注解,如下例所示:spring-doc.cadn.net.cn

示例 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"));
    }
  }
}

同样,与键空间(keyspaces)一样,您可以以编程方式配置索引,如下例所示:spring-doc.cadn.net.cn

示例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 命令来存储这些值,如下例所示:spring-doc.cadn.net.cn

@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 组合来查询这些值。spring-doc.cadn.net.cn

无法将 nearwithin 与其他条件组合使用。