Skip to content

Commit dc72f76

Browse files
authored
[NOID] Fixes #4232: The apoc.vectordb.configure(WEAVIATE', ..) procedure should append /v1 to url (#4248) (#4276)
1 parent 2b74d67 commit dc72f76

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

full-it/src/test/java/apoc/full/it/vectordb/WeaviateTest.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,24 @@ public void queryVectorsWithCreateRelWithoutVectorResult() {
534534
public void queryVectorsWithSystemDbStorage() {
535535
String keyConfig = "weaviate-config-foo";
536536
String baseUrl = "http://" + HOST + "/v1";
537+
assertQueryVectorsWithSystemDbStorage(keyConfig, baseUrl, false);
538+
}
539+
540+
@Test
541+
public void queryVectorsWithSystemDbStorageWithUrlWithoutVersion() {
542+
String keyConfig = "weaviate-config-foo";
543+
String baseUrl = "http://" + HOST;
544+
assertQueryVectorsWithSystemDbStorage(keyConfig, baseUrl, false);
545+
}
546+
547+
@Test
548+
public void queryVectorsWithSystemDbStorageWithUrlV3Version() {
549+
String keyConfig = "weaviate-config-foo";
550+
String baseUrl = "http://" + HOST + "/v3";
551+
assertQueryVectorsWithSystemDbStorage(keyConfig, baseUrl, true);
552+
}
553+
554+
private static void assertQueryVectorsWithSystemDbStorage(String keyConfig, String baseUrl, boolean fails) {
537555
Map<String, String> mapping =
538556
map(EMBEDDING_KEY, "vect", NODE_LABEL, "Test", ENTITY_KEY, "myId", METADATA_KEY, "foo");
539557
sysDb.executeTransactionally(
@@ -550,25 +568,29 @@ public void queryVectorsWithSystemDbStorage() {
550568
"host", baseUrl,
551569
"credentials", ADMIN_KEY,
552570
"mapping", mapping)));
553-
554571
db.executeTransactionally("CREATE (:Test {myId: 'one'}), (:Test {myId: 'two'})");
555-
556-
testResult(
557-
db,
558-
"CALL apoc.vectordb.weaviate.queryAndUpdate($host, 'TestCollection', [0.2, 0.1, 0.9, 0.7], null, 5, $conf)",
559-
map("host", keyConfig, "conf", map("fields", FIELDS, ALL_RESULTS_KEY, true)),
560-
r -> {
561-
Map<String, Object> row = r.next();
562-
assertBerlinResult(row, ID_1, NODE);
563-
assertNotNull(row.get("score"));
564-
assertNotNull(row.get("vector"));
565-
566-
row = r.next();
567-
assertLondonResult(row, ID_2, NODE);
568-
assertNotNull(row.get("score"));
569-
assertNotNull(row.get("vector"));
570-
});
571-
572+
String query =
573+
"CALL apoc.vectordb.weaviate.queryAndUpdate($host, 'TestCollection', [0.2, 0.1, 0.9, 0.7], null, 5, $conf)";
574+
Map<String, Object> params = map("host", keyConfig, "conf", map(FIELDS_KEY, FIELDS, ALL_RESULTS_KEY, true));
575+
if (fails) {
576+
try {
577+
testCall(db, query, params, r -> fail());
578+
} catch (Exception e) {
579+
String message = e.getMessage();
580+
assertTrue(message.contains("java.io.FileNotFoundException"));
581+
}
582+
return;
583+
}
584+
testResult(db, query, params, r -> {
585+
Map<String, Object> row = r.next();
586+
assertBerlinResult(row, ID_1, NODE);
587+
assertNotNull(row.get("score"));
588+
assertNotNull(row.get("vector"));
589+
row = r.next();
590+
assertLondonResult(row, ID_2, NODE);
591+
assertNotNull(row.get("score"));
592+
assertNotNull(row.get("vector"));
593+
});
572594
assertNodesCreated(db);
573595
}
574596
}

full/src/main/java/apoc/vectordb/VectorDb.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static apoc.util.Util.listOfNumbersToFloatArray;
66
import static apoc.util.Util.setProperties;
77
import static apoc.vectordb.VectorDbUtil.*;
8+
import static apoc.vectordb.VectorDbUtil.appendVersionUrlIfNeeded;
89
import static apoc.vectordb.VectorEmbeddingConfig.ALL_RESULTS_KEY;
910
import static apoc.vectordb.VectorEmbeddingConfig.MAPPING_KEY;
1011

@@ -267,7 +268,7 @@ public void vectordb(
267268
Node node = Util.mergeNode(transaction, label, null, Pair.of(SystemPropertyKeys.name.name(), configKey));
268269

269270
Map mapping = (Map) config.get("mapping");
270-
String host = (String) config.get("host");
271+
String host = appendVersionUrlIfNeeded(type, (String) config.get("host"));
271272
Object credentials = config.get("credentials");
272273

273274
if (host != null) {

full/src/main/java/apoc/vectordb/VectorDbUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,18 @@ public static void checkMappingConf(Map<String, Object> configuration, String pr
110110
ERROR_READONLY_MAPPING + "\n" + "Try the equivalent procedure, which is the " + procName);
111111
}
112112
}
113+
114+
/**
115+
* If the vectorDb is WEAVIATE and endpoint doesn't end with `/vN`, where N is a number,
116+
* then add `/v1` to the endpoint
117+
*/
118+
public static String appendVersionUrlIfNeeded(VectorDbHandler.Type type, String host) {
119+
if (VectorDbHandler.Type.WEAVIATE == type) {
120+
String regex = ".*(/v\\d+)$";
121+
if (!host.matches(regex)) {
122+
host = host + "/v1";
123+
}
124+
}
125+
return host;
126+
}
113127
}

0 commit comments

Comments
 (0)