Files
documentdb/pg_helio_api/include/vector/vector_common.h
T
Zinan Ren 🐾 69118d324f Merged PR 1246367: [Vector Search] Support pre-filtering in search - Part 2
### Known limitations & TODO for part 2
- [ ]  The current implementation intersects results from both vector index and bitmap index, it is actually the post-filtering
- [ ]  Not work for sharded collection

### Part 1 explain result
![](https://msdata.visualstudio.com/ba574a88-a171-48e0-8fcb-5fef6d23739c/_apis/git/repositories/501a5e7e-ffdc-4d1e-a5e6-910f73c6eb9a/pullRequests/1238453/attachments/image.png)

### New explain result
![image (5).png](https://msdata.visualstudio.com/ba574a88-a171-48e0-8fcb-5fef6d23739c/_apis/git/repositories/501a5e7e-ffdc-4d1e-a5e6-910f73c6eb9a/pullRequests/1246367/attachments/image%20%285%29.png)
From mongo command:
```
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": { "a": "some sentence" }, "nProbes": 1 }  }
                }
        ],
        "cursor": {}
}');

```
To psql command
```
1. SELECT c1(search query):
			SELECT
				document,
				(public.vector(mongo_catalog.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(mongo_catalog.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::mongo_catalog.bson, 'vector'::text), 3, true)) AS orderVal,
				ctid
			FROM
				mongo_data.documents_1 collection
			WHERE
				shard_key_value = 0
				AND ((mongo_catalog.bson_extract_vector(collection.document, 'v'::text) IS NOT NULL))
			ORDER BY
				(public.vector(mongo_catalog.bson_extract_vector(collection.document, 'v'::text), 3, true) OPERATOR(public.<=>) public.vector(mongo_catalog.bson_extract_vector('{ "vector" : [ { "$numberDouble" : "3.0" }, { "$numberDouble" : "4.9000000000000003553" }, { "$numberDouble" : "1.0" } ], "k" : { "$numberInt" : "1" }, "path" : "v" }'::mongo_catalog.bson, 'vector'::text), 3, true))
2. SELECT c2(filter query):
			SELECT
				ctid
			FROM
				mongo_data.documents_1
			WHERE
				shard_key_value = 0
				AND document @@ '{ "a": "some sentence" }'

3. JOIN c1 ON c1.ctid = c2.ctid
			SELECT
				c1.document
			FROM c1 JOIN c2 ON c2.ctid = c1.ctid
			ORDER BY c1.orderVal
			LIMIT 1
```

Related work items: #2586299
2024-02-07 23:20:07 +03:00

68 lines
1.9 KiB
C

/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* include/vector/vector_common.h
*
* Common definitions for vector indexes.
*
*-------------------------------------------------------------------------
*/
#ifndef VECTOR_COMMON__H
#define VECTOR_COMMON__H
/* ivfflat parameters */
#define IVFFLAT_DEFAULT_NPROBES 1
#define IVFFLAT_MIN_NPROBES 1
#define IVFFLAT_MAX_NPROBES 32768
#define IVFFLAT_DEFAULT_LISTS 100
#define IVFFLAT_MIN_LISTS 1
#define IVFFLAT_MAX_LISTS 32768
#define VECTOR_PARAMETER_NAME_IVF_NPROBES "nProbes"
#define VECTOR_PARAMETER_NAME_IVF_NPROBES_STR_LEN 7
#define VECTOR_PARAMETER_NAME_IVF_NLISTS "numLists"
/* hnsw parameters */
#define HNSW_DEFAULT_M 16
#define HNSW_MIN_M 2
#define HNSW_MAX_M 100
#define HNSW_DEFAULT_EF_CONSTRUCTION 64
#define HNSW_MIN_EF_CONSTRUCTION 4
#define HNSW_MAX_EF_CONSTRUCTION 1000
#define HNSW_DEFAULT_EF_SEARCH 40
#define HNSW_MIN_EF_SEARCH 1
#define HNSW_MAX_EF_SEARCH 1000
#define VECTOR_PARAMETER_NAME_HNSW_M "m"
#define VECTOR_PARAMETER_NAME_HNSW_EF_CONSTRUCTION "efConstruction"
#define VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH "efSearch"
#define VECTOR_PARAMETER_NAME_HNSW_EF_SEARCH_STR_LEN 8
/* dynamic calculation of nprobes or efSearch depending on collection size */
#define VECTOR_SEARCH_SMALL_COLLECTION_ROWS 10000
#define VECTOR_SEARCH_1M_COLLECTION_ROWS 1000000
/* metadata field names */
#define VECTOR_METADATA_FIELD_NAME "__cosmos_meta__"
#define VECTOR_METADATA_FIELD_NAME_STR_LEN 15
#define VECTOR_METADATA_SCORE_FIELD_NAME "score"
#define VECTOR_METADATA_SCORE_FIELD_NAME_STR_LEN 5
/*
* helioapi.enableVectorHNSWIndex GUC determines vector indexes
* and queries are enabled in pg_helio_api or not.
*/
extern bool EnableVectorHNSWIndex;
/*
* GUC to enable vector pre-filtering feature for vector search.
* This is disabled by default.
*/
extern bool EnableVectorPreFilter;
#endif