Skip to content

Commit 3b8ef80

Browse files
denglimingclaude
andcommitted
Add vectorset module for the Redis 8 vector set data type
VectorSet (per key, built on RedissonObject) covers all 13 commands: VADD with AddArgs (REDUCE, CAS, quantization, EF, SETATTR, M), VSIM by vector, FP32 blob or element with SimilarArgs (scores, attributes, COUNT, EPSILON, EF, FILTER, FILTER-EF, TRUTH, NOTHREAD), VREM, VCARD, VDIM, VEMB (plain and RAW), VGETATTR, VSETATTR, VINFO, VLINKS (plain and with scores), VISMEMBER, VRANDMEMBER and VRANGE. VectorSetClient and VectorSetBatch follow the other modules; the all artifact and the Spring Boot starter include it. Integration tests run against the redis:8.8 CI image. README, module README and an example are included. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7686709 commit 3b8ef80

24 files changed

Lines changed: 1534 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ other modules or with an existing Redisson instance.
2626
| RediSearch | `redisearch` | ✅ | Active | [commands](redisearch/README.md) |
2727
| RedisJSON | `redisjson` | ✅ | Active | [commands](redisjson/README.md) |
2828
| RedisTimeSeries | `redistimeseries` | ✅ | Active | [commands](redistimeseries/README.md) |
29+
| Vector sets (Redis 8 native type) | `vectorset` | ✅ | Active | [commands](vectorset/README.md) |
2930
| RedisGraph | `redisgraph` | ❌ | Deprecated, upstream end of life | [commands](redisgraph/README.md) |
3031
| RedisAI | `redisai` | ❌ | Deprecated, upstream end of life | [commands](redisai/README.md) |
3132
| RedisGears | `redisgears` | ❌ | Deprecated, upstream end of life | [commands](redisgears/README.md) |
@@ -39,6 +40,7 @@ users; they will be removed in a future major release.
3940

4041
- Java 8 or later
4142
- Redis 8.x (modules built in), or Redis 7.x with the corresponding module loaded
43+
- Vector sets need Redis 8.0+ (`VRANGE` needs 8.4+)
4244
- Redisson 3.27.x (pulled in transitively)
4345

4446
## Installation
@@ -209,6 +211,20 @@ List<Sample.Value> values = ts.range("temperature:2:32", 0, Long.MAX_VALUE,
209211
List<TimeSeries> byArea = ts.mrange(0, Long.MAX_VALUE, new RangeOptions().withLabels(), "area_id=32");
210212
```
211213

214+
### Vector sets
215+
216+
```java
217+
VectorSetClient client = new VectorSetClient(config);
218+
VectorSet movies = client.getVectorSet("movies");
219+
220+
movies.add("matrix", new double[]{0.9, 0.1, 0.0}, new AddArgs().attributes("{\"year\":1999}"));
221+
movies.add("amelie", new double[]{0.0, 0.2, 0.9}, new AddArgs().attributes("{\"year\":2001}"));
222+
223+
List<Similarity> hits = movies.similar(new double[]{1.0, 0.0, 0.0}, new SimilarArgs().withScores().withAttribs().count(5));
224+
List<Similarity> recent = movies.similarTo("matrix", new SimilarArgs().filter(".year > 2000"));
225+
List<Double> vector = movies.getVector("matrix");
226+
```
227+
212228
### RedisGraph (deprecated)
213229

214230
```java

‎all/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@
5353
<groupId>${project.groupId}</groupId>
5454
<artifactId>redisgraph</artifactId>
5555
</dependency>
56+
57+
<dependency>
58+
<groupId>${project.groupId}</groupId>
59+
<artifactId>vectorset</artifactId>
60+
</dependency>
5661
</dependencies>
5762
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.examples.vectorset;
18+
19+
import io.github.dengliming.redismodule.vectorset.VectorSet;
20+
import io.github.dengliming.redismodule.vectorset.args.AddArgs;
21+
import io.github.dengliming.redismodule.vectorset.args.SimilarArgs;
22+
import io.github.dengliming.redismodule.vectorset.client.VectorSetClient;
23+
import io.github.dengliming.redismodule.vectorset.model.Similarity;
24+
import org.redisson.config.Config;
25+
26+
public class Examples {
27+
28+
public static void main(String[] args) {
29+
Config config = new Config();
30+
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
31+
VectorSetClient client = new VectorSetClient(config);
32+
33+
VectorSet movies = client.getVectorSet("movies");
34+
movies.add("matrix", new double[]{0.9, 0.1, 0.0}, new AddArgs().attributes("{\"year\":1999,\"genre\":\"sci-fi\"}"));
35+
movies.add("inception", new double[]{0.8, 0.2, 0.1}, new AddArgs().attributes("{\"year\":2010,\"genre\":\"sci-fi\"}"));
36+
movies.add("amelie", new double[]{0.0, 0.2, 0.9}, new AddArgs().attributes("{\"year\":2001,\"genre\":\"romance\"}"));
37+
38+
// nearest neighbours of a query vector, with scores and attributes
39+
for (Similarity hit : movies.similar(new double[]{1.0, 0.0, 0.0}, new SimilarArgs().withScores().withAttribs().count(2))) {
40+
System.out.println(hit.getElement() + " " + hit.getScore() + " " + hit.getAttributes());
41+
}
42+
43+
// neighbours of an existing element, filtered on attributes
44+
for (Similarity hit : movies.similarTo("matrix", new SimilarArgs().filter(".year > 2000"))) {
45+
System.out.println("after 2000: " + hit.getElement());
46+
}
47+
48+
System.out.println(movies.cardinality() + " vectors of " + movies.dimension() + " dimensions");
49+
System.out.println(movies.members());
50+
51+
movies.delete();
52+
client.shutdown();
53+
}
54+
}

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>redisgears</module>
1818
<module>redisjson</module>
1919
<module>redisgraph</module>
20+
<module>vectorset</module>
2021
<module>commons</module>
2122
<module>spring-boot-starter</module>
2223
</modules>
@@ -133,6 +134,11 @@
133134
<artifactId>redisgraph</artifactId>
134135
<version>${project.version}</version>
135136
</dependency>
137+
<dependency>
138+
<groupId>io.github.dengliming.redismodule</groupId>
139+
<artifactId>vectorset</artifactId>
140+
<version>${project.version}</version>
141+
</dependency>
136142

137143
<dependency>
138144
<groupId>io.github.dengliming.redismodule</groupId>

‎spring-boot-starter/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Spring Boot Starter
22

33
Auto-configures one client bean per enabled module: `RedisJSONClient`, `RediSearchClient`,
4-
`RedisBloomClient`, `RedisTimeSeriesClient` (and the deprecated `RedisGraphClient`, `RedisAIClient`,
4+
`RedisBloomClient`, `RedisTimeSeriesClient`, `VectorSetClient` (and the deprecated `RedisGraphClient`, `RedisAIClient`,
55
`RedisGearsClient`). Works with Spring Boot 2.7 and 3.x.
66

77
## Dependency
@@ -20,7 +20,7 @@ Auto-configures one client bean per enabled module: `RedisJSONClient`, `RediSear
2020
| --- | --- |
2121
| `redis-module.enabled` | Master switch, defaults to `true`. |
2222
| `redis-module.config` | Redisson configuration (YAML or JSON) shared by every module without its own `config`. Creates a `RedissonClient` bean unless the application already defines one. |
23-
| `redis-module.<module>.enabled` | Registers the client bean of that module. `<module>` is one of `redisjson`, `redisearch`, `redisbloom`, `redistimeseries`, `redisgraph`, `redisai`, `redisgears`. |
23+
| `redis-module.<module>.enabled` | Registers the client bean of that module. `<module>` is one of `redisjson`, `redisearch`, `redisbloom`, `redistimeseries`, `vectorset`, `redisgraph`, `redisai`, `redisgears`. |
2424
| `redis-module.<module>.config` | Dedicated Redisson configuration for that module; the client owns and shuts down this instance. |
2525

2626
Resolution order for a module: its own `config` → the `RedissonClient` bean of the context (yours, or the

‎spring-boot-starter/src/main/java/io/github/dengliming/redismodule/springboot/starter/autoconfigure/RedisModuleAutoConfiguration.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.github.dengliming.redismodule.redistimeseries.client.RedisTimeSeriesClient;
2929
import io.github.dengliming.redismodule.springboot.starter.env.RedisModuleProperties;
3030
import io.github.dengliming.redismodule.springboot.starter.env.RedisModuleProperties.RedisModuleConfig;
31+
import io.github.dengliming.redismodule.vectorset.client.VectorSetClient;
3132
import org.redisson.Redisson;
3233
import org.redisson.api.RedissonClient;
3334
import org.redisson.config.Config;
@@ -127,6 +128,13 @@ public RedisTimeSeriesClient redisTimeSeriesClient(ObjectProvider<RedissonClient
127128
RedisTimeSeriesClient::new, RedisTimeSeriesClient::new);
128129
}
129130

131+
@ConditionalOnProperty(prefix = RedisModuleProperties.PREFIX + ".vectorset", name = "enabled", havingValue = "true")
132+
@Bean(destroyMethod = "shutdown")
133+
@ConditionalOnMissingBean
134+
public VectorSetClient vectorSetClient(ObjectProvider<RedissonClient> redisson) {
135+
return createClient("vectorset", redisModuleProperties.getVectorset(), redisson, VectorSetClient::new, VectorSetClient::new);
136+
}
137+
130138
/**
131139
* A module with its own {@code config} gets a dedicated Redisson instance that the client shuts down with
132140
* the context. Otherwise the client wraps the shared {@link RedissonClient} bean and leaves its lifecycle alone.

‎spring-boot-starter/src/main/java/io/github/dengliming/redismodule/springboot/starter/env/RedisModuleProperties.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class RedisModuleProperties {
3737
private RedisModuleConfig redisgraph;
3838
private RedisModuleConfig redisjson;
3939
private RedisModuleConfig redistimeseries;
40+
private RedisModuleConfig vectorset;
4041

4142
public boolean isEnabled() {
4243
return enabled;
@@ -110,6 +111,14 @@ public void setRedistimeseries(RedisModuleConfig redistimeseries) {
110111
this.redistimeseries = redistimeseries;
111112
}
112113

114+
public RedisModuleConfig getVectorset() {
115+
return vectorset;
116+
}
117+
118+
public void setVectorset(RedisModuleConfig vectorset) {
119+
this.vectorset = vectorset;
120+
}
121+
113122
public static class RedisModuleConfig {
114123
private boolean enabled;
115124
private String config;

‎vectorset/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Vector Set Client
2+
3+
Java client for Redis vector sets, a native data type of Redis 8.0+ for storing vectors under named elements
4+
and running approximate nearest-neighbour search over them (HNSW). Elements may carry JSON attributes that
5+
searches can filter on.
6+
Official docs: https://redis.io/docs/latest/develop/data-types/vector-sets/
7+
8+
Obtain a set with `VectorSetClient.getVectorSet(key)` or from `VectorSetBatch` for pipelining. Vectors are
9+
passed as `double[]` (sent as `VALUES`) or as a little-endian FP32 `byte[]`. Every method has an `*Async` twin.
10+
11+
## Command mapping
12+
13+
| Redis command | Java API | Notes |
14+
| --- | --- | --- |
15+
| VADD | `VectorSet.add()` | `REDUCE`, `CAS`, quantization, `EF`, `SETATTR`, `M` via `AddArgs` |
16+
| VSIM | `VectorSet.similar()`, `similarTo()` | options via `SimilarArgs`; result carries score and attributes when requested |
17+
| VREM | `VectorSet.remove()` | |
18+
| VCARD | `VectorSet.cardinality()` | |
19+
| VDIM | `VectorSet.dimension()` | |
20+
| VEMB | `VectorSet.getVector()`, `getRawVector()` | `RAW` returns quantization type, blob, norm and range |
21+
| VGETATTR | `VectorSet.getAttributes()` | |
22+
| VSETATTR | `VectorSet.setAttributes()` | |
23+
| VINFO | `VectorSet.info()` | |
24+
| VLINKS | `VectorSet.links()`, `linksWithScores()` | one list per HNSW layer |
25+
| VISMEMBER | `VectorSet.contains()` | |
26+
| VRANDMEMBER | `VectorSet.randomMember()`, `randomMembers()` | |
27+
| VRANGE | `VectorSet.range()`, `members()` | Redis 8.4+; lexicographic paging with `[`, `(`, `-`, `+` bounds |

‎vectorset/pom.xml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<parent>
5+
<groupId>io.github.dengliming.redismodule</groupId>
6+
<artifactId>redis-modules-java</artifactId>
7+
<version>2.0.5-SNAPSHOT</version>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
<artifactId>vectorset</artifactId>
11+
<name>VectorSet</name>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>io.github.dengliming.redismodule</groupId>
16+
<artifactId>commons</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>io.github.dengliming.redismodule</groupId>
20+
<artifactId>commons</artifactId>
21+
<type>test-jar</type>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
</project>

0 commit comments

Comments
 (0)