Skip to content

Commit 1a02879

Browse files
authored
Merge pull request #385 from weaviate/feat/add_vector
Allows adding named vectors to collections with existing named vectors.
2 parents 03cf63d + 5c1b80f commit 1a02879

7 files changed

Lines changed: 439 additions & 268 deletions

File tree

src/main/java/io/weaviate/client/v1/async/schema/Schema.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
package io.weaviate.client.v1.async.schema;
22

3+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
4+
35
import io.weaviate.client.Config;
46
import io.weaviate.client.base.util.DbVersionSupport;
57
import io.weaviate.client.v1.async.schema.api.ClassCreator;
68
import io.weaviate.client.v1.async.schema.api.ClassDeleter;
79
import io.weaviate.client.v1.async.schema.api.ClassExists;
810
import io.weaviate.client.v1.async.schema.api.ClassGetter;
911
import io.weaviate.client.v1.async.schema.api.ClassUpdater;
10-
import io.weaviate.client.v1.async.schema.api.SchemaGetter;
1112
import io.weaviate.client.v1.async.schema.api.PropertyCreator;
1213
import io.weaviate.client.v1.async.schema.api.SchemaDeleter;
13-
import io.weaviate.client.v1.async.schema.api.ShardsGetter;
14+
import io.weaviate.client.v1.async.schema.api.SchemaGetter;
1415
import io.weaviate.client.v1.async.schema.api.ShardUpdater;
16+
import io.weaviate.client.v1.async.schema.api.ShardsGetter;
1517
import io.weaviate.client.v1.async.schema.api.ShardsUpdater;
1618
import io.weaviate.client.v1.async.schema.api.TenantsCreator;
17-
import io.weaviate.client.v1.async.schema.api.TenantsGetter;
18-
import io.weaviate.client.v1.async.schema.api.TenantsUpdater;
1919
import io.weaviate.client.v1.async.schema.api.TenantsDeleter;
2020
import io.weaviate.client.v1.async.schema.api.TenantsExists;
21-
21+
import io.weaviate.client.v1.async.schema.api.TenantsGetter;
22+
import io.weaviate.client.v1.async.schema.api.TenantsUpdater;
23+
import io.weaviate.client.v1.async.schema.api.VectorAdder;
2224
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
23-
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
2425

2526
public class Schema {
2627
private final CloseableHttpAsyncClient client;
2728
private final Config config;
2829
private final AccessTokenProvider tokenProvider;
2930
private final DbVersionSupport dbVersionSupport;
3031

31-
public Schema(CloseableHttpAsyncClient client, Config config, AccessTokenProvider tokenProvider, DbVersionSupport dbVersionSupport) {
32+
public Schema(CloseableHttpAsyncClient client, Config config, AccessTokenProvider tokenProvider,
33+
DbVersionSupport dbVersionSupport) {
3234
this.client = client;
3335
this.config = config;
3436
this.tokenProvider = tokenProvider;
@@ -63,8 +65,13 @@ public PropertyCreator propertyCreator() {
6365
return new PropertyCreator(client, config, tokenProvider);
6466
}
6567

68+
public VectorAdder vectorAdder() {
69+
return new VectorAdder(client, config, tokenProvider);
70+
}
71+
6672
public SchemaDeleter allDeleter() {
67-
return new SchemaDeleter(new SchemaGetter(client, config, tokenProvider), new ClassDeleter(client, config, tokenProvider));
73+
return new SchemaDeleter(new SchemaGetter(client, config, tokenProvider),
74+
new ClassDeleter(client, config, tokenProvider));
6875
}
6976

7077
public ShardsGetter shardsGetter() {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package io.weaviate.client.v1.async.schema.api;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.CompletionException;
8+
import java.util.concurrent.ExecutionException;
9+
import java.util.concurrent.Future;
10+
11+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
12+
import org.apache.hc.core5.concurrent.FutureCallback;
13+
import org.apache.hc.core5.http.ContentType;
14+
import org.apache.hc.core5.http.HttpResponse;
15+
16+
import io.weaviate.client.Config;
17+
import io.weaviate.client.base.AsyncBaseClient;
18+
import io.weaviate.client.base.AsyncClientResult;
19+
import io.weaviate.client.base.Response;
20+
import io.weaviate.client.base.Result;
21+
import io.weaviate.client.base.http.async.ResponseParser;
22+
import io.weaviate.client.base.util.UrlEncoder;
23+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
24+
import io.weaviate.client.v1.schema.model.WeaviateClass;
25+
import io.weaviate.client.v1.schema.model.WeaviateClass.VectorConfig;
26+
27+
public class VectorAdder extends AsyncBaseClient<Boolean> implements AsyncClientResult<Boolean> {
28+
private final ClassGetter getter;
29+
30+
private String className;
31+
private Map<String, VectorConfig> addedVectors = new HashMap<>();
32+
33+
public VectorAdder(CloseableHttpAsyncClient client, Config config, AccessTokenProvider tokenProvider) {
34+
super(client, config, tokenProvider);
35+
this.getter = new ClassGetter(client, config, tokenProvider);
36+
}
37+
38+
public VectorAdder withClassName(String className) {
39+
this.className = className;
40+
return this;
41+
}
42+
43+
/**
44+
* Add a named vectors. This method can be chained to add multiple named vectors
45+
* without using a {@link Map}.
46+
*/
47+
public VectorAdder withVectorConfig(String name, VectorConfig vector) {
48+
this.addedVectors.put(name, vector);
49+
return this;
50+
}
51+
52+
/**
53+
* Add all vectors from the map. This will overwrite any vectors added
54+
* previously.
55+
*/
56+
public VectorAdder withVectorConfig(Map<String, VectorConfig> vectors) {
57+
this.addedVectors = Collections.unmodifiableMap(vectors);
58+
return this;
59+
}
60+
61+
@Override
62+
public Future<Result<Boolean>> run(FutureCallback<Result<Boolean>> callback) {
63+
CompletableFuture<Result<WeaviateClass>> getClass = CompletableFuture.supplyAsync(() -> {
64+
try {
65+
return getter.withClassName(className).run().get();
66+
} catch (InterruptedException | ExecutionException e) {
67+
throw new CompletionException(e);
68+
}
69+
});
70+
CompletableFuture<Result<Boolean>> addVectors = getClass.<Result<Boolean>>thenApplyAsync(result -> {
71+
if (result.getError() != null) {
72+
return result.toErrorResult();
73+
}
74+
WeaviateClass cls = result.getResult();
75+
addedVectors.entrySet().stream()
76+
.forEach(vector -> cls.getVectorConfig()
77+
.putIfAbsent(vector.getKey(), vector.getValue()));
78+
79+
String path = String.format("/schema/%s", UrlEncoder.encodePathParam(className));
80+
try {
81+
return sendPutRequest(path, cls, callback, new ResponseParser<Boolean>() {
82+
83+
@Override
84+
public Result<Boolean> parse(HttpResponse response, String body, ContentType contentType) {
85+
Response<WeaviateClass> resp = this.serializer.toResponse(response.getCode(), body, WeaviateClass.class);
86+
return new Result<>(response.getCode(), response.getCode() <= 299, resp.getErrors());
87+
}
88+
}).get();
89+
} catch (InterruptedException | ExecutionException e) {
90+
throw new CompletionException(e);
91+
}
92+
});
93+
94+
return addVectors;
95+
}
96+
}

src/main/java/io/weaviate/client/v1/schema/Schema.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.weaviate.client.v1.schema.api.TenantsExists;
2020
import io.weaviate.client.v1.schema.api.TenantsGetter;
2121
import io.weaviate.client.v1.schema.api.TenantsUpdater;
22+
import io.weaviate.client.v1.schema.api.VectorAdder;
2223

2324
public class Schema {
2425
private final Config config;
@@ -59,6 +60,10 @@ public PropertyCreator propertyCreator() {
5960
return new PropertyCreator(httpClient, config);
6061
}
6162

63+
public VectorAdder vectorAdder() {
64+
return new VectorAdder(httpClient, config);
65+
}
66+
6267
public SchemaDeleter allDeleter() {
6368
return new SchemaDeleter(new SchemaGetter(httpClient, config), new ClassDeleter(httpClient, config));
6469
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.weaviate.client.v1.schema.api;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import io.weaviate.client.Config;
8+
import io.weaviate.client.base.BaseClient;
9+
import io.weaviate.client.base.ClientResult;
10+
import io.weaviate.client.base.Response;
11+
import io.weaviate.client.base.Result;
12+
import io.weaviate.client.base.http.HttpClient;
13+
import io.weaviate.client.base.util.UrlEncoder;
14+
import io.weaviate.client.v1.schema.model.WeaviateClass;
15+
import io.weaviate.client.v1.schema.model.WeaviateClass.VectorConfig;
16+
17+
public class VectorAdder extends BaseClient<WeaviateClass> implements ClientResult<Boolean> {
18+
private final ClassGetter getter;
19+
20+
private String className;
21+
private Map<String, VectorConfig> addedVectors = new HashMap<>();
22+
23+
public VectorAdder(HttpClient httpClient, Config config) {
24+
super(httpClient, config);
25+
this.getter = new ClassGetter(httpClient, config);
26+
}
27+
28+
public VectorAdder withClassName(String className) {
29+
this.className = className;
30+
return this;
31+
}
32+
33+
/**
34+
* Add a named vectors. This method can be chained to add multiple named vectors
35+
* without using a {@link Map}.
36+
*/
37+
public VectorAdder withVectorConfig(String name, VectorConfig vector) {
38+
this.addedVectors.put(name, vector);
39+
return this;
40+
}
41+
42+
/**
43+
* Add all vectors from the map. This will overwrite any vectors added
44+
* previously.
45+
*/
46+
public VectorAdder withVectorConfig(Map<String, VectorConfig> vectors) {
47+
this.addedVectors = Collections.unmodifiableMap(vectors);
48+
return this;
49+
}
50+
51+
@Override
52+
public Result<Boolean> run() {
53+
Result<WeaviateClass> result = getter.withClassName(className).run();
54+
if (result.hasErrors()) {
55+
result.toErrorResult();
56+
}
57+
58+
WeaviateClass cls = result.getResult();
59+
addedVectors.entrySet().stream()
60+
.forEach(vector -> cls.getVectorConfig()
61+
.putIfAbsent(vector.getKey(), vector.getValue()));
62+
63+
String path = String.format("/schema/%s", UrlEncoder.encodePathParam(className));
64+
Response<WeaviateClass> resp = sendPutRequest(path, cls, WeaviateClass.class);
65+
return new Result<>(resp.getStatusCode(), resp.getStatusCode() == 200, resp.getErrors());
66+
}
67+
}

src/test/java/io/weaviate/integration/client/WeaviateVersion.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
public class WeaviateVersion {
44

55
// docker image version
6-
public static final String WEAVIATE_IMAGE = "1.31.0-rc.0-5ff7495";
6+
public static final String WEAVIATE_IMAGE = "1.31.0";
77

88
// to be set according to weaviate docker image
9-
public static final String EXPECTED_WEAVIATE_VERSION = "1.31.0-rc.0";
9+
public static final String EXPECTED_WEAVIATE_VERSION = "1.31.0";
1010
// to be set according to weaviate docker image
11-
public static final String EXPECTED_WEAVIATE_GIT_HASH = "5ff7495";
11+
public static final String EXPECTED_WEAVIATE_GIT_HASH = "79499d6";
1212

13-
private WeaviateVersion() {}
13+
private WeaviateVersion() {
14+
}
1415
}

0 commit comments

Comments
 (0)