From ebbcc5c111024c79a5b8f5642784c7b9bd44efab Mon Sep 17 00:00:00 2001 From: maheshrajamani <99678631+maheshrajamani@users.noreply.github.com> Date: Fri, 28 Jul 2023 07:52:00 -0400 Subject: [PATCH 1/2] Changes for projection support for $vector field --- .../service/projection/DocumentProjector.java | 3 ++- .../jsonapi/api/v1/VectorSearchIntegrationTest.java | 6 ++++++ .../service/projection/DocumentProjectorTest.java | 13 +++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjector.java b/src/main/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjector.java index a48f6231ac..e29260864a 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjector.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjector.java @@ -138,7 +138,8 @@ PathCollector collectFromObject(JsonNode ob, String parentPath) { ErrorCode.UNSUPPORTED_PROJECTION_PARAM.getMessage() + ": empty paths (and path segments) not allowed"); } - if (path.charAt(0) == '$') { + if (path.charAt(0) == '$' + && !path.equals(DocumentConstants.Fields.VECTOR_EMBEDDING_FIELD)) { // First: no operators allowed at root level if (parentPath == null) { throw new JsonApiException( 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 f8539eee58..b5d6bc2ff5 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 @@ -392,6 +392,7 @@ public void happyPath() { { "find": { "sort" : {"$vector" : [0.15, 0.1, 0.1, 0.35, 0.55]}, + "projection" : {"_id" : 1, "$vector" : 1}, "options" : { "limit" : 5 } @@ -408,8 +409,11 @@ public void happyPath() { .then() .statusCode(200) .body("data.documents[0]._id", is("3")) + .body("data.documents[0].$vector", is(notNullValue())) .body("data.documents[1]._id", is("2")) + .body("data.documents[1].$vector", is(notNullValue())) .body("data.documents[2]._id", is("1")) + .body("data.documents[2].$vector", is(notNullValue())) .body("errors", is(nullValue())); } @@ -421,6 +425,7 @@ public void happyPathWithFilter() { { "find": { "filter" : {"_id" : "1"}, + "projection" : {"_id" : 1, "$vector" : 0}, "sort" : {"$vector" : [0.15, 0.1, 0.1, 0.35, 0.55]}, "options" : { "limit" : 5 @@ -438,6 +443,7 @@ public void happyPathWithFilter() { .then() .statusCode(200) .body("data.documents[0]._id", is("1")) + .body("data.documents[0].$vector", is(nullValue())) .body("errors", is(nullValue())); } diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java b/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java index 20d57927e0..8096ff88b7 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java @@ -263,7 +263,8 @@ public void testSimpleIncludeWithId() throws Exception { }, "nested2" : { "z": 5 - } + }, + "$vector" : [0.11, 0.22, 0.33, 0.44] } """); DocumentProjector projection = @@ -275,7 +276,8 @@ public void testSimpleIncludeWithId() throws Exception { "x": 1 }, "nested.z": 1, - "nosuchprop": 1 + "nosuchprop": 1, + "$vector": 1 } """)); assertThat(projection.isInclusion()).isTrue(); @@ -289,7 +291,8 @@ public void testSimpleIncludeWithId() throws Exception { "nested" : { "x": 3, "z": -1 - } + }, + "$vector" : [0.11, 0.22, 0.33, 0.44] } """)); } @@ -392,7 +395,8 @@ public void excludeWithIdIncluded() throws Exception { }, "nested2" : { "z": 5 - } + }, + "$vector" : [0.11, 0.22, 0.33, 0.44] } """); DocumentProjector projection = @@ -406,6 +410,7 @@ public void excludeWithIdIncluded() throws Exception { }, "nested.z": 0, "nosuchprop": 0 + "$vector": 0 } """)); assertThat(projection.isInclusion()).isFalse(); From f76498f20a01cd5ff3daf54130cdee22919a9134 Mon Sep 17 00:00:00 2001 From: maheshrajamani <99678631+maheshrajamani@users.noreply.github.com> Date: Fri, 28 Jul 2023 07:53:16 -0400 Subject: [PATCH 2/2] Fix in DocumentProjectorTest query json --- .../sgv2/jsonapi/service/projection/DocumentProjectorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java b/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java index 8096ff88b7..7721db0ede 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/service/projection/DocumentProjectorTest.java @@ -409,7 +409,7 @@ public void excludeWithIdIncluded() throws Exception { "x": 0 }, "nested.z": 0, - "nosuchprop": 0 + "nosuchprop": 0, "$vector": 0 } """));