PHPackages                             k99k5/hyperf-pgvector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. k99k5/hyperf-pgvector

ActiveLibrary[Database &amp; ORM](/categories/database)

k99k5/hyperf-pgvector
=====================

pgvector support for hyperf

0.3(7mo ago)05MITPHPPHP &gt;= 8.1

Since Aug 7Pushed 7mo agoCompare

[ Source](https://github.com/k99k5/pgvector-hyperf)[ Packagist](https://packagist.org/packages/k99k5/hyperf-pgvector)[ RSS](/packages/k99k5-hyperf-pgvector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

pgvector-php
============

[](#pgvector-php)

[pgvector](https://github.com/pgvector/pgvector) support for PHP

Supports [Laravel](https://github.com/laravel/laravel), [Doctrine](https://github.com/doctrine/orm), and [PgSql](https://www.php.net/manual/en/book.pgsql.php)

[![Build Status](https://github.com/pgvector/pgvector-php/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-php/actions)

Getting Started
---------------

[](#getting-started)

Follow the instructions for your database library:

- [Laravel](#laravel)
- [Doctrine](#doctrine)
- [PgSql](#pgsql)

Or check out some examples:

- [Embeddings](examples/openai/example.php) with OpenAI
- [Binary embeddings](examples/cohere/example.php) with Cohere
- [Hybrid search](examples/hybrid/example.php) with Ollama (Reciprocal Rank Fusion)
- [Sparse search](examples/sparse/example.php) with Text Embeddings Inference
- [Morgan fingerprints](examples/rdkit/example.php) with RDKit
- [Recommendations](examples/disco/example.php) with Disco
- [Horizontal scaling](examples/citus/example.php) with Citus
- [Bulk loading](examples/loading/example.php) with `COPY`

### Laravel

[](#laravel)

Install the package

```
composer require pgvector/pgvector
```

Enable the extension

```
php artisan vendor:publish --tag="pgvector-migrations"
php artisan migrate
```

You can now use the `vector` type in future migrations

```
Schema::create('items', function (Blueprint $table) {
    $table->vector('embedding', 3);
});
```

Update your model

```
use Pgvector\Laravel\Vector;

class Item extends Model
{
    use HasNeighbors;

    protected $casts = ['embedding' => Vector::class];
}
```

Insert a vector

```
$item = new Item();
$item->embedding = [1, 2, 3];
$item->save();
```

Get the nearest neighbors to a record

```
use Pgvector\Laravel\Distance;

$neighbors = $item->nearestNeighbors('embedding', Distance::L2)->take(5)->get();
```

Also supports `InnerProduct`, `Cosine`, `L1`, `Hamming`, and `Jaccard` distance

Get the nearest neighbors to a vector

```
$neighbors = Item::query()->nearestNeighbors('embedding', [1, 2, 3], Distance::L2)->take(5)->get();
```

Get the distances

```
$neighbors->pluck('neighbor_distance');
```

Add an approximate index in a migration

```
public function up()
{
    DB::statement('CREATE INDEX my_index ON items USING hnsw (embedding vector_l2_ops)');
    // or
    DB::statement('CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
}

public function down()
{
    DB::statement('DROP INDEX my_index');
}
```

Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance

### Doctrine

[](#doctrine)

Install the package

```
composer require pgvector/pgvector
```

Register the types and distance functions

```
use Pgvector\Doctrine\PgvectorSetup;

PgvectorSetup::registerTypes($entityManager);
```

Enable the extension

```
$entityManager->getConnection()->executeStatement('CREATE EXTENSION IF NOT EXISTS vector');
```

Update your model

```
use Pgvector\Vector;

#[ORM\Entity]
class Item
{
    #[ORM\Column(type: 'vector', length: 3)]
    private Vector $embedding;

    public function setEmbedding(Vector $embedding): void
    {
        $this->embedding = $embedding;
    }
}
```

Insert a vector

```
$item = new Item();
$item->setEmbedding(new Vector([1, 2, 3]));
$entityManager->persist($item);
$entityManager->flush();
```

Get the nearest neighbors to a vector

```
$neighbors = $entityManager->createQuery('SELECT i FROM Item i ORDER BY l2_distance(i.embedding, ?1)')
    ->setParameter(1, new Vector([1, 2, 3]))
    ->setMaxResults(5)
    ->getResult();
```

Also supports `max_inner_product`, `cosine_distance`, `l1_distance`, `hamming_distance`, and `jaccard_distance`

### PgSql

[](#pgsql)

Enable the extension

```
pg_query($db, 'CREATE EXTENSION IF NOT EXISTS vector');
```

Create a table

```
pg_query($db, 'CREATE TABLE items (embedding vector(3))');
```

Insert a vector

```
use Pgvector\Vector;

$embedding = new Vector([1, 2, 3]);
pg_query_params($db, 'INSERT INTO items (embedding) VALUES ($1)', [$embedding]);
```

Get the nearest neighbors to a vector

```
$embedding = new Vector([1, 2, 3]);
$result = pg_query_params($db, 'SELECT * FROM items ORDER BY embedding  $1 LIMIT 5', [$embedding]);
```

Add an approximate index

```
pg_query($db, 'CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
// or
pg_query($db, 'CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
```

See a [full example](examples/pgsql/example.php)

Reference
---------

[](#reference)

### Vectors

[](#vectors)

Create a vector from an array

```
$vec = new Vector([1, 2, 3]);
```

Get an array

```
$arr = $vec->toArray();
```

### Half Vectors

[](#half-vectors)

Create a half vector from an array

```
$vec = new HalfVector([1, 2, 3]);
```

Get an array

```
$arr = $vec->toArray();
```

### Sparse Vectors

[](#sparse-vectors)

Create a sparse vector from an indexed array

```
$vec = new SparseVector([1, 0, 2, 0, 3, 0]);
```

Or an associative array of non-zero elements

```
$elements = [0 => 1, 2 => 2, 4 => 3];
$vec = new SparseVector($elements, 6);
```

Note: Indices start at 0

Get the number of dimensions

```
$dim = $vec->dimensions();
```

Get the indices of non-zero elements

```
$indices = $vec->indices();
```

Get the values of non-zero elements

```
$values = $vec->values();
```

Get an array

```
$arr = $vec->toArray();
```

History
-------

[](#history)

View the [changelog](https://github.com/pgvector/pgvector-php/blob/master/CHANGELOG.md)

Contributing
------------

[](#contributing)

Everyone is encouraged to help improve this project. Here are a few ways you can help:

- [Report bugs](https://github.com/pgvector/pgvector-php/issues)
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-php/pulls)
- Write, clarify, or fix documentation
- Suggest or add new features

To get started with development:

```
git clone https://github.com/pgvector/pgvector-php.git
cd pgvector-php
composer install
createdb pgvector_php_test
composer test
```

To run an example:

```
cd examples/loading
composer install
createdb pgvector_example
php example.php
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance63

Regular maintenance activity

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 94.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Total

3

Last Release

229d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/628ea07c9c699ff5eeb183ade9e7742ce7304fb4e5b9717f091d66a504c99ea3?d=identicon)[TigerKK](/maintainers/TigerKK)

---

Top Contributors

[![ankane](https://avatars.githubusercontent.com/u/220358?v=4)](https://github.com/ankane "ankane (184 commits)")[![k99k5](https://avatars.githubusercontent.com/u/18001536?v=4)](https://github.com/k99k5 "k99k5 (10 commits)")[![C0chett0](https://avatars.githubusercontent.com/u/1436541?v=4)](https://github.com/C0chett0 "C0chett0 (1 commits)")

### Embed Badge

![Health badge](/badges/k99k5-hyperf-pgvector/health.svg)

```
[![Health](https://phpackages.com/badges/k99k5-hyperf-pgvector/health.svg)](https://phpackages.com/packages/k99k5-hyperf-pgvector)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
