Skip to content

Commit 9a887bf

Browse files
committed
Added bit distance functions for Doctrine
1 parent cc5f0b6 commit 9a887bf

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/doctrine/HammingDistance.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Pgvector\Doctrine;
4+
5+
class HammingDistance extends DistanceNode
6+
{
7+
protected function getOp(): string
8+
{
9+
return '<~>';
10+
}
11+
}

src/doctrine/JaccardDistance.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Pgvector\Doctrine;
4+
5+
class JaccardDistance extends DistanceNode
6+
{
7+
protected function getOp(): string
8+
{
9+
return '<%>';
10+
}
11+
}

tests/DoctrineTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static function setUpBeforeClass(): void
2929
$config->addCustomNumericFunction('max_inner_product', 'Pgvector\Doctrine\MaxInnerProduct');
3030
$config->addCustomNumericFunction('cosine_distance', 'Pgvector\Doctrine\CosineDistance');
3131
$config->addCustomNumericFunction('l1_distance', 'Pgvector\Doctrine\L1Distance');
32+
$config->addCustomNumericFunction('hamming_distance', 'Pgvector\Doctrine\HammingDistance');
33+
$config->addCustomNumericFunction('jaccard_distance', 'Pgvector\Doctrine\JaccardDistance');
3234

3335
$connection = DriverManager::getConnection([
3436
'driver' => 'pgsql',
@@ -126,13 +128,43 @@ public function testVectorL1Distance()
126128
$this->assertEquals([1, 3, 2], array_map(fn ($v) => $v->getId(), $neighbors));
127129
}
128130

131+
public function testBitHammingDistance()
132+
{
133+
$this->createBitItems();
134+
$neighbors = self::$em->createQuery('SELECT i FROM DoctrineItem i ORDER BY hamming_distance(i.binaryEmbedding, ?1)')
135+
->setParameter(1, '101')
136+
->setMaxResults(5)
137+
->getResult();
138+
$this->assertEquals([2, 3, 1], array_map(fn ($v) => $v->getId(), $neighbors));
139+
}
140+
141+
public function testBitJaccardDistance()
142+
{
143+
$this->createBitItems();
144+
$neighbors = self::$em->createQuery('SELECT i FROM DoctrineItem i ORDER BY jaccard_distance(i.binaryEmbedding, ?1)')
145+
->setParameter(1, '101')
146+
->setMaxResults(5)
147+
->getResult();
148+
$this->assertEquals([2, 3, 1], array_map(fn ($v) => $v->getId(), $neighbors));
149+
}
150+
129151
private function createItems()
130152
{
131-
foreach ([[1, 1, 1], [2, 2, 2], [1, 1, 2]] as $i => $v) {
153+
foreach ([[1, 1, 1], [2, 2, 2], [1, 1, 2]] as $v) {
132154
$item = new DoctrineItem();
133155
$item->setEmbedding(new Vector($v));
134156
self::$em->persist($item);
135157
}
136158
self::$em->flush();
137159
}
160+
161+
private function createBitItems()
162+
{
163+
foreach (['000', '101', '111'] as $v) {
164+
$item = new DoctrineItem();
165+
$item->setBinaryEmbedding($v);
166+
self::$em->persist($item);
167+
}
168+
self::$em->flush();
169+
}
138170
}

0 commit comments

Comments
 (0)