From 14090a9bc03953e87ecd8ab552e572017ecc356a Mon Sep 17 00:00:00 2001 From: maheshrajamani <99678631+maheshrajamani@users.noreply.github.com> Date: Mon, 31 Jul 2023 10:38:24 -0400 Subject: [PATCH 1/2] Fixed default vector search function name to cosine. --- .../impl/CreateCollectionCommandResolver.java | 4 ++- .../api/v1/VectorSearchIntegrationTest.java | 26 ++++++++++++++++ .../CreateCollectionCommandResolverTest.java | 31 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java b/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java index 1f1b8ad020..3536820c2a 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java @@ -41,7 +41,9 @@ public Operation resolveCommand(CommandContext ctx, CreateCollectionCommand comm ctx, command.name(), command.options().vector().size(), - command.options().vector().function()); + command.options().vector().function() == null + ? "cosine" + : command.options().vector().function()); } else { return CreateCollectionOperation.withoutVectorSearch(ctx, command.name()); } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/VectorSearchIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/VectorSearchIntegrationTest.java index ea7075adf2..8f6a52acbf 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/VectorSearchIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/VectorSearchIntegrationTest.java @@ -62,6 +62,32 @@ public void happyPathVectorSearch() { .statusCode(200) .body("status.ok", is(1)); } + + @Test + public void happyPathVectorSearchDefaultFunction() { + String json = + """ + { + "createCollection": { + "name" : "my_collection_default_function", + "options": { + "vector": { + "size": 5 + } + } + } + } + """; + given() + .header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken()) + .contentType(ContentType.JSON) + .body(json) + .when() + .post(NamespaceResource.BASE_PATH, namespaceName) + .then() + .statusCode(200) + .body("status.ok", is(1)); + } } @Nested diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolverTest.java b/src/test/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolverTest.java index e5d93b12b9..eda6ede637 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolverTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolverTest.java @@ -85,5 +85,36 @@ public void happyPathVectorSearch() throws Exception { assertThat(op.vectorFunction()).isEqualTo("cosine"); }); } + + @Test + public void happyPathVectorSearchDefaultFunction() throws Exception { + String json = + """ + { + "createCollection": { + "name" : "my_collection", + "options": { + "vector": { + "size": 4 + } + } + } + } + """; + + CreateCollectionCommand command = objectMapper.readValue(json, CreateCollectionCommand.class); + Operation result = resolver.resolveCommand(commandContext, command); + + assertThat(result) + .isInstanceOfSatisfying( + CreateCollectionOperation.class, + op -> { + assertThat(op.name()).isEqualTo("my_collection"); + assertThat(op.commandContext()).isEqualTo(commandContext); + assertThat(op.vectorSearch()).isEqualTo(true); + assertThat(op.vectorSize()).isEqualTo(4); + assertThat(op.vectorFunction()).isEqualTo("cosine"); + }); + } } } From 1debc431a634320b4ebafc2adb561743a6e6fc5d Mon Sep 17 00:00:00 2001 From: maheshrajamani <99678631+maheshrajamani@users.noreply.github.com> Date: Mon, 31 Jul 2023 17:08:40 -0400 Subject: [PATCH 2/2] Changes for create command to default function name to cosine --- .../command/impl/CreateCollectionCommand.java | 7 ++++- .../impl/CreateCollectionCommandResolver.java | 4 +-- .../ObjectMapperConfigurationTest.java | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/CreateCollectionCommand.java b/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/CreateCollectionCommand.java index c671282699..1410c97ec1 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/CreateCollectionCommand.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/api/model/command/impl/CreateCollectionCommand.java @@ -48,6 +48,11 @@ public record VectorSearchConfig( defaultValue = "cosine", type = SchemaType.STRING, implementation = String.class) - String function) {} + String function) { + public VectorSearchConfig(Integer size, String function) { + this.size = size; + this.function = function == null ? "cosine" : function; + } + } } } diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java b/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java index 3536820c2a..1f1b8ad020 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/resolver/model/impl/CreateCollectionCommandResolver.java @@ -41,9 +41,7 @@ public Operation resolveCommand(CommandContext ctx, CreateCollectionCommand comm ctx, command.name(), command.options().vector().size(), - command.options().vector().function() == null - ? "cosine" - : command.options().vector().function()); + command.options().vector().function()); } else { return CreateCollectionOperation.withoutVectorSearch(ctx, command.name()); } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/configuration/ObjectMapperConfigurationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/configuration/ObjectMapperConfigurationTest.java index 0d0bbc4316..a55157ce23 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/configuration/ObjectMapperConfigurationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/configuration/ObjectMapperConfigurationTest.java @@ -346,6 +346,37 @@ public void happyPathVectorSearch() throws Exception { assertThat(createCollection.options().vector().function()).isEqualTo("cosine"); }); } + + @Test + public void happyPathVectorSearchDefaultFunction() throws Exception { + String json = + """ + { + "createCollection": { + "name": "some_name", + "options": { + "vector": { + "size": 5 + } + } + } + } + """; + + Command result = objectMapper.readValue(json, Command.class); + + assertThat(result) + .isInstanceOfSatisfying( + CreateCollectionCommand.class, + createCollection -> { + String name = createCollection.name(); + assertThat(name).isNotNull(); + assertThat(createCollection.options()).isNotNull(); + assertThat(createCollection.options().vector()).isNotNull(); + assertThat(createCollection.options().vector().size()).isEqualTo(5); + assertThat(createCollection.options().vector().function()).isEqualTo("cosine"); + }); + } } @Nested