Skip to content

Commit c424591

Browse files
feat: expose more information about the index (#14)
* feat: expose more information about the index * test: add tests for dense and hybrid index info * chore: remove prefix in import --------- Co-authored-by: ytkimirti <yusuftaha9@gmail.com>
1 parent b752cd4 commit c424591

9 files changed

Lines changed: 143 additions & 2 deletions

File tree

src/DenseIndexInfo.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class DenseIndexInfo implements Arrayable
8+
{
9+
public function __construct(
10+
public int $dimension = 0,
11+
public string $similarityFunction = '',
12+
public string $embeddingModel = '',
13+
) {}
14+
15+
public function toArray(): array
16+
{
17+
return [
18+
'dimension' => $this->dimension,
19+
'similarityFunction' => $this->similarityFunction,
20+
'embeddingModel' => $this->embeddingModel,
21+
];
22+
}
23+
}

src/Enums/IndexType.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Enums;
4+
5+
enum IndexType: string
6+
{
7+
case DENSE = 'DENSE';
8+
case SPARSE = 'SPARSE';
9+
case HYBRID = 'HYBRID';
10+
case UNKNOWN = 'UNKNOWN';
11+
}

src/IndexInfo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Upstash\Vector;
44

55
use Upstash\Vector\Contracts\Arrayable;
6+
use Upstash\Vector\Enums\IndexType;
67

78
final readonly class IndexInfo implements Arrayable
89
{
@@ -16,6 +17,9 @@ public function __construct(
1617
public int $dimension = 0,
1718
public string $similarityFunction = '',
1819
public array $namespaces = [],
20+
public IndexType $indexType = IndexType::UNKNOWN,
21+
public ?DenseIndexInfo $denseIndex = null,
22+
public ?SparseIndexInfo $sparseIndex = null,
1923
) {}
2024

2125
public function namespace(string $namespace): NamespaceInfo
@@ -38,6 +42,9 @@ public function toArray(): array
3842
'dimension' => $this->dimension,
3943
'similarityFunction' => $this->similarityFunction,
4044
'namespaces' => $this->namespaces,
45+
'indexType' => $this->indexType->value,
46+
'denseIndex' => $this->denseIndex?->toArray(),
47+
'sparseIndex' => $this->sparseIndex?->toArray(),
4148
];
4249
}
4350
}

src/Iterators/VectorRangeIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Upstash\Vector\Iterators;
44

5+
use Iterator;
56
use Upstash\Vector\Operations\RangeVectorsOperation;
67
use Upstash\Vector\VectorMatch;
78
use Upstash\Vector\VectorRange;
@@ -10,7 +11,7 @@
1011
/**
1112
* @implements \Iterator<string, VectorMatch>
1213
*/
13-
class VectorRangeIterator implements \Iterator
14+
class VectorRangeIterator implements Iterator
1415
{
1516
private string $nextCursor;
1617

@@ -46,7 +47,6 @@ public function next(): void
4647
$this->results = $rangeResult->getResults();
4748
$this->position = 0;
4849
}
49-
5050
}
5151

5252
public function key(): string

src/Operations/GetIndexInfoOperation.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Upstash\Vector\Operations;
44

55
use Upstash\Vector\Contracts\TransporterInterface;
6+
use Upstash\Vector\DenseIndexInfo;
7+
use Upstash\Vector\Enums\IndexType;
68
use Upstash\Vector\IndexInfo;
79
use Upstash\Vector\NamespaceInfo;
810
use Upstash\Vector\Operations\Concerns\AssertsApiResponseErrors;
11+
use Upstash\Vector\SparseIndexInfo;
912
use Upstash\Vector\Transporter\ContentType;
1013
use Upstash\Vector\Transporter\Method;
1114
use Upstash\Vector\Transporter\TransporterRequest;
@@ -55,6 +58,31 @@ private function transformResponse(TransporterResponse $response): IndexInfo
5558
dimension: $result['dimension'],
5659
similarityFunction: $result['similarityFunction'],
5760
namespaces: $namespaces,
61+
indexType: IndexType::tryFrom($result['indexType'] ?? '') ?? IndexType::UNKNOWN,
62+
denseIndex: isset($result['denseIndex']) ? $this->transformDenseIndex($result['denseIndex']) : null,
63+
sparseIndex: isset($result['sparseIndex']) ? $this->transformSparseIndex($result['sparseIndex']) : null,
64+
);
65+
}
66+
67+
/**
68+
* @param array<string, mixed> $data
69+
*/
70+
private function transformDenseIndex(array $data): DenseIndexInfo
71+
{
72+
return new DenseIndexInfo(
73+
dimension: $data['dimension'] ?? 0,
74+
similarityFunction: $data['similarityFunction'] ?? '',
75+
embeddingModel: $data['embeddingModel'] ?? '',
76+
);
77+
}
78+
79+
/**
80+
* @param array<string, mixed> $data
81+
*/
82+
private function transformSparseIndex(array $data): SparseIndexInfo
83+
{
84+
return new SparseIndexInfo(
85+
embeddingModel: $data['embeddingModel'] ?? '',
5886
);
5987
}
6088
}

src/SparseIndexInfo.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class SparseIndexInfo implements Arrayable
8+
{
9+
public function __construct(
10+
public string $embeddingModel = '',
11+
) {}
12+
13+
public function toArray(): array
14+
{
15+
return [
16+
'embeddingModel' => $this->embeddingModel,
17+
];
18+
}
19+
}

tests/Dense/Operations/GetIndexInfoTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Upstash\Vector\Tests\Dense\Operations;
44

55
use PHPUnit\Framework\TestCase;
6+
use Upstash\Vector\Enums\IndexType;
67
use Upstash\Vector\IndexInfo;
78
use Upstash\Vector\Tests\Concerns\UsesDenseIndex;
89

@@ -15,5 +16,10 @@ public function test_get_info(): void
1516
$info = $this->index->getInfo();
1617

1718
$this->assertInstanceOf(IndexInfo::class, $info);
19+
$this->assertSame(IndexType::DENSE, $info->indexType);
20+
$this->assertNull($info->sparseIndex);
21+
$this->assertNotNull($info->denseIndex);
22+
$this->assertSame(2, $info->denseIndex->dimension);
23+
$this->assertSame('COSINE', $info->denseIndex->similarityFunction);
1824
}
1925
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Tests\Hybrid\Operations;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Upstash\Vector\Enums\IndexType;
7+
use Upstash\Vector\IndexInfo;
8+
use Upstash\Vector\Tests\Concerns\UsesHybridIndex;
9+
10+
class GetIndexInfoTest extends TestCase
11+
{
12+
use UsesHybridIndex;
13+
14+
public function test_get_info(): void
15+
{
16+
$info = $this->index->getInfo();
17+
18+
$this->assertInstanceOf(IndexInfo::class, $info);
19+
$this->assertSame(IndexType::HYBRID, $info->indexType);
20+
$this->assertNotNull($info->sparseIndex);
21+
$this->assertNotNull($info->denseIndex);
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Tests\Sparse\Operations;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Upstash\Vector\Enums\IndexType;
7+
use Upstash\Vector\IndexInfo;
8+
use Upstash\Vector\Tests\Concerns\UsesSparseIndex;
9+
10+
class GetIndexInfoTest extends TestCase
11+
{
12+
use UsesSparseIndex;
13+
14+
public function test_get_info(): void
15+
{
16+
$info = $this->index->getInfo();
17+
18+
$this->assertInstanceOf(IndexInfo::class, $info);
19+
$this->assertSame(IndexType::SPARSE, $info->indexType);
20+
$this->assertNotNull($info->sparseIndex);
21+
$this->assertNull($info->denseIndex);
22+
$this->assertSame('BM25', $info->sparseIndex->embeddingModel);
23+
}
24+
}

0 commit comments

Comments
 (0)