Skip to content

Commit e45d130

Browse files
authored
feat: allow fetching vectors using a prefix (#19)
1 parent e5afd05 commit e45d130

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

src/Contracts/IndexNamespaceInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Upstash\Vector\NamespaceInfo;
1010
use Upstash\Vector\VectorDeleteResult;
1111
use Upstash\Vector\VectorFetch;
12+
use Upstash\Vector\VectorFetchByPrefix;
1213
use Upstash\Vector\VectorFetchResult;
1314
use Upstash\Vector\VectorMatch;
1415
use Upstash\Vector\VectorQuery;
@@ -55,7 +56,7 @@ public function queryData(DataQuery $query): DataQueryResult;
5556
*/
5657
public function delete(array $ids): VectorDeleteResult;
5758

58-
public function fetch(VectorFetch $vectorFetch): VectorFetchResult;
59+
public function fetch(VectorFetch|VectorFetchByPrefix $vectorFetch): VectorFetchResult;
5960

6061
public function random(): ?VectorMatch;
6162

src/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function delete(array $ids): VectorDeleteResult
118118
return $this->namespace('')->delete($ids);
119119
}
120120

121-
public function fetch(VectorFetch $vectorFetch): VectorFetchResult
121+
public function fetch(VectorFetch|VectorFetchByPrefix $vectorFetch): VectorFetchResult
122122
{
123123
return $this->namespace('')->fetch($vectorFetch);
124124
}

src/IndexNamespace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function delete(array $ids): VectorDeleteResult
7979
->delete($ids);
8080
}
8181

82-
public function fetch(VectorFetch $vectorFetch): VectorFetchResult
82+
public function fetch(VectorFetch|VectorFetchByPrefix $vectorFetch): VectorFetchResult
8383
{
8484
return (new FetchVectorsOperation($this->namespace, $this->transporter))
8585
->fetch($vectorFetch);

src/Operations/FetchVectorsOperation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Upstash\Vector\Transporter\TransporterRequest;
1111
use Upstash\Vector\Transporter\TransporterResponse;
1212
use Upstash\Vector\VectorFetch;
13+
use Upstash\Vector\VectorFetchByPrefix;
1314
use Upstash\Vector\VectorFetchResult;
1415
use Upstash\Vector\VectorMatch;
1516

@@ -23,7 +24,7 @@
2324

2425
public function __construct(private string $namespace, private TransporterInterface $transporter) {}
2526

26-
public function fetch(VectorFetch $vectorFetch): VectorFetchResult
27+
public function fetch(VectorFetch|VectorFetchByPrefix $vectorFetch): VectorFetchResult
2728
{
2829
$path = $this->getPath();
2930
$request = new TransporterRequest(

src/VectorFetchByPrefix.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Upstash\Vector;
4+
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class VectorFetchByPrefix implements Arrayable
8+
{
9+
public function __construct(
10+
public string $prefix,
11+
public bool $includeMetadata = false,
12+
public bool $includeVectors = false,
13+
public bool $includeData = false,
14+
) {}
15+
16+
/**
17+
* @return array{
18+
* prefix: string,
19+
* includeMetadata: bool,
20+
* includeVectors: bool,
21+
* includeData: bool,
22+
* }
23+
*/
24+
public function toArray(): array
25+
{
26+
return [
27+
'prefix' => $this->prefix,
28+
'includeMetadata' => $this->includeMetadata,
29+
'includeVectors' => $this->includeVectors,
30+
'includeData' => $this->includeData,
31+
];
32+
}
33+
}

tests/Dense/Operations/FetchVectorsOperationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Upstash\Vector\Tests\Concerns\UsesDenseIndex;
77
use Upstash\Vector\Tests\Concerns\WaitsForIndex;
88
use Upstash\Vector\VectorFetch;
9+
use Upstash\Vector\VectorFetchByPrefix;
910
use Upstash\Vector\VectorUpsert;
1011

1112
use function Upstash\Vector\createRandomVector;
@@ -16,6 +17,27 @@ class FetchVectorsOperationTest extends TestCase
1617
use WaitsForIndex;
1718

1819
public function test_can_fetch_vectors(): void
20+
{
21+
// Arrange
22+
$this->namespace->upsertMany([
23+
new VectorUpsert(id: 'user:1', vector: createRandomVector(2)),
24+
new VectorUpsert(id: 'user:2', vector: createRandomVector(2)),
25+
new VectorUpsert(id: 'test:3', vector: createRandomVector(2)),
26+
]);
27+
$this->waitForIndex($this->namespace);
28+
29+
// Act
30+
$results = $this->namespace->fetch(new VectorFetchByPrefix(
31+
prefix: 'user:',
32+
includeVectors: true,
33+
));
34+
35+
// Assert
36+
$this->assertCount(2, $results);
37+
$this->assertCount(2, $results[0]->vector);
38+
}
39+
40+
public function test_can_fetch_vectors_using_a_filter(): void
1941
{
2042
// Arrange
2143
$this->namespace->upsertMany([

0 commit comments

Comments
 (0)