Skip to content

Commit

Permalink
Changed Distance to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 10, 2024
1 parent df8a547 commit 6ef7546
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0 (unreleased)

- Changed `Distance` to enum

## 0.1.4 (2023-11-14)

- Moved package to `pgvector` namespace
Expand Down
9 changes: 4 additions & 5 deletions src/laravel/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Pgvector\Laravel;

// TODO use enum when PHP 8.0 reaches EOL
class Distance
enum Distance
{
public const L2 = 0;
public const InnerProduct = 1;
public const Cosine = 2;
case L2;
case InnerProduct;
case Cosine;
}
4 changes: 2 additions & 2 deletions src/laravel/HasNeighbors.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait HasNeighbors
{
public function scopeNearestNeighbors(Builder $query, string $column, mixed $value, int $distance): void
public function scopeNearestNeighbors(Builder $query, string $column, mixed $value, Distance $distance): void
{
switch ($distance) {
case Distance::L2:
Expand Down Expand Up @@ -34,7 +34,7 @@ public function scopeNearestNeighbors(Builder $query, string $column, mixed $val
->orderByRaw($order, [$vector]);
}

public function nearestNeighbors(string $column, int $distance): Builder
public function nearestNeighbors(string $column, Distance $distance): Builder
{
$id = $this->getKey();
if (!array_key_exists($column, $this->attributes)) {
Expand Down
3 changes: 1 addition & 2 deletions tests/LaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public function testMissingAttribute()

public function testInvalidDistance()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid distance');
$this->expectException(TypeError::class);

Item::query()->nearestNeighbors('embedding', [1, 2, 3], 4);
}
Expand Down

0 comments on commit 6ef7546

Please sign in to comment.