diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out index 6962a034..24a154ac 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw.out @@ -1,6 +1,6 @@ -- init a test user and set vector pre-filtering on SELECT current_user as original_test_user \gset -CREATE ROLE test_filter_user WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; +CREATE ROLE test_filter_user_hnsw WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; SET citus.next_shard_id TO 8200000; SET documentdb.next_collection_id TO 8200; @@ -777,11 +777,11 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) -- check the vector index is forced to be used -ALTER ROLE test_filter_user SET documentdb.enableVectorPreFilter = "True"; +ALTER ROLE test_filter_user_hnsw SET documentdb.enableVectorPreFilter = "True"; SELECT current_setting('citus' || '.next_shard_id') as vector_citus__next_shard_id \gset SELECT current_setting('documentdb' || '.next_collection_id') as vector__next_collection_id \gset SELECT current_setting('documentdb' || '.next_collection_index_id') as vector__next_collection_index_id \gset -\c - test_filter_user +\c - test_filter_user_hnsw SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); document @@ -790,35 +790,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(29 rows) -- default efSearch = efConstruction(16) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -828,34 +831,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 16 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 16, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) -(25 rows) + -> Seq Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) +(29 rows) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); document @@ -864,35 +871,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) +(29 rows) \c - :original_test_user SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; @@ -927,37 +937,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- "filter": {"meta": { "a" : [ { "b" : 3 } ] }, match with documentIndex_meta @@ -971,37 +984,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) - -> Bitmap Index Scan on "documentIndex_meta" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) + -> Bitmap Index Scan on "documentIndex_meta" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- "filter": {"meta.a.b": 3 }, match with idx_meta.a.b @@ -1015,37 +1031,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a.b": 3 }, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --------------------------------------------------------------------- @@ -1060,36 +1079,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": "some sentence"} } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 16 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 16, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw filter string: with $and, efSearch = 3, match 1 document @@ -1103,80 +1126,87 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 3 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 3 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- hnsw filter string: with $and, efSearch = 1, no match document +-- hnsw filter string: with $and, efSearch = 1, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } +(1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw filter string: with $or, efSearch = 3, match 3 documents @@ -1192,87 +1222,95 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 3 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 3 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(34 rows) ROLLBACK; --- hnsw filter string: with $or, efSearch = 1, match 1 document +-- hnsw filter string: with $or, efSearch = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } +(3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(34 rows) ROLLBACK; -- hnsw filter string: with $or, $and, efSearch = 10, match 2 documents @@ -1287,41 +1325,44 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 10, "path": "v", "filter": {"$or":[{"$and":[{"meta.a":{"$gt":"other sentence"}},{"meta.a":{"$lt":"some sentence"}}]},{"meta.a":{"$in":[{"b":3},{"b":5}]}}]}, "efSearch": 10 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) OR (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_internal.##=) ANY (ARRAY['{ "meta.a" : { "b" : { "$numberInt" : "3" } } }'::documentdb_api_internal.bsonindexbounds, '{ "meta.a" : { "b" : { "$numberInt" : "5" } } }'::documentdb_api_internal.bsonindexbounds])))) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "10" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) OR (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_internal.##=) ANY (ARRAY['{ "meta.a" : { "b" : { "$numberInt" : "3" } } }'::documentdb_api_internal.bsonindexbounds, '{ "meta.a" : { "b" : { "$numberInt" : "5" } } }'::documentdb_api_internal.bsonindexbounds])))) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 10 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "10" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 10, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "10" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) - Filter: (((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) -(32 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) + Filter: (((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) +(35 rows) ROLLBACK; -- hnsw filter string:, with $eq, $gt, $lt, $gte, $lte, $ne, $in, $and, $or, $regex @@ -1401,36 +1442,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.b": 2 } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 16 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 16, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw filter number, with $and, efSearch = 1, match 1 document @@ -1438,46 +1483,51 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "efSearch": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } +(3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "efSearch": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) +(34 rows) ROLLBACK; -- hnsw filter number, with $and, default efSearch, match 1 document @@ -1491,83 +1541,92 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 16 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 16, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) +(31 rows) ROLLBACK; --- hnsw filter number, with $or, efSearch = 1, match 1 document +-- hnsw filter number, with $or, efSearch = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "efSearch": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } +(3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "efSearch": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) +(34 rows) ROLLBACK; -- hnsw filter number, with $or, default efSearch, match 3 documents @@ -1583,36 +1642,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$eq": 1 } }, { "meta.b": { "$eq": 2 } } , { "meta.b": { "$eq": 5 } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 16 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 16, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw filter number: with $eq, $gt, $lt, $gte, $lte, $ne, $in, $and, $or @@ -1701,127 +1764,139 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 16 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 16, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- hnsw filter boolean, with efSearch = 1, match 1 document +-- hnsw filter boolean, with efSearch = 1, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true }, "efSearch": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } +(2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true }, "efSearch": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- TODO, current implementation is post-filtering, need to fix --- hnsw filter boolean, with c = false, efSearch = 1, match 0 document +-- hnsw filter boolean, with c = false, efSearch = 1, match 2 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "efSearch": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } + { "_id" : { "$numberInt" : "9" }, "searchScore" : { "$numberDouble" : "1203412.0" } } +(2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "efSearch": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- hnsw filter boolean, with c = false, efSearch = 3, match 1 document +-- hnsw filter boolean, with c = false, efSearch = 3, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -1829,40 +1904,44 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } -(1 row) + { "_id" : { "$numberInt" : "9" }, "searchScore" : { "$numberDouble" : "1203412.0" } } +(2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "efSearch": 3 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 3 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw search: filter boolean, with $eq, $ne, $in, $nin, $and, $or @@ -1938,35 +2017,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$regex": "^some", "$options" : "i"}}, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector))) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector))), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index_cos on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index_cos on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) +(29 rows) ROLLBACK; -- hnsw search: inner product @@ -1992,35 +2074,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$regex": "^some", "$options" : "i"}}, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector))) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector))), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index_ip on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index_ip on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) +(29 rows) ROLLBACK; -- hnsw search: restore to euclidean distance @@ -2053,37 +2138,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a.b": 3 }, "efSearch": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw search: with filter and score projection, efSearch = 4, match 1 document @@ -2101,131 +2189,144 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "efSearch": 4 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "4" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 4 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "4" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "4" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) - -> Bitmap Index Scan on "documentIndex_meta" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) + -> Bitmap Index Scan on "documentIndex_meta" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- hnsw search: with filter and score projection, efSearch = 3, no match document +-- hnsw search: with filter and score projection, efSearch = 3, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 3 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "9" }, "rank" : { "$numberDouble" : "1203412.0" } } +(1 row) ROLLBACK; BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 3 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 3 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- hnsw search: with filter and score projection, $ne, efSearch = 1, no match document +-- hnsw search: with filter and score projection, $ne, efSearch = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$ne": "some sentence"}}, "efSearch": 1 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "7" }, "rank" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "rank" : { "$numberDouble" : "1004092.0" } } + { "_id" : { "$numberInt" : "9" }, "rank" : { "$numberDouble" : "1203412.0" } } +(3 rows) ROLLBACK; BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$ne": "some sentence"}}, "efSearch": 1 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(31 rows) ROLLBACK; BEGIN; @@ -2243,37 +2344,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.b": { "$gte": 2 } }, "efSearch": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; BEGIN; @@ -2291,37 +2395,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"c": { "$eq": false}}, "efSearch": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8202_8200036 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8202'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8202_8200036 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8202_8200036 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- hnsw search: with filter and score projection, $or, multiple filters @@ -2329,10 +2436,12 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 10, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence" } }, { "meta.b": { "$gt": 2 } }, {"c": { "$eq": false } } ] }, "efSearch": 1 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "rank" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "8" }, "rank" : { "$numberDouble" : "1004092.0" } } + { "_id" : { "$numberInt" : "9" }, "rank" : { "$numberDouble" : "1203412.0" } } +(3 rows) ROLLBACK; --------------------------------------------------------------------- @@ -2670,7 +2779,7 @@ SELECT drop_collection('db','aggregation_pipeline_empty_vector'); t (1 row) -DROP ROLE IF EXISTS test_filter_user; +DROP ROLE IF EXISTS test_filter_user_hnsw; --------------------------------------------------------------------- -- exact search SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_vector_hnsw_exact', '{ "_id": 6, "meta":{ "a": "some sentence", "b": 1 }, "c": true , "v": [3.0, 5.0, 1.1 ] }'); @@ -3350,11 +3459,18 @@ SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_hnsw_halfvec', '{ " { "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } } (1 row) +SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "meta.a": 1 }, "name": "idx_meta.a" } ] }', true); + create_indexes_non_concurrently +--------------------------------------------------------------------- + { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "0" }, "numIndexesAfter" : { "$numberInt" : "1" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } +(1 row) + +ANALYZE; -- check half vector search, L2 similarity SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "half_index", "cosmosSearchOptions": { "kind": "vector-hnsw", "m": 16, "efConstruction": 64, "similarity": "L2", "dimensions": 3, "compression": "half" } } ] }', true); create_indexes_non_concurrently --------------------------------------------------------------------- - { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "0" }, "numIndexesAfter" : { "$numberInt" : "1" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } + { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "1" }, "numIndexesAfter" : { "$numberInt" : "2" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } (1 row) -- oversampling, error cases @@ -3441,22 +3557,65 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) <-> '[3,4.8984375,1]'::halfvec) (22 rows) +ROLLBACK; +-- filter +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } +(3 rows) + +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" + Node: host=localhost port=58070 dbname=regression + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Sort Key: ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Subquery Scan on agg_stage_0_1 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + -> Limit + Output: collection_1.document, ((vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.8984375,1]'::halfvec)) + -> Nested Loop + Output: collection_1.document, ((vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.8984375,1]'::halfvec)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.8984375,1]'::halfvec), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using half_index on documentdb_data.documents_8207_8200135 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.8984375,1]'::halfvec) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize + Output: collection.ctid + -> Bitmap Heap Scan on documentdb_data.documents_8207_8200135 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(31 rows) + ROLLBACK; CALL documentdb_api.drop_indexes('db', '{ "dropIndexes": "aggregation_pipeline_hnsw_halfvec", "index": "half_index"}'); retval --------------------------------------------------------------------- - { "ok" : true, "nIndexesWas" : { "$numberLong" : "1" } } + { "ok" : true, "nIndexesWas" : { "$numberLong" : "2" } } (1 row) -- check filter with half vector, COS similarity SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "half_index", "cosmosSearchOptions": { "kind": "vector-hnsw", "m": 16, "efConstruction": 64, "similarity": "COS", "dimensions": 3, "compression": "half" } } ] }', true); create_indexes_non_concurrently ---------------------------------------------------------------------- - { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "0" }, "numIndexesAfter" : { "$numberInt" : "1" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } -(1 row) - -SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "meta.a": 1 }, "name": "idx_meta.a" } ] }', true); - create_indexes_non_concurrently --------------------------------------------------------------------- { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "1" }, "numIndexesAfter" : { "$numberInt" : "2" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } (1 row) @@ -3465,21 +3624,23 @@ ANALYZE; BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; -SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "99986.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "88331.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "78296.0" } } +(3 rows) -EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); - QUERY PLAN +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" + Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" Node: host=localhost port=58070 dbname=regression -> Sort Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)))), ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) @@ -3493,18 +3654,18 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } -> Index Scan using half_index on documentdb_data.documents_8207_8200135 collection_1 Output: collection_1.document, collection_1.ctid Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) -> Materialize Output: collection.ctid -> Bitmap Heap Scan on documentdb_data.documents_8207_8200135 collection Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) (31 rows) ROLLBACK; @@ -3519,14 +3680,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "efSearch": 100, "oversampling": 1.5 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '6'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" LIMIT '4'::bigint + Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '6'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" LIMIT '4'::bigint Node: host=localhost port=58070 dbname=regression -> Limit Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)))), ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) @@ -3542,11 +3703,11 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } -> Index Scan using half_index on documentdb_data.documents_8207_8200135 collection_1 Output: collection_1.document, collection_1.ctid Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) -> Materialize Output: collection.ctid -> Bitmap Heap Scan on documentdb_data.documents_8207_8200135 collection @@ -3646,6 +3807,55 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) (20 rows) +ROLLBACK; +-- filter +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); + document +--------------------------------------------------------------------- + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "6360000.0" } } + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "4860000.0" } } + { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "3460000.0" } } +(3 rows) + +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$lte" : "some sentence" } }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8207_8200135 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8207'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" + Node: host=localhost port=58070 dbname=regression + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)))), ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) + Sort Key: ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) + -> Subquery Scan on agg_stage_0_1 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector))), (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector) + -> Limit + Output: collection_1.document, ((vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.8984375,1]'::halfvec)) + -> Nested Loop + Output: collection_1.document, ((vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.8984375,1]'::halfvec)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.8984375,1]'::halfvec), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using half_index on documentdb_data.documents_8207_8200135 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.8984375,1]'::halfvec) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize + Output: collection.ctid + -> Bitmap Heap Scan on documentdb_data.documents_8207_8200135 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(31 rows) + ROLLBACK; -- check value of vector is out of range -- create index failed diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw_planner.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw_planner.out index 9ace4032..e8bc5e50 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw_planner.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_hnsw_planner.out @@ -96,9 +96,9 @@ EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bs Filter: bson_search_param(document, '{ "efSearch" : { "$numberInt" : "1" } }'::bson) (7 rows) -SELECT batch_insert_testing_vector_documents_hnsw_planner('agg_vector_hnsw_planner', 1, 100, 10); +SELECT batch_insert_testing_vector_documents_hnsw_planner('agg_vector_hnsw_planner', 10, 100, 10); NOTICE: Inserting 100 documents into agg_vector_hnsw_planner -NOTICE: Begin id: 1, Batch size: 10, batch count: 10 +NOTICE: Begin id: 10, Batch size: 10, batch count: 10 batch_insert_testing_vector_documents_hnsw_planner --------------------------------------------------------------------- @@ -137,35 +137,39 @@ SELECT documentdb_api_internal.create_indexes_non_concurrently('vector_db', '{ " set documentdb.enableVectorPreFilter to on; EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('vector_db', '{ "aggregate": "agg_vector_hnsw_planner", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "efSearch": 1, "filter": { "a": { "$lt": "s" }} } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- - Subquery Scan on agg_stage_0 (actual rows=0 loops=1) - Output: bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (public.vector(bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector))) - -> Limit (actual rows=0 loops=1) - Output: collection_1.document, ((public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector)) - -> Nested Loop (actual rows=0 loops=1) + Sort (actual rows=0 loops=1) + Output: (bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (public.vector(bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector)))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Method: quicksort Memory: 25kB + -> Subquery Scan on agg_stage_0 (actual rows=0 loops=1) + Output: bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (public.vector(bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector))), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit (actual rows=0 loops=1) Output: collection_1.document, ((public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector)) - Join Filter: (collection_1.ctid = collection.ctid) - Rows Removed by Join Filter: 1 - -> Custom Scan (DocumentDBApiQueryScan) (actual rows=1 loops=1) - Output: collection_1.document, (public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 1 } - -> Index Scan using foo_1 on documentdb_data.documents_8401_8400002 collection_1 (actual rows=1 loops=1) - Output: collection_1.document, collection_1.ctid - Order By: (public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector) - Filter: bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" } }'::bson) - -> Materialize (actual rows=1 loops=1) - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8401_8400002 collection (actual rows=1 loops=1) + -> Nested Loop (actual rows=0 loops=1) + Output: collection_1.document, ((public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector)) + Join Filter: (collection_1.ctid = collection.ctid) + Rows Removed by Join Filter: 102 + -> Custom Scan (DocumentDBApiQueryScan) (actual rows=102 loops=1) + Output: collection_1.document, (public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using foo_1 on documentdb_data.documents_8401_8400002 collection_1 (actual rows=102 loops=1) + Output: collection_1.document, collection_1.ctid + Order By: (public.vector(bson_extract_vector(collection_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector) + Filter: bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::bson) + -> Materialize (actual rows=1 loops=102) Output: collection.ctid - Filter: (collection.document @< '{ "a" : "s" }'::bson) - Rows Removed by Filter: 99 -(21 rows) + -> Seq Scan on documentdb_data.documents_8401_8400002 collection (actual rows=1 loops=1) + Output: collection.ctid + Filter: (collection.document @< '{ "a" : "s" }'::bson) + Rows Removed by Filter: 104 +(25 rows) set documentdb.enableVectorPreFilterV2 to on; -- This is currently a post-filter and will not use the filter index. EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bson_aggregation_pipeline('vector_db', '{ "aggregate": "agg_vector_hnsw_planner", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "efSearch": 1, "filter": { "a": { "$lt": "s" }} } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Sort (actual rows=0 loops=1) Sort Key: (((public.vector(bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector) + '0'::double precision)) @@ -173,15 +177,15 @@ EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bs -> Subquery Scan on agg_stage_0 (actual rows=0 loops=1) -> Limit (actual rows=0 loops=1) -> Custom Scan (DocumentDBApiQueryScan) (actual rows=0 loops=1) - CosmosSearch Custom Params: { "efSearch" : 1 } + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } -> Index Scan using foo_1 on documents_8401_8400002 collection (actual rows=0 loops=1) Order By: (public.vector(bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector) - Filter: ((document @< '{ "a" : "s" }'::bson) AND bson_search_param(document, '{ "efSearch" : { "$numberInt" : "1" } }'::bson)) - Rows Removed by Filter: 1 + Filter: ((document @< '{ "a" : "s" }'::bson) AND bson_search_param(document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::bson)) + Rows Removed by Filter: 102 (11 rows) EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bson_aggregation_pipeline('vector_db', '{ "aggregate": "agg_vector_hnsw_planner", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "efSearch": 1, "filter": { "_id": { "$lt": 5 }} } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Sort (actual rows=0 loops=1) Sort Key: (((public.vector(bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector) + '0'::double precision)) @@ -189,10 +193,10 @@ EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bs -> Subquery Scan on agg_stage_0 (actual rows=0 loops=1) -> Limit (actual rows=0 loops=1) -> Custom Scan (DocumentDBApiQueryScan) (actual rows=0 loops=1) - CosmosSearch Custom Params: { "efSearch" : 1 } + CosmosSearch Custom Params: { "efSearch" : 1, "iterativeScan" : "relaxed_order" } -> Index Scan using foo_1 on documents_8401_8400002 collection (actual rows=0 loops=1) Order By: (public.vector(bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) '[3,4.9,1]'::public.vector) - Filter: ((document @< '{ "_id" : { "$numberInt" : "5" } }'::bson) AND (object_id < '{ "" : { "$numberInt" : "5" } }'::bson) AND bson_search_param(document, '{ "efSearch" : { "$numberInt" : "1" } }'::bson)) - Rows Removed by Filter: 1 + Filter: ((document @< '{ "_id" : { "$numberInt" : "5" } }'::bson) AND (object_id < '{ "" : { "$numberInt" : "5" } }'::bson) AND bson_search_param(document, '{ "efSearch" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::bson)) + Rows Removed by Filter: 102 (11 rows) diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out index 739c0fa3..322270ed 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_ivf.out @@ -1,6 +1,6 @@ -- init a test user and set vector pre-filtering on SELECT current_user as original_test_user \gset -CREATE ROLE test_filter_user WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; +CREATE ROLE test_filter_user_ivf WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; SET citus.next_shard_id TO 8100000; SET documentdb.next_collection_id TO 8100; @@ -213,7 +213,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio { "_id" : { "$numberInt" : "6" }, "a" : "some sentence", "v" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "5.0" }, { "$numberDouble" : "1.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "0.99986126308999445644" } } } (1 row) --- default nProbes = 1 +-- default nProbes = numLists(2) BEGIN; SET LOCAL enable_seqscan = off; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_vector", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v" } } } ], "cursor": {} }'); @@ -737,42 +737,45 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence" } }, { "meta.b": { "$gt": 2 } }, {"c": { "$eq": false } } ] }, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(33 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(36 rows) ROLLBACK; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] }, { "c": { "$eq": false } } ] } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -785,36 +788,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] }, { "c": { "$eq": false } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "c" : { "$eq" : false } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "c" : { "$eq" : false } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "c" : { "$eq" : false } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "c" : { "$eq" : false } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "c" : { "$eq" : false } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "c" : { "$eq" : false } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) - -> Bitmap Index Scan on "wildcardIndex" - Index Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) + -> Bitmap Index Scan on "wildcardIndex" + Index Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) +(31 rows) ROLLBACK; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "c": { "$eq": false } } ] }, { "meta.b": { "$lt": 5 } } ] } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -827,37 +834,41 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "c": { "$eq": false } } ] }, { "meta.b": { "$lt": 5 } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) - Filter: ((collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) + Filter: ((collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) +(32 rows) ROLLBACK; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.a": { "$regex": "^some", "$options" : "i" } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -871,45 +882,49 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.a": { "$regex": "^some", "$options" : "i" } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) - Filter: ((collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) + Filter: ((collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) +(32 rows) ROLLBACK; -- check the vector index is forced to be used -ALTER ROLE test_filter_user SET documentdb.enableVectorPreFilter = "True"; +ALTER ROLE test_filter_user_ivf SET documentdb.enableVectorPreFilter = "True"; SELECT current_setting('citus' || '.next_shard_id') as vector_citus__next_shard_id \gset SELECT current_setting('documentdb' || '.next_collection_id') as vector__next_collection_id \gset SELECT current_setting('documentdb' || '.next_collection_index_id') as vector__next_collection_index_id \gset -\c - test_filter_user +\c - test_filter_user_ivf SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); document @@ -918,35 +933,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(29 rows) -- default nProbes = numLists(4) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] } ] } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -956,8 +974,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } (2 rows) --- todo: add this test back when pgvector version upgrade is done. --- EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] } ] } } } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] } ] } } } } ], "cursor": {} }'); + QUERY PLAN +--------------------------------------------------------------------- + Custom Scan (Citus Adaptive) + Output: remote_scan.document + Task Count: 1 + Tasks Shown: All + -> Task + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) + Node: host=localhost port=58070 dbname=regression + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize + Output: collection.ctid + -> Seq Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) +(29 rows) + SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence"}}, { "meta.a": [ { "b" : 3 } ] } ] }, "nProbes": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); document --------------------------------------------------------------------- @@ -966,35 +1016,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence"}}, { "meta.a": [ { "b" : 3 } ] } ] }, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.a" : [ "some sentence", [ { "b" : { "$numberInt" : "3" } } ] ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.a" : [ "some sentence", [ { "b" : { "$numberInt" : "3" } } ] ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ "some sentence", [ { "b" : { "$numberInt" : "3" } } ] ] }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ "some sentence", [ { "b" : { "$numberInt" : "3" } } ] ] }'::documentdb_core.bson) +(29 rows) \c - :original_test_user SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; @@ -1029,37 +1082,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- "filter": {"meta": { "a" : [ { "b" : 3 } ] }, match with documentIndex_meta @@ -1073,37 +1129,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) - -> Bitmap Index Scan on "documentIndex_meta" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) + -> Bitmap Index Scan on "documentIndex_meta" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- "filter": {"meta.a.b": 3 }, match with idx_meta.a.b @@ -1117,37 +1176,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a.b": 3 }, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --------------------------------------------------------------------- @@ -1162,36 +1224,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": "some sentence"} } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : "some sentence" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf filter string: with $and, nProbes = 3, match 1 document @@ -1205,80 +1271,87 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 3 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 3 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- ivf filter string: with $and, nProbes = 1, no match document +-- ivf filter string: with $and, nProbes = 1, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } +(1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf filter string: with $or, nProbes = 3, match 3 documents @@ -1294,87 +1367,95 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 3 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 3 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(34 rows) ROLLBACK; --- ivf filter string: with $or, nProbes = 1, match 1 document +-- ivf filter string: with $or, nProbes = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } +(3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(34 rows) ROLLBACK; -- ivf filter string: with $or, $and, nProbes = 10, match 2 documents @@ -1389,41 +1470,44 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 10, "path": "v", "filter": {"$or":[{"$and":[{"meta.a":{"$gt":"other sentence"}},{"meta.a":{"$lt":"some sentence"}}]},{"meta.a":{"$in":[{"b":3},{"b":5}]}}]}, "nProbes": 10 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) OR (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_internal.##=) ANY (ARRAY['{ "meta.a" : { "b" : { "$numberInt" : "3" } } }'::documentdb_api_internal.bsonindexbounds, '{ "meta.a" : { "b" : { "$numberInt" : "5" } } }'::documentdb_api_internal.bsonindexbounds])))) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "10" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "$and" : [ { "meta.a" : { "$gt" : "other sentence" } }, { "meta.a" : { "$lt" : "some sentence" } } ] }, { "meta.a" : { "$in" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] } } ] }, "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.a" : "other sentence" }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery)) OR (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_internal.##=) ANY (ARRAY['{ "meta.a" : { "b" : { "$numberInt" : "3" } } }'::documentdb_api_internal.bsonindexbounds, '{ "meta.a" : { "b" : { "$numberInt" : "5" } } }'::documentdb_api_internal.bsonindexbounds])))) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 10 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "10" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 10, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "10" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) - Filter: (((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) -(32 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) + Filter: (((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.a" : "other sentence" }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.a" : "some sentence" }'::documentdb_core.bson)) OR (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.a" : { "min" : "other sentence", "max" : "some sentence", "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } }, { "b" : { "$numberInt" : "5" } } ] }'::documentdb_core.bson) +(35 rows) ROLLBACK; -- ivf filter string: with $eq, $gt, $lt, $gte, $lte, $ne, $in, $and, $or, $regex @@ -1503,79 +1587,87 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.b": 2 } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.b" : { "$numberInt" : "2" } } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- ivf filter number, with $and, nProbes = 1, no match document +-- ivf filter number, with $and, nProbes = 1, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "nProbes": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } +(1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "nProbes": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.b" : { "min" : { "$numberInt" : "1" }, "max" : { "$numberInt" : "5" }, "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.b" : { "min" : { "$numberInt" : "1" }, "max" : { "$numberInt" : "5" }, "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.b" : { "min" : { "$numberInt" : "1" }, "max" : { "$numberInt" : "5" }, "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<>) '{ "meta.b" : { "min" : { "$numberInt" : "1" }, "max" : { "$numberInt" : "5" }, "minInclusive" : false, "maxInclusive" : false } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf filter number, with $and, default nProbes, match 1 document @@ -1589,83 +1681,92 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$and" : [ { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: ((collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) +(31 rows) ROLLBACK; --- ivf filter number, with $or, nProbes = 1, match 1 document +-- ivf filter number, with $or, nProbes = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "nProbes": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } +(3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$gt": 1 } }, { "meta.b": { "$lt": 5 } } ] }, "nProbes": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$gt" : { "$numberInt" : "1" } } }, { "meta.b" : { "$lt" : { "$numberInt" : "5" } } } ] }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) -(31 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "1" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@<) '{ "meta.b" : { "$numberInt" : "5" } }'::documentdb_core.bson) +(34 rows) ROLLBACK; -- ivf filter number, with $or, default nProbes, match 3 documents @@ -1681,36 +1782,40 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (3 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.b": { "$eq": 1 } }, { "meta.b": { "$eq": 2 } } , { "meta.b": { "$eq": 5 } } ] } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "$or" : [ { "meta.b" : { "$eq" : { "$numberInt" : "1" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "2" } } }, { "meta.b" : { "$eq" : { "$numberInt" : "5" } } } ] } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_in(collection.document, '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@*=) '{ "meta.b" : [ { "$numberInt" : "1" }, { "$numberInt" : "2" }, { "$numberInt" : "5" } ] }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf filter number, with $eq, $gt, $lt, $gte, $lte, $ne, $in, $and, $or @@ -1799,127 +1904,139 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true } } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) -(27 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- ivf filter boolean, with nProbes = 1, match 1 document +-- ivf filter boolean, with nProbes = 1, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true }, "nProbes": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "searchScore" : { "$numberDouble" : "14142.0" } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "searchScore" : { "$numberDouble" : "508134.0" } } +(2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true }, "nProbes": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : true }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : true }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : true }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- TODO, current implementation is post-filtering, need to fix --- ivf filter boolean, with c = false, nProbes = 1, match 0 document +-- ivf filter boolean, with c = false, nProbes = 1, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "nProbes": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } + { "_id" : { "$numberInt" : "9" }, "searchScore" : { "$numberDouble" : "1203412.0" } } +(2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "nProbes": 1 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- ivf filter boolean, with c = false, nProbes = 3, match 1 document +-- ivf filter boolean, with c = false, nProbes = 3, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -1927,40 +2044,44 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "8" }, "searchScore" : { "$numberDouble" : "1004092.0" } } -(1 row) + { "_id" : { "$numberInt" : "9" }, "searchScore" : { "$numberDouble" : "1203412.0" } } +(2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "nProbes": 3 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "c" : false }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 3 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf filter boolean, with $eq, $ne, $in, $nin, $and, $or @@ -2060,35 +2181,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$regex": "^some", "$options" : "i"}}, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector))) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector))), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index_cos on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index_cos on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) +(29 rows) ROLLBACK; -- ivf search: inner product @@ -2139,35 +2263,38 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (2 rows) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$regex": "^some", "$options" : "i"}}, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, ('-1'::double precision OPERATOR(pg_catalog.*) (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<#>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_regex(collection.document, '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector))) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, ('-1'::double precision * (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector))), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index_ip on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Seq Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index_ip on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <#> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) -(26 rows) + -> Seq Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@~) '{ "meta.a" : { "$regularExpression" : { "pattern" : "^some", "options" : "i" } } }'::documentdb_core.bson) +(29 rows) ROLLBACK; -- ivf search: restore to euclidean distance @@ -2224,37 +2351,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a.b": 3 }, "nProbes": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a.b" : { "$numberInt" : "3" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a.b" : { "$numberInt" : "3" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf search: with filter and score projection, nProbes = 4, match 1 document @@ -2272,131 +2402,144 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "nProbes": 4 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "4" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }, "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 4 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "4" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 4, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "4" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) - -> Bitmap Index Scan on "documentIndex_meta" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) + -> Bitmap Index Scan on "documentIndex_meta" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta" : { "a" : [ { "b" : { "$numberInt" : "3" } } ] } }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- ivf search: with filter and score projection, nProbes = 3, no match document +-- ivf search: with filter and score projection, nProbes = 3, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 3 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "9" }, "rank" : { "$numberDouble" : "1203412.0" } } +(1 row) ROLLBACK; BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 3 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 3 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 3, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "3" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(31 rows) ROLLBACK; --- ivf search: with filter and score projection, $ne, nProbes = 1, no match document +-- ivf search: with filter and score projection, $ne, nProbes = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$ne": "some sentence"}}, "nProbes": 1 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- -(0 rows) + { "_id" : { "$numberInt" : "7" }, "rank" : { "$numberDouble" : "508134.0" } } + { "_id" : { "$numberInt" : "8" }, "rank" : { "$numberDouble" : "1004092.0" } } + { "_id" : { "$numberInt" : "9" }, "rank" : { "$numberDouble" : "1203412.0" } } +(3 rows) ROLLBACK; BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$ne": "some sentence"}}, "nProbes": 1 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$ne" : "some sentence" } }, "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (documentdb_api_catalog.bson_dollar_ne(collection.document, '{ "meta.a" : "some sentence" }'::documentdb_core.bson) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 1 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 1, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "1" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@!=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) +(31 rows) ROLLBACK; BEGIN; @@ -2414,37 +2557,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.b": { "$gte": 2 } }, "nProbes": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "meta.b" : { "$gte" : { "$numberInt" : "2" } } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>=) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) +(31 rows) ROLLBACK; BEGIN; @@ -2462,37 +2608,40 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"c": { "$eq": false}}, "nProbes": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "2" }, "path" : "v", "filter" : { "c" : { "$eq" : false } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '2'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(28 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(31 rows) ROLLBACK; -- ivf search: with filter and score projection, $or, multiple filters @@ -2512,42 +2661,45 @@ BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 10, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence" } }, { "meta.b": { "$gt": 2 } }, {"c": { "$eq": false } } ] }, "nProbes": 100 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "10" }, "path" : "v", "filter" : { "$or" : [ { "meta.a" : { "$eq" : "some sentence" } }, { "meta.b" : { "$gt" : { "$numberInt" : "2" } } }, { "c" : { "$eq" : false } } ] }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8102_8100032 collection WHERE (((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bsonquery) OR (collection.document OPERATOR(documentdb_api_catalog.#=) '{ "c" : false }'::documentdb_core.bsonquery)) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8102'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '10'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson)), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_dollar_project(documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), '{ "rank" : { "$round" : [ { "$multiply" : [ { "$meta" : "searchScore" }, { "$numberInt" : "100000" } ] } ] } }'::documentdb_core.bson, '{ "now" : NOW_SYS_VARIABLE }'::documentdb_core.bson), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } - -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Materialize - Output: collection.ctid - -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using ivf_index on documentdb_data.documents_8102_8100032 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Materialize Output: collection.ctid - Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) - -> BitmapOr - -> Bitmap Index Scan on "idx_meta.a" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) - -> Bitmap Index Scan on "numberIndex_meta.b" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) - -> Bitmap Index Scan on "boolIndex_c" - Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) -(33 rows) + -> Bitmap Heap Scan on documentdb_data.documents_8102_8100032 collection + Output: collection.ctid + Recheck Cond: ((collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) OR (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson)) + -> BitmapOr + -> Bitmap Index Scan on "idx_meta.a" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : "some sentence" }'::documentdb_core.bson) + -> Bitmap Index Scan on "numberIndex_meta.b" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@>) '{ "meta.b" : { "$numberInt" : "2" } }'::documentdb_core.bson) + -> Bitmap Index Scan on "boolIndex_c" + Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "c" : false }'::documentdb_core.bson) +(36 rows) ROLLBACK; --------------------------------------------------------------------- @@ -2841,7 +2993,7 @@ SELECT drop_collection('db','aggregation_pipeline_empty_vector'); t (1 row) -DROP ROLE IF EXISTS test_filter_user; +DROP ROLE IF EXISTS test_filter_user_ivf; --------------------------------------------------------------------- -- exact search SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_vector_ivf_exact', '{ "_id": 6, "meta":{ "a": "some sentence", "b": 1 }, "c": true , "v": [3.0, 5.0, 1.1 ] }'); @@ -3080,10 +3232,11 @@ ROLLBACK; BEGIN; SET LOCAL enable_seqscan = off; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_vector_ivf_exact", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "exact": false } } } ], "cursor": {} }'); - document + document --------------------------------------------------------------------- { "_id" : { "$numberInt" : "6" }, "meta" : { "a" : "some sentence", "b" : { "$numberInt" : "1" } }, "c" : true, "v" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "5.0" }, { "$numberDouble" : "1.1000000000000000888" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "0.14142130855867909855" } } } -(1 row) + { "_id" : { "$numberInt" : "7" }, "meta" : { "a" : "some other sentence", "b" : { "$numberInt" : "2" } }, "c" : true, "v" : [ { "$numberDouble" : "8.0" }, { "$numberDouble" : "5.0" }, { "$numberDouble" : "0.10000000000000000555" } ], "__cosmos_meta__" : { "score" : { "$numberDouble" : "5.0813383763359256662" } } } +(2 rows) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_vector_ivf_exact", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "nProbes": 1, "exact": false } } } ], "cursor": {} }'); document @@ -3706,14 +3859,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "nProbes": 100 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" + Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" Node: host=localhost port=58070 dbname=regression -> Sort Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)))), ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) @@ -3727,11 +3880,11 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } -> Index Scan using half_index on documentdb_data.documents_8107_8100132 collection_1 Output: collection_1.document, collection_1.ctid Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) -> Materialize Output: collection.ctid -> Bitmap Heap Scan on documentdb_data.documents_8107_8100132 collection @@ -3753,14 +3906,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "nProbes": 100, "oversampling": 1.5 } } } ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '6'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" LIMIT '4'::bigint + Query: SELECT document FROM (SELECT documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision OPERATOR(pg_catalog.-) (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)))) AS document, (public.vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "fullScoreVal" FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector_to_halfvec(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : { "$eq" : "some sentence" } }, "nProbes" : { "$numberInt" : "100" }, "oversampling" : { "$numberDouble" : "1.5" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8107_8100132 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : "some sentence" }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8107'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '6'::bigint) agg_stage_0_1) agg_stage_0 ORDER BY "fullScoreVal" LIMIT '4'::bigint Node: host=localhost port=58070 dbname=regression -> Limit Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0_1.document, ('1'::double precision - (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)))), ((vector(documentdb_api_internal.bson_extract_vector(agg_stage_0_1.document, 'v'::text), 3, true) <=> '[3,4.9,1]'::vector)) @@ -3776,11 +3929,11 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' Join Filter: (collection_1.ctid = collection.ctid) -> Custom Scan (DocumentDBApiQueryScan) Output: collection_1.document, (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec), collection_1.ctid - CosmosSearch Custom Params: { "nProbes" : 100 } + CosmosSearch Custom Params: { "nProbes" : 100, "iterativeScan" : "relaxed_order" } -> Index Scan using half_index on documentdb_data.documents_8107_8100132 collection_1 Output: collection_1.document, collection_1.ctid Order By: (vector_to_halfvec(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <=> '[3,4.8984375,1]'::halfvec) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" } }'::documentdb_core.bson) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "nProbes" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) -> Materialize Output: collection.ctid -> Bitmap Heap Scan on documentdb_data.documents_8107_8100132 collection diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_mongo.out b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_mongo.out index 59e94413..31e7abe3 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_mongo.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_pipeline_tests_vector_mongo.out @@ -264,33 +264,36 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio (1 row) EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_mongo_vector_filter", "pipeline": [ { "$vectorSearch": { "queryVector": [ 3.0, 4.9, 1.0 ], "limit": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "numCandidates": 100 }} ], "cursor": {} }'); - QUERY PLAN + QUERY PLAN --------------------------------------------------------------------- Custom Scan (Citus Adaptive) Output: remote_scan.document Task Count: 1 Tasks Shown: All -> Task - Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8301_8300030 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8301'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8301_8300030 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8301'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 + Query: SELECT documentdb_api_internal.bson_document_add_score_field(document, (public.vector(documentdb_api_internal.bson_extract_vector(document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) AS document FROM (SELECT c1.document, c1."orderVal" AS "sortScore" FROM ((SELECT collection.document, (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true)) AS "orderVal", collection.ctid FROM documentdb_data.documents_8301_8300030 collection WHERE ((collection.shard_key_value OPERATOR(pg_catalog.=) '8301'::bigint) AND documentdb_api_internal.bson_search_param(collection.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) AND (documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL)) ORDER BY (public.vector(documentdb_api_internal.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<->) public.vector(documentdb_api_internal.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "4" }, "path" : "v", "filter" : { "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }, "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson, 'vector'::text), 3, true))) c1 JOIN (SELECT collection.ctid FROM documentdb_data.documents_8301_8300030 collection WHERE ((collection.document OPERATOR(documentdb_api_catalog.#=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bsonquery) AND (collection.shard_key_value OPERATOR(pg_catalog.=) '8301'::bigint))) c2 ON ((c2.ctid OPERATOR(pg_catalog.=) c1.ctid))) ORDER BY c1."orderVal" LIMIT '4'::bigint) agg_stage_0 ORDER BY ("sortScore" OPERATOR(pg_catalog.+) '0'::double precision) Node: host=localhost port=58070 dbname=regression - -> Subquery Scan on agg_stage_0 - Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Limit - Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - -> Nested Loop + -> Sort + Output: (documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector))), ((agg_stage_0."sortScore" + '0'::double precision)) + Sort Key: ((agg_stage_0."sortScore" + '0'::double precision)) + -> Subquery Scan on agg_stage_0 + Output: documentdb_api_internal.bson_document_add_score_field(agg_stage_0.document, (vector(documentdb_api_internal.bson_extract_vector(agg_stage_0.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)), (agg_stage_0."sortScore" + '0'::double precision) + -> Limit Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) - Join Filter: (collection_1.ctid = collection.ctid) - -> Custom Scan (DocumentDBApiQueryScan) - Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid - CosmosSearch Custom Params: { "efSearch" : 100 } - -> Index Scan using hnsw_index on documentdb_data.documents_8301_8300030 collection_1 - Output: collection_1.document, collection_1.ctid - Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) - Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" } }'::documentdb_core.bson) - -> Seq Scan on documentdb_data.documents_8301_8300030 collection - Output: collection.ctid - Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) -(24 rows) + -> Nested Loop + Output: collection_1.document, ((vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector)) + Join Filter: (collection_1.ctid = collection.ctid) + -> Custom Scan (DocumentDBApiQueryScan) + Output: collection_1.document, (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector), collection_1.ctid + CosmosSearch Custom Params: { "efSearch" : 100, "iterativeScan" : "relaxed_order" } + -> Index Scan using hnsw_index on documentdb_data.documents_8301_8300030 collection_1 + Output: collection_1.document, collection_1.ctid + Order By: (vector(documentdb_api_internal.bson_extract_vector(collection_1.document, 'v'::text), 3, true) <-> '[3,4.9,1]'::vector) + Filter: documentdb_api_internal.bson_search_param(collection_1.document, '{ "efSearch" : { "$numberInt" : "100" }, "iterativeScan" : "relaxed_order" }'::documentdb_core.bson) + -> Seq Scan on documentdb_data.documents_8301_8300030 collection + Output: collection.ctid + Filter: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "meta.a" : [ { "b" : { "$numberInt" : "3" } } ] }'::documentdb_core.bson) +(27 rows) SELECT documentdb_api.shard_collection('db','aggregation_pipeline_mongo_vector_filter', '{"_id":"hashed"}', false); shard_collection diff --git a/internal/pg_documentdb_distributed/src/test/regress/expected/commands_create_indexes_background.out b/internal/pg_documentdb_distributed/src/test/regress/expected/commands_create_indexes_background.out index 16dee10f..8b7b8cf6 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/expected/commands_create_indexes_background.out +++ b/internal/pg_documentdb_distributed/src/test/regress/expected/commands_create_indexes_background.out @@ -733,9 +733,19 @@ CALL documentdb_distributed_test_helpers.create_indexes_background( (1 row) -- test skippable error test --- todo: add this test back when pgvector version upgrade is done. --- CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 100, "similarity": "COS", "dimensions": 2001 } } ] }'); --- CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-hnsw", "similarity": "COS", "dimensions": 2001 } } ] }'); +CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 100, "similarity": "COS", "dimensions": 2001 } } ] }'); +NOTICE: creating collection + retval | ok +--------------------------------------------------------------------- + { "raw" : { "defaultShard" : { "ok" : { "$numberInt" : "0" }, "errmsg" : "column cannot have more than 2000 dimensions for ivfflat index", "code" : { "$numberInt" : "261" } } }, "ok" : { "$numberInt" : "0" } } | f +(1 row) + +CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-hnsw", "similarity": "COS", "dimensions": 2001 } } ] }'); + retval | ok +--------------------------------------------------------------------- + { "raw" : { "defaultShard" : { "ok" : { "$numberInt" : "0" }, "errmsg" : "column cannot have more than 2000 dimensions for hnsw index", "code" : { "$numberInt" : "261" } } }, "ok" : { "$numberInt" : "0" } } | f +(1 row) + -- test another skippable error SELECT documentdb_api.insert_one('db','LargeKeySize', '{"_id": 1, "a" : [ "p86PXqGpykgF4prF9X0ouGVG7mkRGg9ZyXZwt25L49trG5IQXgrHdkR01ZKSBlZv8SH04XUych6c0HNvS6Dn17Rx3JgKYMFqxSTyhRZLdlmMgxCMBE7wYDrvtfCPRgeDPGOR4FTVP0521ulBAnAdoxub6WndMqLhGZKymTmmjdCJ28ka0fnN47BeMPy9jqllCfuWh6MUg4bSRrvkopOHgkXx0Jdg7KwLsvfozsoVzKVI1BYlV6ruz1QipKZTRgAJVUYEzG9qA28ZBFu6ZLN0vzeXpPlxNbPu4pwLcPISxCNmgIeDm397Bs9krq0OCqhqhVJzYaLLmcTipuqi0mmPYNBF5RYfiEWeLmarJzW4oirL1s6d8EMGqvlRW5M6GY5OCUo5BfvADGpa8VQ3dGF1GKs05U7IWax6am9TOuq6hXWuhw3r8pmv0OvhqNmh0Wm9rAoK2zEHnuwoUqh57McwWx5gvouiZQdgDrRlU0IAvv4wpPwtgoAZpNje9ZPNywbrdYkKdy3CPGDf4bhreNvnOVx2aIfY3ZlQCL8RWXjIN1HWhb8nRTZuaqJVDh8lnB8kessHCrS0tTLEcnjZIRVPXge5F3AD2x4PYrP1jthursnY6XqqzZVvN2PEia9AXyqvAi8447AONDn27AqUEDRCVBg8l4DzbZ7O2OUOyG3nBE78xDdQMbpkhmPF0MPiihgZtcPLxB4E36I5Kr1g0ecmX6XsFN2FFDDHg0R8oi120fnm33UWWwpfM13czkJKzkGucSDv0NO0nrmd0yxlTwLCwYg3IOP62pyUFfZNj755sbXigEajKcypSgNdCcVJ8fak9xhe575FmcA1LSr8qOKKCyy3bZdyFKuwDtAtCOrlp2Ay5qtqIJhovZp3ek6U1ZKlLAXPf5Xk661XHOLuFNExn7vyMxeFKb9v2EWmdrO622ylfc0xnGnOc2yT7lAE7w1x4AGxBdgjI3q052o4gWfALRDjbhEK39sM2rpTIwMrvsSo35rsv5p9mQ2zQCL1OUBHQHmDFEzfH7zanSgISWGYjdbpGoyo3JxhcypqUxx7jB4DIe20i3fGbdpFOKVirFjZ4LyfaDgGWGaW6eD7XyAigTuajLe5NpXBJOAQr31C8YYl1XlWZSBv8jdOjpLE8BixgHsXTldxzh7QxPQH2eKO74FeDySOZCNlsHENbQHbwfQJRxz33oHpr0dsNRn2AYCP6mOFJ8G9AASMx6jSP5j2ZRZGFb6GKhG2FyiIklRTPvtu14aMPTgD0tPBlXEWwi6IuynzrXXzOY3BodDRk2EwkruuuuEwCuSd82HJDuChSWf2A9duqv9J0UrbYuXN9NWOVypBq4tlY6joGUPe509Z4EQEKAmFIUV0GBQixzt1tPVgBs9esNvNFSftzKgYBS4FJe489UWwaX3njm5uQYW7wFJuqcI4A0MrfOYJSgJwzvtfaKwu95yAtjW7QgLPG355RkKsjZDLZdjuNvjN17yYC0xchIaBGaK6cIGDjRV2mnmKHaaLRrEwjKqS1F1FCH19JnXSB1OoDdcoEYAbQwK2Kd0KWKh6tslohWoPWvFlOXBzZOEnPNENpU8xxD8mHxYDr8siiaknwRMvqf4yP2Oe4v7GFwOrMmR6UJknUu7xp2HvsjqceO5g7nkuUyiec4lk1sPraMygpBAboLCB3S4qSfNqtfJj0vwZVXmmK3lJyrh2dgJ7botypjDE1ENq6vovXKtZ7OYfE81V2ic5qnSKakbwmsiS3uywVjuvFtDBwgZQBhEMqcG6txa9qNINGA5Xbt1xnhJbFuxaykJpBTGtYAem00AX5ZRTVPy57WRue1aIvopDx92yL2Lw3eWSebATumO91PYlVDUhHzaQ232MS2hrHbKZYWguCb9UU3Wrsu9f5dybJDhJhE7jOnDb4hYJvdxQH8Ni7cELn4bf0AxXTQ74RJM2ZiPCAG8CYtpDcaHnU4BEfs5stjBOX1rWgQihsz5fbCEZOcDYJ5om5Gwk3R12Q49Ly3IARzsVTZnswxfpOfq6bIY9oKLpYanNDaf9ZhZEaa2MwOA9Ruiy4RmquYoJ92gBxS1l1FC8jYlCEfvSAkylhSA54TsWVVIwsOZd6Hj3RQQZtQcJEIMIHPxdTNUx1sSMCItVbTikTw8gviNtI1UM6VOJxiqBsdOmfy8WbfP7djRbRUBwCnCFoaEIFrAQfebevG6hxQ6MbeVP17ythvVUlDSJL2Bn7KmwNBj7Aido8RmBUPXuTxZzMpuLjn1q0Wm4FMz232XddybeBnELMMDkEWUqPL94xo1AdfhtXQ2WhpIJKHsSqj6vv51PnmfzmHZaqXkWIY6WhCf7SsBMojqBUEEI3VYKxcQ5IBzvX284CrH5x2M6AoGpANFp036l6cor9VBVYHyXODCBO1ACMDe9YENwatNgWhpiu2W6Ao1jE5vs00Vk9j4gY9NFPNrjpw5gFgRdinELZAyd2akBSoYdXxBt9NtfVJEYl2OiblplIOgB7fi4HEho4JtNhmyS3P3BdlQkRAciVHfHKAOdi2dBZxxVFJjqBuVW4Svv6XJuYYLPMJiPGrgV3rvlzlUdUAn0LKsga4BEn4cPoRnRPPYgj7L5bkqMxonzRiCBkMU4HTYBTrVfNqu7zHLcMQwc9lIEHYHDN2JyqEr0emG5B8NMqDJFwUHIILvA5pUuZQT5PV87QpLs8n24vV5YHqFDFm7KlGxan3Hdy5Zw4PaQsROlwIxFGFuHUXi6B4nn8KYZlULfAQ7stk4DDukrPmOXlbbDOhNHu2pXqejS12MTOYZ3" ] }'); NOTICE: creating collection diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql index 4f1371c6..c1a54dfa 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw.sql @@ -1,6 +1,6 @@ -- init a test user and set vector pre-filtering on SELECT current_user as original_test_user \gset -CREATE ROLE test_filter_user WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; +CREATE ROLE test_filter_user_hnsw WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; @@ -302,11 +302,11 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.a": { "$regex": "^some", "$options" : "i" } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); -- check the vector index is forced to be used -ALTER ROLE test_filter_user SET documentdb.enableVectorPreFilter = "True"; +ALTER ROLE test_filter_user_hnsw SET documentdb.enableVectorPreFilter = "True"; SELECT current_setting('citus' || '.next_shard_id') as vector_citus__next_shard_id \gset SELECT current_setting('documentdb' || '.next_collection_id') as vector__next_collection_id \gset SELECT current_setting('documentdb' || '.next_collection_index_id') as vector__next_collection_index_id \gset -\c - test_filter_user +\c - test_filter_user_hnsw SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -368,7 +368,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 3 } } } ], "cursor": {} }'); ROLLBACK; --- hnsw filter string: with $and, efSearch = 1, no match document +-- hnsw filter string: with $and, efSearch = 1, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -384,7 +384,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "efSearch": 3 } } } ], "cursor": {} }'); ROLLBACK; --- hnsw filter string: with $or, efSearch = 1, match 1 document +-- hnsw filter string: with $or, efSearch = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -440,7 +440,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } } ], "cursor": {} }'); ROLLBACK; --- hnsw filter number, with $or, efSearch = 1, match 1 document +-- hnsw filter number, with $or, efSearch = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -481,7 +481,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true } } } } ], "cursor": {} }'); ROLLBACK; --- hnsw filter boolean, with efSearch = 1, match 1 document +-- hnsw filter boolean, with efSearch = 1, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -489,8 +489,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true }, "efSearch": 1 } } } ], "cursor": {} }'); ROLLBACK; --- TODO, current implementation is post-filtering, need to fix --- hnsw filter boolean, with c = false, efSearch = 1, match 0 document +-- hnsw filter boolean, with c = false, efSearch = 1, match 2 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -498,7 +497,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "efSearch": 1 } } } ], "cursor": {} }'); ROLLBACK; --- hnsw filter boolean, with c = false, efSearch = 3, match 1 document +-- hnsw filter boolean, with c = false, efSearch = 3, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -574,7 +573,7 @@ SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "efSearch": 4 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); ROLLBACK; --- hnsw search: with filter and score projection, efSearch = 3, no match document +-- hnsw search: with filter and score projection, efSearch = 3, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -587,7 +586,7 @@ SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "efSearch": 3 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); ROLLBACK; --- hnsw search: with filter and score projection, $ne, efSearch = 1, no match document +-- hnsw search: with filter and score projection, $ne, efSearch = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -776,7 +775,7 @@ COMMIT; SELECT drop_collection('db','aggregation_pipeline_empty_vector'); -DROP ROLE IF EXISTS test_filter_user; +DROP ROLE IF EXISTS test_filter_user_hnsw; ---------------------------------------------------------------------------------------------------- @@ -957,6 +956,9 @@ SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_hnsw_halfvec', '{ " SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_hnsw_halfvec', '{ "_id": 9, "meta":{ "a" : [ { "b" : 3 } ] }, "c": false, "v": [15.0, 5.0, 0.1 ] }'); SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_hnsw_halfvec', '{ "_id": 10, "meta":{ "a" : [ { "b" : 5 } ] }, "c": false }'); +SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "meta.a": 1 }, "name": "idx_meta.a" } ] }', true); +ANALYZE; + -- check half vector search, L2 similarity SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "half_index", "cosmosSearchOptions": { "kind": "vector-hnsw", "m": 16, "efConstruction": 64, "similarity": "L2", "dimensions": 3, "compression": "half" } } ] }', true); @@ -979,18 +981,25 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "k": 5, "path": "v", "vector": [ 3.0, 4.9, 1.0 ], "oversampling": 1.5 } } } ], "cursor": {} }'); ROLLBACK; +-- filter +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); +ROLLBACK; + CALL documentdb_api.drop_indexes('db', '{ "dropIndexes": "aggregation_pipeline_hnsw_halfvec", "index": "half_index"}'); -- check filter with half vector, COS similarity SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "v": "cosmosSearch" }, "name": "half_index", "cosmosSearchOptions": { "kind": "vector-hnsw", "m": 16, "efConstruction": 64, "similarity": "COS", "dimensions": 3, "compression": "half" } } ] }', true); -SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "aggregation_pipeline_hnsw_halfvec", "indexes": [ { "key": { "meta.a": 1 }, "name": "idx_meta.a" } ] }', true); ANALYZE; BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; -SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); -EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); ROLLBACK; -- oversampling = 1.5 @@ -1026,6 +1035,14 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$eq": "some sentence"}}, "efSearch": 100, "exact": true, "oversampling": 1 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); ROLLBACK; +-- filter +BEGIN; +SET LOCAL enable_seqscan to off; +SET LOCAL documentdb.enableVectorPreFilter = on; +SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_hnsw_halfvec", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": { "$lte": "some sentence"}}, "efSearch": 100 } } } ], "cursor": {} }'); +ROLLBACK; + -- check value of vector is out of range -- create index failed SELECT documentdb_api.insert_one('db', 'aggregation_pipeline_hnsw_halfvec', '{ "_id": 22222, "meta":{ "a" : [ { "b" : 3 } ] }, "c": false, "larage_vector": [22222222222222.0, 5.0, 0.1 ] }'); diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw_planner.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw_planner.sql index e0df8dc6..c5010244 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw_planner.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_hnsw_planner.sql @@ -61,7 +61,7 @@ ANALYZE; set documentdb.enable_force_push_vector_index to on; EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bson_aggregation_pipeline('vector_db', '{ "aggregate": "agg_vector_hnsw_planner", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "efSearch": 1 } } } ], "cursor": {} }'); -SELECT batch_insert_testing_vector_documents_hnsw_planner('agg_vector_hnsw_planner', 1, 100, 10); +SELECT batch_insert_testing_vector_documents_hnsw_planner('agg_vector_hnsw_planner', 10, 100, 10); EXPLAIN (COSTS OFF, ANALYZE ON, SUMMARY OFF, TIMING OFF) SELECT document FROM bson_aggregation_pipeline('vector_db', '{ "aggregate": "agg_vector_hnsw_planner", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "efSearch": 1 } } } ], "cursor": {} }'); -- create indexes for vector filter diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql index 5be3245c..cf74beeb 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_pipeline_tests_vector_ivf.sql @@ -1,6 +1,6 @@ -- init a test user and set vector pre-filtering on SELECT current_user as original_test_user \gset -CREATE ROLE test_filter_user WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; +CREATE ROLE test_filter_user_ivf WITH LOGIN INHERIT SUPERUSER CREATEDB CREATEROLE IN ROLE :original_test_user; SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; @@ -125,7 +125,7 @@ $$; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_vector", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 1.1, "path": "v", "nProbes": 1 } } } ], "cursor": {} }'); SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_vector", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 1.8, "path": "v", "nProbes": 1 } } } ], "cursor": {} }'); --- default nProbes = 1 +-- default nProbes = numLists(2) BEGIN; SET LOCAL enable_seqscan = off; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_vector", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v" } } } ], "cursor": {} }'); @@ -304,11 +304,11 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' ROLLBACK; -- check the vector index is forced to be used -ALTER ROLE test_filter_user SET documentdb.enableVectorPreFilter = "True"; +ALTER ROLE test_filter_user_ivf SET documentdb.enableVectorPreFilter = "True"; SELECT current_setting('citus' || '.next_shard_id') as vector_citus__next_shard_id \gset SELECT current_setting('documentdb' || '.next_collection_id') as vector__next_collection_id \gset SELECT current_setting('documentdb' || '.next_collection_index_id') as vector__next_collection_index_id \gset -\c - test_filter_user +\c - test_filter_user_ivf SET search_path TO documentdb_core,documentdb_api,documentdb_api_catalog,documentdb_api_internal; SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); @@ -316,8 +316,7 @@ EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline(' -- default nProbes = numLists(4) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] } ] } } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); --- todo: add this test back when pgvector version upgrade is done. --- EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] } ] } } } } ], "cursor": {} }'); +EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] } ] } } } } ], "cursor": {} }'); SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence"}}, { "meta.a": [ { "b" : 3 } ] } ] }, "nProbes": 100 } } }, { "$project": {"searchScore": {"$round": [ {"$multiply": ["$__cosmos_meta__.score", 100000]}]} } } ], "cursor": {} }'); EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$eq": "some sentence"}}, { "meta.a": [ { "b" : 3 } ] } ] }, "nProbes": 100 } } } ], "cursor": {} }'); @@ -371,7 +370,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 3 } } } ], "cursor": {} }'); ROLLBACK; --- ivf filter string: with $and, nProbes = 1, no match document +-- ivf filter string: with $and, nProbes = 1, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -387,7 +386,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$or": [ { "meta.a": { "$gt": "other sentence" } }, { "meta.a": { "$lt": "some sentence" } } ] }, "nProbes": 3 } } } ], "cursor": {} }'); ROLLBACK; --- ivf filter string: with $or, nProbes = 1, match 1 document +-- ivf filter string: with $or, nProbes = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -428,7 +427,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"meta.b": 2 } } } } ], "cursor": {} }'); ROLLBACK; --- ivf filter number, with $and, nProbes = 1, no match document +-- ivf filter number, with $and, nProbes = 1, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -444,7 +443,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": {"$and": [ { "$or": [{ "meta.b": { "$eq": 2 } }, { "meta.b": { "$eq": 5 } } ] }, { "meta.b": { "$lt": 5 } } ] } } } } ], "cursor": {} }'); ROLLBACK; --- ivf filter number, with $or, nProbes = 1, match 1 document +-- ivf filter number, with $or, nProbes = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -485,7 +484,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true } } } } ], "cursor": {} }'); ROLLBACK; --- ivf filter boolean, with nProbes = 1, match 1 document +-- ivf filter boolean, with nProbes = 1, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -493,8 +492,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": true }, "nProbes": 1 } } } ], "cursor": {} }'); ROLLBACK; --- TODO, current implementation is post-filtering, need to fix --- ivf filter boolean, with c = false, nProbes = 1, match 0 document +-- ivf filter boolean, with c = false, nProbes = 1, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -502,7 +500,7 @@ SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregatio EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 4, "path": "v", "filter": { "c": false }, "nProbes": 1 } } } ], "cursor": {} }'); ROLLBACK; --- ivf filter boolean, with c = false, nProbes = 3, match 1 document +-- ivf filter boolean, with c = false, nProbes = 3, match 2 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -579,7 +577,7 @@ SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta": { "a" : [ { "b" : 3 } ] } }, "nProbes": 4 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); ROLLBACK; --- ivf search: with filter and score projection, nProbes = 3, no match document +-- ivf search: with filter and score projection, nProbes = 3, match 1 document BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -592,7 +590,7 @@ SET LOCAL documentdb.enableVectorPreFilter = on; EXPLAIN (COSTS OFF, VERBOSE ON) SELECT document FROM bson_aggregation_pipeline('db', '{ "aggregate": "aggregation_pipeline_ivf_filter", "pipeline": [ { "$search": { "cosmosSearch": { "vector": [ 3.0, 4.9, 1.0 ], "k": 2, "path": "v", "filter": {"meta.a": [ { "b" : 3 } ]}, "nProbes": 3 } } } , { "$project": { "rank": {"$round":[{"$multiply": [{"$meta": "searchScore" }, 100000]}]} } } ], "cursor": {} }'); ROLLBACK; --- ivf search: with filter and score projection, $ne, nProbes = 1, no match document +-- ivf search: with filter and score projection, $ne, nProbes = 1, match 3 documents BEGIN; SET LOCAL enable_seqscan to off; SET LOCAL documentdb.enableVectorPreFilter = on; @@ -795,7 +793,7 @@ COMMIT; SELECT drop_collection('db','aggregation_pipeline_empty_vector'); -DROP ROLE IF EXISTS test_filter_user; +DROP ROLE IF EXISTS test_filter_user_ivf; ---------------------------------------------------------------------------------------------------- diff --git a/internal/pg_documentdb_distributed/src/test/regress/sql/commands_create_indexes_background.sql b/internal/pg_documentdb_distributed/src/test/regress/sql/commands_create_indexes_background.sql index 339c8d55..8b701476 100644 --- a/internal/pg_documentdb_distributed/src/test/regress/sql/commands_create_indexes_background.sql +++ b/internal/pg_documentdb_distributed/src/test/regress/sql/commands_create_indexes_background.sql @@ -273,9 +273,8 @@ CALL documentdb_distributed_test_helpers.create_indexes_background( }' ); -- test skippable error test --- todo: add this test back when pgvector version upgrade is done. --- CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 100, "similarity": "COS", "dimensions": 2001 } } ] }'); --- CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-hnsw", "similarity": "COS", "dimensions": 2001 } } ] }'); +CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-ivf", "numLists": 100, "similarity": "COS", "dimensions": 2001 } } ] }'); +CALL documentdb_distributed_test_helpers.create_indexes_background('db', '{ "createIndexes": "ValidateVectorSearchAsync", "indexes": [ { "name": "largeVectorIndex", "key": { "largevector": "cosmosSearch"}, "cosmosSearchOptions": { "kind": "vector-hnsw", "similarity": "COS", "dimensions": 2001 } } ] }'); -- test another skippable error SELECT documentdb_api.insert_one('db','LargeKeySize', '{"_id": 1, "a" : [ "p86PXqGpykgF4prF9X0ouGVG7mkRGg9ZyXZwt25L49trG5IQXgrHdkR01ZKSBlZv8SH04XUych6c0HNvS6Dn17Rx3JgKYMFqxSTyhRZLdlmMgxCMBE7wYDrvtfCPRgeDPGOR4FTVP0521ulBAnAdoxub6WndMqLhGZKymTmmjdCJ28ka0fnN47BeMPy9jqllCfuWh6MUg4bSRrvkopOHgkXx0Jdg7KwLsvfozsoVzKVI1BYlV6ruz1QipKZTRgAJVUYEzG9qA28ZBFu6ZLN0vzeXpPlxNbPu4pwLcPISxCNmgIeDm397Bs9krq0OCqhqhVJzYaLLmcTipuqi0mmPYNBF5RYfiEWeLmarJzW4oirL1s6d8EMGqvlRW5M6GY5OCUo5BfvADGpa8VQ3dGF1GKs05U7IWax6am9TOuq6hXWuhw3r8pmv0OvhqNmh0Wm9rAoK2zEHnuwoUqh57McwWx5gvouiZQdgDrRlU0IAvv4wpPwtgoAZpNje9ZPNywbrdYkKdy3CPGDf4bhreNvnOVx2aIfY3ZlQCL8RWXjIN1HWhb8nRTZuaqJVDh8lnB8kessHCrS0tTLEcnjZIRVPXge5F3AD2x4PYrP1jthursnY6XqqzZVvN2PEia9AXyqvAi8447AONDn27AqUEDRCVBg8l4DzbZ7O2OUOyG3nBE78xDdQMbpkhmPF0MPiihgZtcPLxB4E36I5Kr1g0ecmX6XsFN2FFDDHg0R8oi120fnm33UWWwpfM13czkJKzkGucSDv0NO0nrmd0yxlTwLCwYg3IOP62pyUFfZNj755sbXigEajKcypSgNdCcVJ8fak9xhe575FmcA1LSr8qOKKCyy3bZdyFKuwDtAtCOrlp2Ay5qtqIJhovZp3ek6U1ZKlLAXPf5Xk661XHOLuFNExn7vyMxeFKb9v2EWmdrO622ylfc0xnGnOc2yT7lAE7w1x4AGxBdgjI3q052o4gWfALRDjbhEK39sM2rpTIwMrvsSo35rsv5p9mQ2zQCL1OUBHQHmDFEzfH7zanSgISWGYjdbpGoyo3JxhcypqUxx7jB4DIe20i3fGbdpFOKVirFjZ4LyfaDgGWGaW6eD7XyAigTuajLe5NpXBJOAQr31C8YYl1XlWZSBv8jdOjpLE8BixgHsXTldxzh7QxPQH2eKO74FeDySOZCNlsHENbQHbwfQJRxz33oHpr0dsNRn2AYCP6mOFJ8G9AASMx6jSP5j2ZRZGFb6GKhG2FyiIklRTPvtu14aMPTgD0tPBlXEWwi6IuynzrXXzOY3BodDRk2EwkruuuuEwCuSd82HJDuChSWf2A9duqv9J0UrbYuXN9NWOVypBq4tlY6joGUPe509Z4EQEKAmFIUV0GBQixzt1tPVgBs9esNvNFSftzKgYBS4FJe489UWwaX3njm5uQYW7wFJuqcI4A0MrfOYJSgJwzvtfaKwu95yAtjW7QgLPG355RkKsjZDLZdjuNvjN17yYC0xchIaBGaK6cIGDjRV2mnmKHaaLRrEwjKqS1F1FCH19JnXSB1OoDdcoEYAbQwK2Kd0KWKh6tslohWoPWvFlOXBzZOEnPNENpU8xxD8mHxYDr8siiaknwRMvqf4yP2Oe4v7GFwOrMmR6UJknUu7xp2HvsjqceO5g7nkuUyiec4lk1sPraMygpBAboLCB3S4qSfNqtfJj0vwZVXmmK3lJyrh2dgJ7botypjDE1ENq6vovXKtZ7OYfE81V2ic5qnSKakbwmsiS3uywVjuvFtDBwgZQBhEMqcG6txa9qNINGA5Xbt1xnhJbFuxaykJpBTGtYAem00AX5ZRTVPy57WRue1aIvopDx92yL2Lw3eWSebATumO91PYlVDUhHzaQ232MS2hrHbKZYWguCb9UU3Wrsu9f5dybJDhJhE7jOnDb4hYJvdxQH8Ni7cELn4bf0AxXTQ74RJM2ZiPCAG8CYtpDcaHnU4BEfs5stjBOX1rWgQihsz5fbCEZOcDYJ5om5Gwk3R12Q49Ly3IARzsVTZnswxfpOfq6bIY9oKLpYanNDaf9ZhZEaa2MwOA9Ruiy4RmquYoJ92gBxS1l1FC8jYlCEfvSAkylhSA54TsWVVIwsOZd6Hj3RQQZtQcJEIMIHPxdTNUx1sSMCItVbTikTw8gviNtI1UM6VOJxiqBsdOmfy8WbfP7djRbRUBwCnCFoaEIFrAQfebevG6hxQ6MbeVP17ythvVUlDSJL2Bn7KmwNBj7Aido8RmBUPXuTxZzMpuLjn1q0Wm4FMz232XddybeBnELMMDkEWUqPL94xo1AdfhtXQ2WhpIJKHsSqj6vv51PnmfzmHZaqXkWIY6WhCf7SsBMojqBUEEI3VYKxcQ5IBzvX284CrH5x2M6AoGpANFp036l6cor9VBVYHyXODCBO1ACMDe9YENwatNgWhpiu2W6Ao1jE5vs00Vk9j4gY9NFPNrjpw5gFgRdinELZAyd2akBSoYdXxBt9NtfVJEYl2OiblplIOgB7fi4HEho4JtNhmyS3P3BdlQkRAciVHfHKAOdi2dBZxxVFJjqBuVW4Svv6XJuYYLPMJiPGrgV3rvlzlUdUAn0LKsga4BEn4cPoRnRPPYgj7L5bkqMxonzRiCBkMU4HTYBTrVfNqu7zHLcMQwc9lIEHYHDN2JyqEr0emG5B8NMqDJFwUHIILvA5pUuZQT5PV87QpLs8n24vV5YHqFDFm7KlGxan3Hdy5Zw4PaQsROlwIxFGFuHUXi6B4nn8KYZlULfAQ7stk4DDukrPmOXlbbDOhNHu2pXqejS12MTOYZ3" ] }'); diff --git a/pg_documentdb/include/metadata/metadata_cache.h b/pg_documentdb/include/metadata/metadata_cache.h index 9fa79e61..7085134d 100644 --- a/pg_documentdb/include/metadata/metadata_cache.h +++ b/pg_documentdb/include/metadata/metadata_cache.h @@ -214,6 +214,7 @@ Oid Float8MultiplyOperatorId(void); /* Vector Functions */ Oid PgDoubleToVectorFunctionOid(void); +Oid PgDoubleToSparseVecFunctionOid(bool missingOK); Oid VectorAsVectorFunctionOid(void); Oid VectorAsHalfVecFunctionOid(bool missingOK); Oid ApiCatalogBsonExtractVectorFunctionId(void); diff --git a/pg_documentdb/include/vector/vector_common.h b/pg_documentdb/include/vector/vector_common.h index a2c5af93..1b51fba4 100644 --- a/pg_documentdb/include/vector/vector_common.h +++ b/pg_documentdb/include/vector/vector_common.h @@ -45,6 +45,10 @@ #define VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH "efSearch" #define VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH_STR_LEN 8 +/* Search parameter name for iterative scan mode */ +#define VECTOR_PARAMETER_NAME_ITERATIVE_SCAN "iterativeScan" +#define VECTOR_PARAMETER_NAME_ITERATIVE_SCAN_STR_LEN 13 + /* dynamic calculation of nprobes or efSearch depending on collection size */ #define VECTOR_SEARCH_SMALL_COLLECTION_ROWS 10000 #define VECTOR_SEARCH_1M_COLLECTION_ROWS 1000000 @@ -68,6 +72,11 @@ extern bool EnableVectorHNSWIndex; extern bool EnableVectorPreFilter; extern bool EnableVectorPreFilterV2; +/* + * GUC to set the iterative scan mode for pre-filtering + */ +extern int VectorPreFilterIterativeScanMode; + /* * GUC to enable vector compression feature for vector search. */ diff --git a/pg_documentdb/include/vector/vector_configs.h b/pg_documentdb/include/vector/vector_configs.h new file mode 100644 index 00000000..499c16cb --- /dev/null +++ b/pg_documentdb/include/vector/vector_configs.h @@ -0,0 +1,25 @@ +/*------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * + * include/vector/vector_configs.h + * + * Vector index configuration + * + *------------------------------------------------------------------------- + */ +#ifndef VECTOR_CONFIGS__H +#define VECTOR_CONFIGS__H + +/* Possible iterative scan modes for pre-filtering */ +typedef enum VectorIterativeScanMode +{ + VectorIterativeScan_OFF = 0, + + /* Relaxed allows results to be slightly out of order by distance, but provides better recall */ + VectorIterativeScan_RELAXED_ORDER = 1, + + /* Strict ensures results are in the exact order by distance */ + VectorIterativeScan_STRICT_ORDER = 2 +} VectorIterativeScanMode; + +#endif diff --git a/pg_documentdb/include/vector/vector_planner.h b/pg_documentdb/include/vector/vector_planner.h index 9a215cce..07e739ef 100644 --- a/pg_documentdb/include/vector/vector_planner.h +++ b/pg_documentdb/include/vector/vector_planner.h @@ -17,10 +17,9 @@ #include "vector/vector_utilities.h" -pgbson * CalculateSearchParamBsonForIndexPath(IndexPath *vectorSearchPath); +pgbson * CalculateSearchParamBsonForIndexPath(IndexPath *vectorSearchPath, + pgbson *searchParamBson); void SetSearchParametersToGUC(Oid vectorAccessMethodOid, pgbson *searchParamBson); -void TrySetDefaultSearchParamForCustomScan(SearchQueryEvalData *querySearchData); - #endif diff --git a/pg_documentdb/include/vector/vector_spec.h b/pg_documentdb/include/vector/vector_spec.h index 243f9196..039eef37 100644 --- a/pg_documentdb/include/vector/vector_spec.h +++ b/pg_documentdb/include/vector/vector_spec.h @@ -162,17 +162,13 @@ typedef Oid (*GetIndexAccessMethodOidFunc)(void); */ typedef void (*SetSearchParametersToGUCFunc)(const pgbson *searchParamBson); -/* - * Get the default SearchParamBson for the vector index. - */ -typedef pgbson *(*GetDefaultSearchParamBsonFunc)(void); - /* * Dynamic calculation of search parameters * based on the number of rows and index options. */ -typedef pgbson *(*CalculateSearchParamBsonFunc)(bytea *indexOptions, Cardinality - indexRows); +typedef pgbson *(*CalculateSearchParamBsonFunc)(bytea *indexOptions, + Cardinality indexRows, + pgbson *searchParamBson); /* * Extract the index compression type from the index options. @@ -201,8 +197,6 @@ typedef struct VectorIndexDefinition const char *indexAccessMethodName; - bool needsReorderAfterFilter; - ParseIndexCreationSpecFunc parseIndexCreationSpecFunc; GenerateIndexParamStringFunc generateIndexParamStrFunc; @@ -213,8 +207,6 @@ typedef struct VectorIndexDefinition SetSearchParametersToGUCFunc setSearchParametersToGUCFunc; - GetDefaultSearchParamBsonFunc getDefaultSearchParamBsonFunc; - CalculateSearchParamBsonFunc calculateSearchParamBsonFunc; ExtractIndexCompressionTypeFunc extractIndexCompressionTypeFunc; diff --git a/pg_documentdb/include/vector/vector_utilities.h b/pg_documentdb/include/vector/vector_utilities.h index cab0dddf..2a165242 100644 --- a/pg_documentdb/include/vector/vector_utilities.h +++ b/pg_documentdb/include/vector/vector_utilities.h @@ -14,7 +14,7 @@ #include #include "vector/vector_spec.h" - +#include "vector/vector_configs.h" /* * Type that holds data needed for diff --git a/pg_documentdb/src/aggregation/bson_aggregation_vector_search.c b/pg_documentdb/src/aggregation/bson_aggregation_vector_search.c index 28d809b9..127b7a2a 100644 --- a/pg_documentdb/src/aggregation/bson_aggregation_vector_search.c +++ b/pg_documentdb/src/aggregation/bson_aggregation_vector_search.c @@ -45,6 +45,7 @@ #include "utils/feature_counter.h" #include "utils/hashset_utils.h" #include "vector/vector_common.h" +#include "vector/vector_configs.h" #include "vector/vector_spec.h" #include "vector/vector_utilities.h" #include "api_hooks.h" @@ -77,7 +78,13 @@ typedef struct /* --------------------------------------------------------- */ /* Forward declaration */ /* --------------------------------------------------------- */ -static void AddSearchParamFunctionToQuery(Query *query, Oid accessMethodOid, +static bool IsPgvectorIterativeScanAvailable(); + +static pgbson * SetIterativeScanToSearchParam(pgbson *searchParamPgbson); + +static char * GetVectorIterativeScanModeName(VectorIterativeScanMode iterativeScanMode); + +static void AddSearchParamFunctionToQuery(Query *query, pgbson *searchParamPgbson); static Expr * AddScoreFieldToDocumentEntry(TargetEntry *documentEntry, @@ -89,9 +96,7 @@ static TargetEntry * AddCtidToQueryTargetList(Query *query, static Query * JoinVectorSearchQueryWithFilterQuery(Query *leftQuery, Query *rightQuery, TargetEntry *leftJoinEntry, - TargetEntry *rightJoinEntry, - const TargetEntry *sortEntry, - bool needsReordering); + TargetEntry *rightJoinEntry); static void AddPathStringToHashset(List *indexIdList, HTAB *stringHashSet); @@ -341,6 +346,69 @@ HandleMongoNativeVectorSearch(const bson_value_t *existingValue, Query *query, /* Private methods */ /* --------------------------------------------------------- */ +/* + * The pgvector iterative scan is available from 0.8.0, + */ +static bool +IsPgvectorIterativeScanAvailable() +{ + /* array_to_sparsevec function is introduced in 0.8.0 */ + /* so we can check the function is available or not to indicate the iterative scan is available */ + bool missingOK = true; + return OidIsValid(PgDoubleToSparseVecFunctionOid(missingOK)); +} + + +static pgbson * +SetIterativeScanToSearchParam(pgbson *searchParamPgbson) +{ + if (!IsPgvectorIterativeScanAvailable()) + { + return searchParamPgbson; + } + + pgbson_writer docWriter; + PgbsonWriterInit(&docWriter); + + if (searchParamPgbson != NULL) + { + PgbsonWriterConcat(&docWriter, searchParamPgbson); + } + + PgbsonWriterAppendUtf8(&docWriter, + VECTOR_PARAMETER_NAME_ITERATIVE_SCAN, + VECTOR_PARAMETER_NAME_ITERATIVE_SCAN_STR_LEN, + GetVectorIterativeScanModeName( + VectorPreFilterIterativeScanMode)); + + return PgbsonWriterGetPgbson(&docWriter); +} + + +static char * +GetVectorIterativeScanModeName(VectorIterativeScanMode iterativeScanMode) +{ + switch (iterativeScanMode) + { + case VectorIterativeScan_RELAXED_ORDER: + { + return "relaxed_order"; + } + + case VectorIterativeScan_STRICT_ORDER: + { + return "strict_order"; + } + + case VectorIterativeScan_OFF: + default: + { + break; + } + } + return "off"; +} + /* * Adds a wrapper function(bson_search_param) which includes the search parameters to the query. @@ -348,14 +416,13 @@ HandleMongoNativeVectorSearch(const bson_value_t *existingValue, Query *query, * In custom scan, we will dynamically calculate search param depend on the number of vectors in the collection. * * The example of the search param: - * ivfflat: { "nProbes": 4 } - * hnsw: { "efSearch": 16 } + * ivfflat: { "nProbes": 4, "iterativeScan": "strict_order"} + * hnsw: { "efSearch": 16, "iterativeScan": "relaxed_order" } * e.g. * WHERE bson_search_param(document, { "nProbes": 4 }) or WHERE bson_search_param(document, { "efSearch": 16 }) */ static void -AddSearchParamFunctionToQuery(Query *query, Oid accessMethodOid, - pgbson *searchParamPgbson) +AddSearchParamFunctionToQuery(Query *query, pgbson *searchParamPgbson) { if (searchParamPgbson != NULL) { @@ -431,9 +498,7 @@ AddCtidToQueryTargetList(Query *query, bool replaceTargetList) static Query * JoinVectorSearchQueryWithFilterQuery(Query *leftQuery, Query *rightQuery, TargetEntry *leftJoinEntry, - TargetEntry *rightJoinEntry, - const TargetEntry *sortEntry, - bool needsReordering) + TargetEntry *rightJoinEntry) { Query *finalQuery = makeNode(Query); finalQuery->commandType = CMD_SELECT; @@ -637,20 +702,20 @@ AddScoreFieldToDocumentEntry(TargetEntry *documentEntry, Expr *vectorSortExpr, }; /* Use expression_tree_mutator so we copy the orderby Expr before changing it */ - Expr *scoreExprInput = (Expr *) expression_tree_mutator((Node *) vectorSortExpr, - ReplaceDocumentVarOnSort, - &sortContext); + Expr *sortExprInput = (Expr *) expression_tree_mutator((Node *) vectorSortExpr, + ReplaceDocumentVarOnSort, + &sortContext); /* For half vector index, we need to use full vector to re-calculate the score */ List *orderArgs = NIL; - if (IsA(scoreExprInput, OpExpr)) + if (IsA(sortExprInput, OpExpr)) { - OpExpr *orderOpExpr = (OpExpr *) scoreExprInput; + OpExpr *orderOpExpr = (OpExpr *) sortExprInput; orderArgs = orderOpExpr->args; } - else if (IsA(scoreExprInput, FuncExpr)) + else if (IsA(sortExprInput, FuncExpr)) { - FuncExpr *orderFuncExpr = (FuncExpr *) scoreExprInput; + FuncExpr *orderFuncExpr = (FuncExpr *) sortExprInput; orderArgs = orderFuncExpr->args; } else @@ -693,22 +758,22 @@ AddScoreFieldToDocumentEntry(TargetEntry *documentEntry, Expr *vectorSortExpr, "unknown vector search operator type"))); } - if (IsA(scoreExprInput, FuncExpr)) + if (IsA(sortExprInput, FuncExpr)) { - FuncExpr *orderFuncExpr = (FuncExpr *) scoreExprInput; + FuncExpr *orderFuncExpr = (FuncExpr *) sortExprInput; orderFuncExpr->funcid = get_opcode(similarityOpOid); orderFuncExpr->args = newArgs; } else /* OpExpr */ { - OpExpr *orderOpExpr = (OpExpr *) scoreExprInput; + OpExpr *orderOpExpr = (OpExpr *) sortExprInput; orderOpExpr->opno = similarityOpOid; orderOpExpr->args = newArgs; } } /* Add the score field to the document */ - Expr *scoreExpr = GenerateScoreExpr((Expr *) scoreExprInput, distanceMetric); + Expr *scoreExpr = GenerateScoreExpr((Expr *) sortExprInput, distanceMetric); List *args = list_make2(documentEntry->expr, scoreExpr); FuncExpr *resultExpr = makeFuncExpr( @@ -717,7 +782,7 @@ AddScoreFieldToDocumentEntry(TargetEntry *documentEntry, Expr *vectorSortExpr, documentEntry->expr = (Expr *) resultExpr; - return scoreExprInput; + return sortExprInput; } @@ -1071,16 +1136,12 @@ GeneratePrefilteringVectorSearchQuery(Query *searchQuery, Query *joinedQuery = JoinVectorSearchQueryWithFilterQuery(searchQuery, filterQuery, leftCtidEntry, - rightCtidEntry, - sortEntry, - vectorSearchOptions-> - vectorIndexDef-> - needsReorderAfterFilter); + rightCtidEntry); /* Add the limit to the sub-query */ joinedQuery->limitCount = limitCount; - if (vectorSearchOptions->vectorIndexDef->needsReorderAfterFilter && + if (VectorPreFilterIterativeScanMode == VectorIterativeScan_RELAXED_ORDER && vectorSearchOptions->compressionType == VectorIndexCompressionType_None) { /* Search result by iterative search may be disordered, so we need to reorder the result */ @@ -1464,8 +1525,17 @@ HandleVectorSearchCore(Query *query, VectorSearchOptions *vectorSearchOptions, /* Create the WHERE bson_search_param(document, searchParamPgbson) and add it to the WHERE */ if (!vectorSearchOptions->exactSearch) { - AddSearchParamFunctionToQuery(query, vectorSearchOptions->vectorAccessMethodOid, - vectorSearchOptions->searchParamPgbson); + /* set iterative parameter for filtering */ + /* e.g. Search param: { "iterativeScan": "relaxed_order" } */ + bson_value_t filterBson = vectorSearchOptions->filterBson; + if (filterBson.value_type != BSON_TYPE_EOD && + !IsBsonValueEmptyDocument(&filterBson)) + { + vectorSearchOptions->searchParamPgbson = SetIterativeScanToSearchParam( + vectorSearchOptions->searchParamPgbson); + } + + AddSearchParamFunctionToQuery(query, vectorSearchOptions->searchParamPgbson); } else { @@ -1515,7 +1585,7 @@ HandleVectorSearchCore(Query *query, VectorSearchOptions *vectorSearchOptions, false, true); - Expr *scoreExpr = NULL; + Expr *sortExpr = NULL; /* If there's a filter, add it to the query */ if (EnableVectorPreFilterV2 && @@ -1547,10 +1617,10 @@ HandleVectorSearchCore(Query *query, VectorSearchOptions *vectorSearchOptions, * matching the filter. */ TargetEntry *documentEntry = linitial(query->targetList); - scoreExpr = AddScoreFieldToDocumentEntry(documentEntry, sortEntry->expr, - vectorSearchOptions->distanceMetric); + sortExpr = AddScoreFieldToDocumentEntry(documentEntry, sortEntry->expr, + vectorSearchOptions->distanceMetric); - TargetEntry *scoreEntry = makeTargetEntry(scoreExpr, 2, "sortVal", false); + TargetEntry *scoreEntry = makeTargetEntry(sortExpr, 2, "sortVal", false); query->targetList = lappend(query->targetList, scoreEntry); /* now reorder to ensure it matches the score order by */ @@ -1579,8 +1649,8 @@ HandleVectorSearchCore(Query *query, VectorSearchOptions *vectorSearchOptions, /* Add the score field */ TargetEntry *documentEntry = linitial(query->targetList); - scoreExpr = AddScoreFieldToDocumentEntry(documentEntry, sortEntry->expr, - vectorSearchOptions->distanceMetric); + sortExpr = AddScoreFieldToDocumentEntry(documentEntry, sortEntry->expr, + vectorSearchOptions->distanceMetric); } else { @@ -1592,18 +1662,18 @@ HandleVectorSearchCore(Query *query, VectorSearchOptions *vectorSearchOptions, /* Add the score field */ TargetEntry *documentEntry = linitial(query->targetList); - scoreExpr = AddScoreFieldToDocumentEntry(documentEntry, sortEntry->expr, - vectorSearchOptions->distanceMetric); + sortExpr = AddScoreFieldToDocumentEntry(documentEntry, sortEntry->expr, + vectorSearchOptions->distanceMetric); } /* Reorder for the compression index */ if (vectorSearchOptions->compressionType != VectorIndexCompressionType_None && - scoreExpr != NULL && + sortExpr != NULL && vectorSearchOptions->exactSearch == false) { /* reorder the results by the full vector distance */ /* Exact search doesn't need to reorder */ - query = ReorderResultsForCompression(query, context, scoreExpr); + query = ReorderResultsForCompression(query, context, sortExpr); } /* Add k limit to the top level query if oversampling is specified */ diff --git a/pg_documentdb/src/configs/system_configs.c b/pg_documentdb/src/configs/system_configs.c index 124b5e51..5deef783 100644 --- a/pg_documentdb/src/configs/system_configs.c +++ b/pg_documentdb/src/configs/system_configs.c @@ -13,12 +13,22 @@ #include #include #include "configs/config_initialization.h" +#include "vector/vector_configs.h" /* * Externally defined GUC constants * TODO(OSS): Move these as appropriate. */ +/* Enum values for iterative scan mode */ +static const struct config_enum_entry VECTOR_ITERATIVE_SCAN_OPTIONS[] = +{ + { "off", VectorIterativeScan_OFF, false }, + { "relaxed_order", VectorIterativeScan_RELAXED_ORDER, false }, + { "strict_order", VectorIterativeScan_STRICT_ORDER, false }, + { NULL, 0, false } +}; + /* * enable_create_collection_on_insert GUC determines whether * an insert into a non-existent collection should create a collection. @@ -109,6 +119,9 @@ int MaxAggregationStagesAllowed = DEFAULT_AGGREGATION_STAGES_LIMIT; #define DEFAULT_INDEX_TERM_COMPRESSION_THRESHOLD INT_MAX int IndexTermCompressionThreshold = DEFAULT_INDEX_TERM_COMPRESSION_THRESHOLD; +#define DEFAULT_VECTOR_ITERATIVE_SCAN_MODE VectorIterativeScan_RELAXED_ORDER +int VectorPreFilterIterativeScanMode = DEFAULT_VECTOR_ITERATIVE_SCAN_MODE; + void InitializeSystemConfigurations(const char *prefix, const char *newGucPrefix) { @@ -317,4 +330,14 @@ InitializeSystemConfigurations(const char *prefix, const char *newGucPrefix) DEFAULT_INDEX_TERM_COMPRESSION_THRESHOLD, 256, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomEnumVariable( + psprintf("%s.vectorPreFilterIterativeScanMode", newGucPrefix), + gettext_noop( + "Set the iterative scan mode for vector pre-filtering. " + "Relaxed order allows results to be slightly out of order by distance, but provides better recall. " + "Strict order ensures results are in the exact order by distance"), + NULL, &VectorPreFilterIterativeScanMode, DEFAULT_VECTOR_ITERATIVE_SCAN_MODE, + VECTOR_ITERATIVE_SCAN_OPTIONS, + PGC_USERSET, 0, NULL, NULL, NULL); } diff --git a/pg_documentdb/src/customscan/custom_query_scan.c b/pg_documentdb/src/customscan/custom_query_scan.c index b8d8691c..2b7ea603 100644 --- a/pg_documentdb/src/customscan/custom_query_scan.c +++ b/pg_documentdb/src/customscan/custom_query_scan.c @@ -191,26 +191,17 @@ AddExtensionQueryScanForVectorQuery(PlannerInfo *root, RelOptInfo *rel, inputState->querySearchData = *searchQueryData; inputState->hasVectorSearchData = true; - if (EnableVectorPreFilter) - { - bool failIfNotFound = true; - rel->pathlist = AddCustomPathForVectorCore(root, rel->pathlist, rel, inputState, - failIfNotFound); + bool failIfNotFound = true; + rel->pathlist = AddCustomPathForVectorCore(root, rel->pathlist, rel, inputState, + failIfNotFound); - if (rel->partial_pathlist != NIL) - { - failIfNotFound = false; - rel->partial_pathlist = AddCustomPathForVectorCore(root, - rel->partial_pathlist, rel, - inputState, - failIfNotFound); - } - } - else + if (rel->partial_pathlist != NIL) { - TrySetDefaultSearchParamForCustomScan(&(inputState->querySearchData)); - rel->pathlist = AddCustomPathCore(rel->pathlist, inputState); - rel->partial_pathlist = AddCustomPathCore(rel->partial_pathlist, inputState); + failIfNotFound = false; + rel->partial_pathlist = AddCustomPathForVectorCore(root, + rel->partial_pathlist, rel, + inputState, + failIfNotFound); } } @@ -351,14 +342,14 @@ AddCustomPathForVectorCore(PlannerInfo *planner, List *pathList, RelOptInfo *rel searchBson = DatumGetPgBson(queryState->querySearchData.SearchParamBson); } - if (searchBson == NULL || IsPgbsonEmptyDocument(searchBson)) - { - IndexPath *indexPath = (IndexPath *) vectorSearchPath; - - pgbson *defaultSearchParam = CalculateSearchParamBsonForIndexPath(indexPath); - - queryState->querySearchData.SearchParamBson = PointerGetDatum(defaultSearchParam); - } + /* For normal vector search, if the search param is not specified, searchBson will be NULL + * For filtering vector search, if the search param is not specified, searchBson contains the iterative param + * We let index specific handler to decide if the default search param is needed or not + */ + IndexPath *indexPath = (IndexPath *) vectorSearchPath; + pgbson *defaultSearchParam = CalculateSearchParamBsonForIndexPath(indexPath, + searchBson); + queryState->querySearchData.SearchParamBson = PointerGetDatum(defaultSearchParam); /* wrap the path in a custom path */ CustomPath *customPath = makeNode(CustomPath); diff --git a/pg_documentdb/src/metadata/metadata_cache.c b/pg_documentdb/src/metadata/metadata_cache.c index 6a08e6c4..87ab2fb2 100644 --- a/pg_documentdb/src/metadata/metadata_cache.c +++ b/pg_documentdb/src/metadata/metadata_cache.c @@ -598,6 +598,9 @@ typedef struct DocumentDBApiOidCacheData /* OID of the array_to_vector function. */ Oid PgDoubleToVectorFunctionOid; + /* OID of the array_to_sparsevec function. */ + Oid PgDoubleToSparseVecFunctionOid; + /* OID of the vector as vector Cast function */ Oid VectorAsVectorFunctionOid; @@ -2776,6 +2779,32 @@ PgDoubleToVectorFunctionOid(void) } +/* + * Returns the function Oid for converting a double[] to a sparse vector specifically the array_to_sparsevec function. + * Note: the array_to_sparsevec function is introduced in pgvector 0.8.0, + * Currently it is only used for checking if the version of pgvector is 0.8.0 + * Set missingOK to true, this function will return InvalidOid if the pgvector version is lower than that. + */ +Oid +PgDoubleToSparseVecFunctionOid(bool missingOK) +{ + InitializeDocumentDBApiExtensionCache(); + + if (Cache.PgDoubleToSparseVecFunctionOid == InvalidOid) + { + List *functionNameList = list_make2(makeString("public"), + makeString("array_to_sparsevec")); + + Oid paramOids[3] = { FLOAT8ARRAYOID, INT4OID, BOOLOID }; + + Cache.PgDoubleToSparseVecFunctionOid = + LookupFuncName(functionNameList, 3, paramOids, missingOK); + } + + return Cache.PgDoubleToSparseVecFunctionOid; +} + + /* * VectorAsVectorFunctionOid returns the OID of the vector as vector cast function. */ diff --git a/pg_documentdb/src/vector/vector_index_kind_impl.c b/pg_documentdb/src/vector/vector_index_kind_impl.c index 8b55e3e1..64b4c67f 100644 --- a/pg_documentdb/src/vector/vector_index_kind_impl.c +++ b/pg_documentdb/src/vector/vector_index_kind_impl.c @@ -28,6 +28,7 @@ #include "utils/feature_counter.h" #include "vector/vector_common.h" #include "vector/vector_spec.h" +#include "vector/vector_utilities.h" /* --------------------------------------------------------- */ @@ -90,13 +91,11 @@ static void SetIVFSearchParametersToGUC(const pgbson *searchParamBson); static void SetHNSWSearchParametersToGUC(const pgbson *searchParamBson); -static pgbson * GetIVFDefaultSearchParamBson(void); +static pgbson * CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows, + pgbson *searchParamBson); -static pgbson * GetHNSWDefaultSearchParamBson(void); - -static pgbson * CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows); - -static pgbson * CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows); +static pgbson * CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows, + pgbson *searchParamBson); static VectorIndexCompressionType ExtractIVFCompressionType(bytea *indexOptions); @@ -110,26 +109,22 @@ static VectorIndexDefinition VectorIndexDefinitionArray[] = { { .kindName = "vector-ivf", .indexAccessMethodName = "ivfflat", - .needsReorderAfterFilter = false, .parseIndexCreationSpecFunc = &ParseIVFCreationSpec, .generateIndexParamStrFunc = &GenerateIVFIndexParamStr, .parseIndexSearchSpecFunc = &ParseIVFIndexSearchSpec, .getIndexAccessMethodOidFunc = &PgVectorIvfFlatIndexAmId, .setSearchParametersToGUCFunc = &SetIVFSearchParametersToGUC, - .getDefaultSearchParamBsonFunc = &GetIVFDefaultSearchParamBson, .calculateSearchParamBsonFunc = &CalculateIVFSearchParamBson, .extractIndexCompressionTypeFunc = &ExtractIVFCompressionType }, { .kindName = "vector-hnsw", .indexAccessMethodName = "hnsw", - .needsReorderAfterFilter = false, .parseIndexCreationSpecFunc = &ParseHNSWCreationSpec, .generateIndexParamStrFunc = &GenerateHNSWIndexParamStr, .parseIndexSearchSpecFunc = &ParseHNSWIndexSearchSpec, .getIndexAccessMethodOidFunc = &PgVectorHNSWIndexAmId, .setSearchParametersToGUCFunc = &SetHNSWSearchParametersToGUC, - .getDefaultSearchParamBsonFunc = &GetHNSWDefaultSearchParamBson, .calculateSearchParamBsonFunc = &CalculateHNSWSearchParamBson, .extractIndexCompressionTypeFunc = &ExtractHNSWCompressionType }, @@ -221,12 +216,6 @@ RegisterVectorIndexExtension(const VectorIndexDefinition *extensibleDefinition) "setSearchParametersToGUCFunc is not defined for the vector index"))); } - if (extensibleDefinition->getDefaultSearchParamBsonFunc == NULL) - { - ereport(ERROR, (errmsg( - "getDefaultSearchParamBsonFunc is not defined for the vector index"))); - } - if (extensibleDefinition->calculateSearchParamBsonFunc == NULL) { ereport(ERROR, (errmsg( @@ -630,8 +619,19 @@ ParseHNSWIndexSearchSpec(const VectorSearchOptions *vectorSearchOptions) /* Private methods */ /* --------------------------------------------------------- */ static pgbson * -CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows) +CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows, + pgbson *searchParamBson) { + /* If the searchParamBson already has the nProbes */ + bson_iter_t documentIterator; + if (searchParamBson != NULL && + PgbsonInitIteratorAtPath(searchParamBson, VECTOR_PARAMETER_NAME_IVF_NPROBES, + &documentIterator)) + { + return searchParamBson; + } + + /* Calculate the default nProbes */ int numLists = -1; if (indexOptions == NULL) { @@ -678,13 +678,29 @@ CalculateIVFSearchParamBson(bytea *indexOptions, Cardinality indexRows) defaultNumProbes); } + if (searchParamBson != NULL) + { + PgbsonWriterConcat(&optionsWriter, searchParamBson); + } + return PgbsonWriterGetPgbson(&optionsWriter); } static pgbson * -CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows) +CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows, + pgbson *searchParamBson) { + /* If the searchParamBson already has the efSearch */ + bson_iter_t documentIterator; + if (searchParamBson != NULL && + PgbsonInitIteratorAtPath(searchParamBson, VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH, + &documentIterator)) + { + return searchParamBson; + } + + /* Calculate the default efSearch */ int efConstruction = -1; if (indexOptions == NULL) @@ -724,6 +740,11 @@ CalculateHNSWSearchParamBson(bytea *indexOptions, Cardinality indexRows) defaultEfSearch); } + if (searchParamBson != NULL) + { + PgbsonWriterConcat(&optionsWriter, searchParamBson); + } + return PgbsonWriterGetPgbson(&optionsWriter); } @@ -736,6 +757,7 @@ SetIVFSearchParametersToGUC(const pgbson *searchParamBson) while (bson_iter_next(&documentIterator)) { const char *key = bson_iter_key(&documentIterator); + const bson_value_t *value = bson_iter_value(&documentIterator); if (strcmp(key, VECTOR_PARAMETER_NAME_IVF_NPROBES) == 0) { int32_t nProbes = BsonValueAsInt32(bson_iter_value( @@ -748,8 +770,31 @@ SetIVFSearchParametersToGUC(const pgbson *searchParamBson) snprintf(nProbesStr, sizeof(nProbesStr), "%d", nProbes); SetGUCLocally("ivfflat.probes", nProbesStr); + } + else if (strcmp(key, VECTOR_PARAMETER_NAME_ITERATIVE_SCAN) == 0) + { + if (value->value_type == BSON_TYPE_UTF8) + { + /* + * set local ivfflat.iterative_scan=relaxed_order + * TODO: set pgvector GUCs for ivf iterative scan + * ivfflat.max_probes: Specify the max number of probes(32768 by default) + */ + const char *iterativeScanMode = value->value.v_utf8.str; - break; + /* The default of GUC VectorPreFilterIterativeScanMode is relaxed_order, + * in case we set it to strict_order, it is not supported for the ivf index, use relaxed_order instead + * this may cause the results to be slightly out of order */ + if (strcmp(iterativeScanMode, "strict_order") == 0) + { + ereport(WARNING, errmsg( + "Iiterative_scan '%s' is not supported for ivf index, use relaxed_order instead, this may cause the results to be slightly out of order.", + iterativeScanMode)); + iterativeScanMode = "relaxed_order"; + } + + SetGUCLocally("ivfflat.iterative_scan", iterativeScanMode); + } } } } @@ -763,10 +808,10 @@ SetHNSWSearchParametersToGUC(const pgbson *searchParamBson) while (bson_iter_next(&documentIterator)) { const char *key = bson_iter_key(&documentIterator); + const bson_value_t *value = bson_iter_value(&documentIterator); if (strcmp(key, VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH) == 0) { - int32_t efSearch = BsonValueAsInt32(bson_iter_value( - &documentIterator)); + int32_t efSearch = BsonValueAsInt32(value); /* * set efSearch to local GUC hnsw.ef_search @@ -775,41 +820,24 @@ SetHNSWSearchParametersToGUC(const pgbson *searchParamBson) snprintf(efSearchStr, sizeof(efSearchStr), "%d", efSearch); SetGUCLocally("hnsw.ef_search", efSearchStr); - - break; + } + else if (strcmp(key, VECTOR_PARAMETER_NAME_ITERATIVE_SCAN) == 0) + { + if (value->value_type == BSON_TYPE_UTF8) + { + /* + * set local hnsw.iterative_scan=relaxed_order + * TODO: set pgvector GUCs for hnsw iterative scan + * hnsw.max_scan_tuples: Specify the max number of tuples to visit (20,000 by default) + * hnsw.scan_mem_multiplier: Specify the max amount of memory to use, as a multiple of work_mem (1 by default) + */ + SetGUCLocally("hnsw.iterative_scan", value->value.v_utf8.str); + } } } } -static pgbson * -GetIVFDefaultSearchParamBson(void) -{ - pgbson_writer optionsWriter; - PgbsonWriterInit(&optionsWriter); - - PgbsonWriterAppendInt32(&optionsWriter, VECTOR_PARAMETER_NAME_IVF_NPROBES, - VECTOR_PARAMETER_NAME_IVF_NPROBES_STR_LEN, - IVFFLAT_DEFAULT_NPROBES); - - return PgbsonWriterGetPgbson(&optionsWriter); -} - - -static pgbson * -GetHNSWDefaultSearchParamBson(void) -{ - pgbson_writer optionsWriter; - PgbsonWriterInit(&optionsWriter); - - PgbsonWriterAppendInt32(&optionsWriter, VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH, - VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH_STR_LEN, - HNSW_DEFAULT_EF_SEARCH); - - return PgbsonWriterGetPgbson(&optionsWriter); -} - - static VectorIndexCompressionType ExtractIVFCompressionType(bytea *indexOptions) { diff --git a/pg_documentdb/src/vector/vector_utilities.c b/pg_documentdb/src/vector/vector_utilities.c index 0ada7874..4fcf8b2c 100644 --- a/pg_documentdb/src/vector/vector_utilities.c +++ b/pg_documentdb/src/vector/vector_utilities.c @@ -25,6 +25,7 @@ #include "metadata/metadata_cache.h" #include "vector/bson_extract_vector.h" #include "vector/vector_common.h" +#include "vector/vector_configs.h" #include "vector/vector_planner.h" #include "vector/vector_utilities.h" #include "vector/vector_spec.h" @@ -100,11 +101,10 @@ EvaluateMetaSearchScore(pgbson *document) * b. If the number of rows is greater than 10K, the default efSearch is HNSW_DEFAULT_EF_SEARCH. */ pgbson * -CalculateSearchParamBsonForIndexPath(IndexPath *vectorSearchPath) +CalculateSearchParamBsonForIndexPath(IndexPath *vectorSearchPath, pgbson *searchParamBson) { IndexPath *indexPath = (IndexPath *) vectorSearchPath; Oid indexRelam = indexPath->indexinfo->relam; - pgbson *searchParamBson = NULL; /* Get rows in the index */ Cardinality indexRows = indexPath->indexinfo->tuples; @@ -122,7 +122,7 @@ CalculateSearchParamBsonForIndexPath(IndexPath *vectorSearchPath) if (indexRelation->rd_options != NULL) { searchParamBson = definition->calculateSearchParamBsonFunc( - indexRelation->rd_options, indexRows); + indexRelation->rd_options, indexRows, searchParamBson); } RelationClose(indexRelation); @@ -422,31 +422,6 @@ SetSearchParametersToGUC(Oid vectorAccessMethodOid, pgbson *searchParamBson) } -/* - * This function set the default search parameters to inputState if the user did not specify - */ -void -TrySetDefaultSearchParamForCustomScan(SearchQueryEvalData *querySearchData) -{ - pgbson *searchInput = NULL; - if (querySearchData->SearchParamBson != (Datum) 0) - { - searchInput = DatumGetPgBson(querySearchData->SearchParamBson); - } - - if (searchInput == NULL || IsPgbsonEmptyDocument(searchInput)) - { - const VectorIndexDefinition *definition = GetVectorIndexDefinitionByIndexAmOid( - querySearchData->VectorAccessMethodOid); - if (definition != NULL) - { - querySearchData->SearchParamBson = PointerGetDatum( - definition->getDefaultSearchParamBsonFunc()); - } - } -} - - /* * This function returns the full vector operator id for the given distance metric. */