PHP code example of mauricioperera / php-vector-store
1. Go to this page and download the library: Download mauricioperera/php-vector-store library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mauricioperera / php-vector-store example snippets
use PHPVectorStore\VectorStore;
use PHPVectorStore\QuantizedStore;
use PHPVectorStore\IVFIndex;
use PHPVectorStore\HybridSearch;
use PHPVectorStore\HybridMode;
use PHPVectorStore\Distance;
use PHPVectorStore\BM25\Index as BM25Index;
// 1. Vector search
$store = new QuantizedStore( __DIR__ . '/vectors', 384 );
$store->set( 'articles', 'art-1', $embedding, ['title' => 'My Article'] );
$store->flush();
$results = $store->matryoshkaSearch( 'articles', $query, 5, [128, 256, 384] );
// 2. Full-text search (BM25)
$bm25 = new BM25Index();
$bm25->addDocument( 'articles', 'art-1', 'My article about machine learning...' );
$results = $bm25->search( 'articles', 'machine learning', 10 );
// 3. Hybrid search (vector + text combined)
$hybrid = new HybridSearch( $store, $bm25, HybridMode::RRF );
$results = $hybrid->search( 'articles', $query_vector, 'machine learning', 5 );
// 4. Multiple distance metrics
$results = $store->search( 'articles', $query, 5, 0, Distance::Euclidean );
// Full precision: dim x 4 bytes per vector
$store = new VectorStore( '/path', 768 );
// Int8 quantized: dim + 8 bytes per vector (4x smaller)
$q8 = new QuantizedStore( '/path', 384 );
// Binary quantized: ceil(dim/8) bytes per vector (32x smaller)
$b1 = new BinaryQuantizedStore( '/path', 768 ); // 96 B/vec
use PHPVectorStore\BM25\Index;
use PHPVectorStore\BM25\Config;
use PHPVectorStore\BM25\SimpleTokenizer;
$bm25 = new Index(
config: new Config( k1: 1.5, b: 0.75 ),
tokenizer: new SimpleTokenizer(),
);
// Index documents
$bm25->addDocument( 'articles', 'doc-1', 'The quick brown fox...' );
$bm25->addDocument( 'articles', 'doc-2', 'Database systems and SQL...' );
// Search
$results = $bm25->search( 'articles', 'quick fox', 10 );
// [['id' => 'doc-1', 'score' => 1.234, 'rank' => 1], ...]
// Get raw scores (for hybrid fusion)
$scores = $bm25->scoreAll( 'articles', 'quick fox' );
// ['doc-1' => 1.234, 'doc-2' => 0.0]
// Persist to disk
$bm25->save( '/path/vectors', 'articles' ); // writes articles.bm25.bin
$bm25->load( '/path/vectors', 'articles' ); // restores state
// Custom stop words for Spanish
$tokenizer = new SimpleTokenizer(
stopWords: ['el', 'la', 'los', 'las', 'de', 'en', 'y', 'que', 'es', 'un', 'una'],
minTokenLength: 2,
);
$bm25 = new Index( tokenizer: $tokenizer );
use PHPVectorStore\StoreInterface;
function buildIndex( StoreInterface $store ): void {
$ivf = new IVFIndex( $store );
$ivf->build( 'articles' );
}
// Works with any store
buildIndex( new VectorStore( '/path', 384 ) );
buildIndex( new QuantizedStore( '/path', 384 ) );
buildIndex( new BinaryQuantizedStore( '/path', 768 ) );
use PHPVectorStore\Document;
use PHPVectorStore\SearchResult;
$doc = new Document(
id: 'doc-1',
vector: [0.1, 0.2, ...],
text: 'The quick brown fox...',
metadata: ['title' => 'My Doc'],
);
$result = new SearchResult(
id: 'doc-1',
score: 0.95,
rank: 1,
metadata: ['title' => 'My Doc'],
collection: 'articles',
);
use PHPVectorStore\Exception\VectorStoreException;
use PHPVectorStore\Exception\DimensionMismatchException;
use PHPVectorStore\Exception\CollectionNotFoundException;