-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path10-vector-store.php
More file actions
97 lines (69 loc) · 3.5 KB
/
Copy path10-vector-store.php
File metadata and controls
97 lines (69 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use FerryAI\Vector\CollectionManager;
use FerryAI\Vector\ExportImport;
use FerryAI\Vector\MetadataFilter;
use FerryAI\Vector\SQLiteStore;
echo "=== 10 — Vector Store ===\n\n";
$store = new SQLiteStore(':memory:');
$manager = new CollectionManager($store);
echo "--- CRUD ---\n\n";
$products = $manager->create('products', 3);
$products->add('p1', [0.1, 0.2, 0.3], ['name' => 'Widget', 'price' => 99, 'category' => 'tools']);
$products->add('p2', [0.4, 0.5, 0.6], ['name' => 'Gadget', 'price' => 149, 'category' => 'electronics']);
$products->add('p3', [0.7, 0.8, 0.9], ['name' => 'Screwdriver', 'price' => 12, 'category' => 'tools']);
$products->addBatch([
['id' => 'p4', 'vector' => [1.0, 1.0, 1.0], 'metadata' => ['name' => 'Drill', 'price' => 250, 'category' => 'tools']],
['id' => 'p5', 'vector' => [0.5, 0.5, 0.5], 'metadata' => ['name' => 'Tablet', 'price' => 499, 'category' => 'electronics']],
]);
printf("count: %d\n", $products->count());
printf("dimension: %d\n", $products->dimension());
printf("collectionName: %s\n\n", $products->collectionName());
echo "--- Search ---\n\n";
$query = [0.1, 0.2, 0.3];
$results = $products->search($query, k: 3);
foreach ($results as $r) {
printf(" %s d=%.4f %s ($%d)\n", $r['id'], $r['distance'], $r['metadata']['name'], $r['metadata']['price']);
}
echo "\n--- Search with MetadataFilter ---\n\n";
$results = $products->search($query, k: 10, filter: [
'and' => [
['category' => ['eq' => 'tools']],
['price' => ['lt' => 200]],
],
]);
printf("tools under $200: %d results\n", count($results));
foreach ($results as $r) {
printf(" %s %s ($%d)\n", $r['id'], $r['metadata']['name'], $r['metadata']['price']);
}
echo "\n--- MetadataFilter Operators ---\n\n";
$filter = new MetadataFilter();
$meta = ['price' => 100, 'tags' => ['sale', 'new'], 'name' => 'Test Product'];
printf("price > 50: %s\n", $filter->matches($meta, ['price' => ['gt' => 50]]) ? 'true' : 'false');
printf("price < 200: %s\n", $filter->matches($meta, ['price' => ['lt' => 200]]) ? 'true' : 'false');
printf("category exists: %s\n", $filter->matches($meta, ['category' => ['exists' => true]]) ? 'true' : 'false');
printf("name exists: %s\n", $filter->matches($meta, ['name' => ['exists' => true]]) ? 'true' : 'false');
printf("name contains 'Test': %s\n\n", $filter->matches($meta, ['name' => ['contains' => 'Test']]) ? 'true' : 'false');
echo "--- Update & Delete ---\n\n";
$products->update('p3', null, ['name' => 'Premium Screwdriver', 'price' => 25]);
$products->delete('p5');
printf("after update+delete: %d vectors\n", $products->count());
$deleted = $products->deleteByFilter(['category' => ['eq' => 'electronics']]);
printf("deletedByFilter(electronics): %d\n\n", $deleted);
echo "--- Export ---\n\n";
$tmpFile = sys_get_temp_dir() . '/ferry-export-' . uniqid() . '.jsonl';
ExportImport::toJson($products, $tmpFile);
printf("exported: %s (%d bytes)\n", basename($tmpFile), filesize($tmpFile));
ExportImport::toCsv($products, sys_get_temp_dir() . '/ferry-export.csv');
printf("CSV exported\n\n");
unlink($tmpFile);
echo "--- Collections ---\n\n";
$manager->create('users', 128);
$manager->create('docs', 768);
printf("collections: %s\n", implode(', ', $manager->list()));
printf("exists('users'): %s\n", $manager->exists('users') ? 'YES' : 'NO');
$manager->delete('users');
printf("after delete users: %s\n\n", implode(', ', $manager->list()));
echo "=== OK ===\n";