@@ -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