mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-15 00:56:54 +08:00
Test recall for all operators
This commit is contained in:
@@ -2,16 +2,16 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
use PostgresNode;
|
use PostgresNode;
|
||||||
use TestLib;
|
use TestLib;
|
||||||
use Test::More tests => 3;
|
use Test::More tests => 9;
|
||||||
|
|
||||||
my $node;
|
my $node;
|
||||||
my @queries = ();
|
my @queries = ();
|
||||||
my @expected = ();
|
my @expected;
|
||||||
my $limit = 30;
|
my $limit = 30;
|
||||||
|
|
||||||
sub test_recall
|
sub test_recall
|
||||||
{
|
{
|
||||||
my ($probes, $min) = @_;
|
my ($probes, $min, $operator) = @_;
|
||||||
my $correct = 0;
|
my $correct = 0;
|
||||||
my $total = 0;
|
my $total = 0;
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ sub test_recall
|
|||||||
my $actual = $node->safe_psql("postgres", qq(
|
my $actual = $node->safe_psql("postgres", qq(
|
||||||
SET enable_seqscan = off;
|
SET enable_seqscan = off;
|
||||||
SET ivfflat.probes = $probes;
|
SET ivfflat.probes = $probes;
|
||||||
SELECT i FROM tst ORDER BY v <-> '$queries[$i]' LIMIT $limit;
|
SELECT i FROM tst ORDER BY v $operator '$queries[$i]' LIMIT $limit;
|
||||||
));
|
));
|
||||||
my @actual_ids = split("\n", $actual);
|
my @actual_ids = split("\n", $actual);
|
||||||
my %actual_set = map { $_ => 1 } @actual_ids;
|
my %actual_set = map { $_ => 1 } @actual_ids;
|
||||||
@@ -57,16 +57,32 @@ for (1..20) {
|
|||||||
push(@queries, "[$r1,$r2,$r3]");
|
push(@queries, "[$r1,$r2,$r3]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Check each index type
|
||||||
|
my @operators = ("<->", "<#>", "<=>");
|
||||||
|
|
||||||
|
foreach (@operators) {
|
||||||
|
my $operator = $_;
|
||||||
|
|
||||||
# Get exact results
|
# Get exact results
|
||||||
|
@expected = ();
|
||||||
foreach (@queries) {
|
foreach (@queries) {
|
||||||
my $res = $node->safe_psql("postgres", "SELECT i FROM tst ORDER BY v <-> '$_' LIMIT $limit;");
|
my $res = $node->safe_psql("postgres", "SELECT i FROM tst ORDER BY v $operator '$_' LIMIT $limit;");
|
||||||
push(@expected, $res);
|
push(@expected, $res);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add index
|
# Add index
|
||||||
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v);");
|
my $opclass;
|
||||||
|
if ($operator == "<->") {
|
||||||
|
$opclass = "vector_l2_ops";
|
||||||
|
} elsif ($operator == "<#>") {
|
||||||
|
$opclass = "vector_ip_ops";
|
||||||
|
} else {
|
||||||
|
$opclass = "vector_cosine_ops";
|
||||||
|
}
|
||||||
|
$node->safe_psql("postgres", "CREATE INDEX ON tst USING ivfflat (v $opclass);");
|
||||||
|
|
||||||
# Test approximate results
|
# Test approximate results
|
||||||
test_recall(1, 0.8);
|
test_recall(1, 0.8, $operator);
|
||||||
test_recall(10, 0.95);
|
test_recall(10, 0.95, $operator);
|
||||||
test_recall(100, 1.0);
|
test_recall(100, 1.0, $operator);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user