跳到主要内容
版本:4.1.1

二级索引

Doubao 中英对照 Secondary Indexes

二级索引用于支持基于原生 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
}
  • SADD people:attributes.map-key:map-value e2c7dcee-b8cd-4424-883e-736ce564363e

  • SADD people:relatives.map-key.firstname:tam e2c7dcee-b8cd-4424-883e-736ce564363e

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

警告

无法在 References 上解析索引。

与 keyspace 一样,你无需注解实际的领域类型即可配置索引,如下例所示:

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

同样,与 keyspace 一样,你可以以编程方式配置索引,如下例所示:

示例 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
  • 在嵌套属性上声明查询方法,使用 Point 和 Distance。

  • 在嵌套属性上声明查询方法,使用 Circle 进行范围搜索。

  • GEOADD people:address:location 13.361389 38.115556 e2c7dcee-b8cd-4424-883e-736ce564363e

  • GEORADIUS people:address:location 15.0 37.0 200.0 km

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

备注

不能将 near 和 within 与其他条件组合使用。