mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-07 03:22:46 -05:00
### Does this PR have any customer impact?
Yes
### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Feature
### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No
### Are you introducing any new config? If yes, do you have tests with and without them being set?
Yes
### Release Note (Refer [Template](../docs/release_notes/pgmongo_template.md))
### Description
With the release of [pgvector 0.8.0](https://github.com/pgvector/pgvector/tree/5bc7937715c67add21c8bcc0d4284162c7a0174f?tab=readme-ov-file#iterative-index-scans), which supports iterative scan for HNSW and IVF, we enable it in filtering search to support pre-filtering(currently it is post-filtering)
- Pass the new parameter "iterativeScan" through the function bson_search_param from the coordinator to the worker.
Example : { "efSearch" : 10, "iterativeScan" : "relaxed_order"}
- Enable iterative scan for hnsw/ivf index on workers:
SET LOCAL hnsw.iterative_scan = relaxed_order;
SET LOCAL ivfflat.iterative_scan = relaxed_order;
relaxed_order by default, which allows results to be slightly out of order by distance, but provides better recall.
After iterative search, re-ordering will be applied to the results.
- Set iterative scan for diskann index to relaxed_order
SET LOCAL diskann.iterative_search = relaxed_order;
- Check the availability of iterative scan in pgvector
The pgvector iterative scan is available from version 0.8.0. Currently, if the pgvector extension has not been upgraded to 0.8.0, the iterative scan will be disabled in those clusters until pgvector is upgraded to 0.8.0.
###Related GUCs
**documentdb.vectorPreFilterIterativeScanMode**: enable iterative index scans, which will automatically scan more of the index until enough results are found (relaxed_order by default)
- off, disable iterative scan
- 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
####GUCs from pgvector
**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)
**ivfflat.max_probes**: Specify the max number of probes(32768 by default)
###Mongo Command(no change in this PR)
```
db.testing_hnsw_vector_collection.aggregate(
[
{
"$search": {
"cosmosSearch": {
"vector": [ 3.0, 4.9, 1.0 ],
"path": "vect",
"filter": {"meta.a": [ { "b" : 3 } ]},
"k": 4,
"efSearch": 100
},
}
},
{
"$project": {
"rank": {
"$floor": [{"$multiply": [{ "$meta": "searchScore"},100000]}]
}
}
}
])
```
----
#### AI description (iteration 1)
#### PR Classification
New feature
#### PR Summary
This pull request introduces iterative index scans for HNSW and IVF in filtering searches, enhancing the pre-filtering capabilities of vector searches...
224 lines
5.9 KiB
C
224 lines
5.9 KiB
C
/*-------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
*
|
|
* include/vector/vector_spec.h
|
|
*
|
|
* Common definitions for vector indexes.
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef VECTOR_SPEC__H
|
|
#define VECTOR_SPEC__H
|
|
|
|
#include <nodes/primnodes.h>
|
|
#include <utils/relcache.h>
|
|
|
|
#include "io/bson_core.h"
|
|
|
|
typedef struct VectorIndexDefinition VectorIndexDefinition;
|
|
|
|
/*
|
|
* The distance metric for a vector based index.
|
|
*/
|
|
typedef enum VectorIndexDistanceMetric
|
|
{
|
|
VectorIndexDistanceMetric_Unknown = 0,
|
|
|
|
/* Use basic linear vector distance */
|
|
VectorIndexDistanceMetric_L2Distance = 1,
|
|
|
|
/* Use vector inner product distance */
|
|
VectorIndexDistanceMetric_IPDistance = 2,
|
|
|
|
/* Use inner product cosine distance. */
|
|
VectorIndexDistanceMetric_CosineDistance = 3,
|
|
} VectorIndexDistanceMetric;
|
|
|
|
/*
|
|
* The compression type for a vector index.
|
|
*/
|
|
typedef enum VectorIndexCompressionType
|
|
{
|
|
/* Use no compression */
|
|
VectorIndexCompressionType_None = 0,
|
|
|
|
/* Use half precision compression */
|
|
VectorIndexCompressionType_Half = 1,
|
|
|
|
/* Use product quantization */
|
|
VectorIndexCompressionType_PQ = 2,
|
|
|
|
/* Use binary quantization */
|
|
VectorIndexCompressionType_BQ = 3,
|
|
} VectorIndexCompressionType;
|
|
|
|
/*
|
|
* Options associated with vector based Cosmos Search indexes
|
|
*/
|
|
typedef struct VectorIndexCommonOptions
|
|
{
|
|
/* The type of distance for the vector distance */
|
|
VectorIndexDistanceMetric distanceMetric;
|
|
|
|
/* The type of compression for the vector index */
|
|
VectorIndexCompressionType compressionType;
|
|
|
|
/* The number of dimensions of the vector */
|
|
int32_t numDimensions;
|
|
} VectorIndexCommonOptions;
|
|
|
|
/*
|
|
* Options for a vector index that specifies the kind of index.
|
|
*/
|
|
typedef struct VectorKindSpecifiedOptions
|
|
{ } VectorKindSpecifiedOptions;
|
|
|
|
/*
|
|
* Index options specific to cosmosSearchOptions
|
|
* for vector and text search support.
|
|
*/
|
|
typedef struct
|
|
{
|
|
/* The raw pgbson for the cosmosSearchOptions. */
|
|
pgbson *searchOptionsDoc;
|
|
|
|
/* The index kind for the cosmosSearch index. */
|
|
const char *indexKindStr;
|
|
|
|
/* Options for a vector search */
|
|
VectorIndexCommonOptions commonOptions;
|
|
VectorKindSpecifiedOptions *vectorOptions;
|
|
} CosmosSearchOptions;
|
|
|
|
typedef struct VectorSearchOptions
|
|
{
|
|
/* it's the query spec pgbson that we pass to the bson_extract_vector() method to get the float[] vector. */
|
|
pgbson *searchSpecPgbson;
|
|
|
|
/* the bson value for the query vector */
|
|
bson_value_t queryVectorValue;
|
|
|
|
/* the length of the query vector */
|
|
int32_t queryVectorLength;
|
|
|
|
/* search path*/
|
|
char *searchPath;
|
|
|
|
/* query result count */
|
|
int32_t resultCount;
|
|
|
|
/* search param pgbson e.g. {"efSearch": 10} or {"nProbes": 10} */
|
|
pgbson *searchParamPgbson;
|
|
|
|
/* filter bson */
|
|
bson_value_t filterBson;
|
|
|
|
/* score bson */
|
|
bson_value_t scoreBson;
|
|
|
|
/* The vector access method oid */
|
|
Oid vectorAccessMethodOid;
|
|
|
|
/* whether to use exact search or ann search */
|
|
bool exactSearch;
|
|
|
|
/* The vector index definition */
|
|
const VectorIndexDefinition *vectorIndexDef;
|
|
|
|
/* Over sample rate */
|
|
double oversampling;
|
|
|
|
/* The compression type of the vector index */
|
|
VectorIndexCompressionType compressionType;
|
|
|
|
/* The type of distance for the vector distance */
|
|
VectorIndexDistanceMetric distanceMetric;
|
|
} VectorSearchOptions;
|
|
|
|
/*
|
|
* Function pointers for parsing and validating
|
|
* vector index creation specs on coordinator.
|
|
*/
|
|
typedef void (*ParseIndexCreationSpecFunc)(bson_iter_t *indexDefDocIter,
|
|
CosmosSearchOptions *cosmosSearchOptions);
|
|
|
|
typedef char *
|
|
(*GenerateIndexParamStringFunc)(const CosmosSearchOptions *cosmosSearchOptions);
|
|
|
|
/*
|
|
* Function pointers for parsing and validating
|
|
* vector index search specs on coordinator.
|
|
*/
|
|
typedef pgbson *(*ParseIndexSearchSpecFunc)(const
|
|
VectorSearchOptions *vectorSearchOptions);
|
|
|
|
/*
|
|
* Function pointers for planner and custom scan
|
|
*/
|
|
typedef Oid (*GetIndexAccessMethodOidFunc)(void);
|
|
|
|
/*
|
|
* Function pointers for setting search parameters to GUC on worker.
|
|
*/
|
|
typedef void (*SetSearchParametersToGUCFunc)(const pgbson *searchParamBson);
|
|
|
|
/*
|
|
* Dynamic calculation of search parameters
|
|
* based on the number of rows and index options.
|
|
*/
|
|
typedef pgbson *(*CalculateSearchParamBsonFunc)(bytea *indexOptions,
|
|
Cardinality indexRows,
|
|
pgbson *searchParamBson);
|
|
|
|
/*
|
|
* Extract the index compression type from the index options.
|
|
*/
|
|
typedef VectorIndexCompressionType (*ExtractIndexCompressionTypeFunc)(
|
|
bytea *indexOptions);
|
|
|
|
/*
|
|
* Definition of an extensible vector index.
|
|
* It contains the function pointers for
|
|
* 1. Vector index creation
|
|
* 1.1 Parsing and validating index creation specs.
|
|
* 1.2 Generating index parameter for the index creation.
|
|
* 2. Vector search
|
|
* 2.1 Parsing and validating index search specs.
|
|
* 2.2 Getting the index access method oid.
|
|
* 2.3 Getting the similarity operator oid by family oid.
|
|
* 3. Planner and custom scan
|
|
* 3.1 Setting search parameters to GUC on worker.
|
|
* 3.2 Getting the default search parameter bson.
|
|
* 3.3 Calculating search parameter based on the number of rows and index options.
|
|
*/
|
|
typedef struct VectorIndexDefinition
|
|
{
|
|
const char *kindName;
|
|
|
|
const char *indexAccessMethodName;
|
|
|
|
ParseIndexCreationSpecFunc parseIndexCreationSpecFunc;
|
|
|
|
GenerateIndexParamStringFunc generateIndexParamStrFunc;
|
|
|
|
ParseIndexSearchSpecFunc parseIndexSearchSpecFunc;
|
|
|
|
GetIndexAccessMethodOidFunc getIndexAccessMethodOidFunc;
|
|
|
|
SetSearchParametersToGUCFunc setSearchParametersToGUCFunc;
|
|
|
|
CalculateSearchParamBsonFunc calculateSearchParamBsonFunc;
|
|
|
|
ExtractIndexCompressionTypeFunc extractIndexCompressionTypeFunc;
|
|
} VectorIndexDefinition;
|
|
|
|
const VectorIndexDefinition * GetVectorIndexDefinitionByIndexAmOid(Oid indexAmOid);
|
|
|
|
const VectorIndexDefinition * GetVectorIndexDefinitionByIndexKindName(
|
|
StringView *indexKindStr);
|
|
|
|
void RegisterVectorIndexExtension(const VectorIndexDefinition *extensibleDefinition);
|
|
|
|
|
|
#endif
|