Improved precision for cosine distance [skip ci]

This commit is contained in:
Andrew Kane
2024-09-23 18:50:10 -07:00
parent 0a2803130f
commit 22efa8ec22
4 changed files with 6 additions and 10 deletions

View File

@@ -932,7 +932,7 @@ CREATE OPERATOR CLASS minivec_ip_ops
CREATE OPERATOR CLASS minivec_cosine_ops
FOR TYPE minivec USING hnsw AS
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 3 hnsw_minivec_support(internal);

View File

@@ -159,6 +159,9 @@ HnswOptionalProcInfo(Relation index, uint16 procnum)
Datum
HnswNormValue(const HnswTypeInfo * typeInfo, Oid collation, Datum value)
{
if (!typeInfo->normalize)
return value;
return DirectFunctionCall1Coll(typeInfo->normalize, collation, value);
}
@@ -1382,7 +1385,8 @@ hnsw_minivec_support(PG_FUNCTION_ARGS)
{
static const HnswTypeInfo typeInfo = {
.maxDimensions = HNSW_MAX_DIM * 4,
.normalize = minivec_l2_normalize,
/* Do not normalize to maximize precision */
.normalize = NULL,
.checkValue = NULL
};

View File

@@ -95,10 +95,6 @@ for my $i (0 .. $#operators)
# Test approximate results
my $min = 0.98;
if ($operator eq "<=>")
{
$min = 0.60;
}
test_recall($min, $operator);
$node->safe_psql("postgres", "DROP INDEX idx;");

View File

@@ -104,10 +104,6 @@ for my $i (0 .. $#operators)
# Test approximate results
my $min = 0.98;
if ($operator eq "<=>")
{
$min = 0.60;
}
test_recall($min, $operator);
$node->safe_psql("postgres", "DROP INDEX idx;");