mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-10 22:56:55 +08:00
Improved precision for cosine distance [skip ci]
This commit is contained in:
@@ -932,7 +932,7 @@ CREATE OPERATOR CLASS minivec_ip_ops
|
|||||||
CREATE OPERATOR CLASS minivec_cosine_ops
|
CREATE OPERATOR CLASS minivec_cosine_ops
|
||||||
FOR TYPE minivec USING hnsw AS
|
FOR TYPE minivec USING hnsw AS
|
||||||
OPERATOR 1 <=> (minivec, minivec) FOR ORDER BY float_ops,
|
OPERATOR 1 <=> (minivec, minivec) FOR ORDER BY float_ops,
|
||||||
FUNCTION 1 minivec_negative_inner_product(minivec, minivec),
|
FUNCTION 1 cosine_distance(minivec, minivec),
|
||||||
FUNCTION 2 l2_norm(minivec),
|
FUNCTION 2 l2_norm(minivec),
|
||||||
FUNCTION 3 hnsw_minivec_support(internal);
|
FUNCTION 3 hnsw_minivec_support(internal);
|
||||||
|
|
||||||
|
|||||||
@@ -159,6 +159,9 @@ HnswOptionalProcInfo(Relation index, uint16 procnum)
|
|||||||
Datum
|
Datum
|
||||||
HnswNormValue(const HnswTypeInfo * typeInfo, Oid collation, Datum value)
|
HnswNormValue(const HnswTypeInfo * typeInfo, Oid collation, Datum value)
|
||||||
{
|
{
|
||||||
|
if (!typeInfo->normalize)
|
||||||
|
return value;
|
||||||
|
|
||||||
return DirectFunctionCall1Coll(typeInfo->normalize, collation, value);
|
return DirectFunctionCall1Coll(typeInfo->normalize, collation, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1382,7 +1385,8 @@ hnsw_minivec_support(PG_FUNCTION_ARGS)
|
|||||||
{
|
{
|
||||||
static const HnswTypeInfo typeInfo = {
|
static const HnswTypeInfo typeInfo = {
|
||||||
.maxDimensions = HNSW_MAX_DIM * 4,
|
.maxDimensions = HNSW_MAX_DIM * 4,
|
||||||
.normalize = minivec_l2_normalize,
|
/* Do not normalize to maximize precision */
|
||||||
|
.normalize = NULL,
|
||||||
.checkValue = NULL
|
.checkValue = NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -95,10 +95,6 @@ for my $i (0 .. $#operators)
|
|||||||
|
|
||||||
# Test approximate results
|
# Test approximate results
|
||||||
my $min = 0.98;
|
my $min = 0.98;
|
||||||
if ($operator eq "<=>")
|
|
||||||
{
|
|
||||||
$min = 0.60;
|
|
||||||
}
|
|
||||||
test_recall($min, $operator);
|
test_recall($min, $operator);
|
||||||
|
|
||||||
$node->safe_psql("postgres", "DROP INDEX idx;");
|
$node->safe_psql("postgres", "DROP INDEX idx;");
|
||||||
|
|||||||
@@ -104,10 +104,6 @@ for my $i (0 .. $#operators)
|
|||||||
|
|
||||||
# Test approximate results
|
# Test approximate results
|
||||||
my $min = 0.98;
|
my $min = 0.98;
|
||||||
if ($operator eq "<=>")
|
|
||||||
{
|
|
||||||
$min = 0.60;
|
|
||||||
}
|
|
||||||
test_recall($min, $operator);
|
test_recall($min, $operator);
|
||||||
|
|
||||||
$node->safe_psql("postgres", "DROP INDEX idx;");
|
$node->safe_psql("postgres", "DROP INDEX idx;");
|
||||||
|
|||||||
Reference in New Issue
Block a user