PHPackages                             ankane/ngt - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ankane/ngt

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ankane/ngt
==========

High-speed approximate nearest neighbors for PHP

v0.3.0(4mo ago)84.9k↑19.3%1Apache-2.0PHPPHP &gt;= 8.2CI passing

Since Nov 19Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/ankane/ngt-php)[ Packagist](https://packagist.org/packages/ankane/ngt)[ RSS](/packages/ankane-ngt/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (5)Used By (0)

NGT PHP
=======

[](#ngt-php)

[NGT](https://github.com/yahoojapan/NGT) - high-speed approximate nearest neighbors - for PHP

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

Installation
------------

[](#installation)

Run:

```
composer require ankane/ngt
```

Add scripts to `composer.json` to download the shared library:

```
    "scripts": {
        "post-install-cmd": "Ngt\\Vendor::check",
        "post-update-cmd": "Ngt\\Vendor::check"
    }
```

And run:

```
composer install
```

On Mac, also install OpenMP:

```
brew install libomp
```

NGT is not available for Windows

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

[](#getting-started)

Prep your data

```
$objects = [
    [1, 1, 2, 1],
    [5, 4, 6, 5],
    [1, 2, 1, 2]
];
```

Create an index

```
$index = new Ngt\Index($dimensions);
```

Insert objects

```
$index->batchInsert($objects);
```

Search the index

```
$index->search($query, size: 3);
```

Save the index

```
$index->save($path);
```

Load an index

```
$index = Ngt\Index::load($path);
```

Get an object by id

```
$index->object($id);
```

Insert a single object

```
$index->insert($object);
```

Remove an object by id

```
$index->remove($id);
```

Build the index

```
$index->buildIndex();
```

Full Example
------------

[](#full-example)

```
$dim = 10;
$objects = [];
for ($i = 0; $i < 100; $i++) {
    $object = [];
    for ($j = 0; $j < $dim; $j++) {
        $object[] = rand(0, 100);
    }
    $objects[] = $object;
}

$index = new Ngt\Index($dim);
$index->batchInsert($objects);

$query = $objects[0];
$result = $index->search($query, size: 3);

foreach ($result as $res) {
    print($res['id'] . ', ' . $res['distance'] . "\n");
}
```

Index Options
-------------

[](#index-options)

Defaults shown below

```
use Ngt\DistanceType;
use Ngt\ObjectType;

new Ngt\Index(
    $dimensions,
    edgeSizeForCreation: 10,
    edgeSizeForSearch: 40,
    distanceType: DistanceType::L2,  // L1, L2, Hamming, Angle, Cosine, NormalizedAngle, NormalizedCosine, Jaccard
    objectType: ObjectType::Float    // Float, Float16, Integer
);
```

Credits
-------

[](#credits)

This library is modeled after NGT’s [Python API](https://github.com/yahoojapan/NGT/blob/master/python/README-ngtpy.md).

History
-------

[](#history)

View the [changelog](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/ankane/ngt-php/issues)
- Fix bugs and [submit pull requests](https://github.com/ankane/ngt-php/pulls)
- Write, clarify, or fix documentation
- Suggest or add new features

To get started with development:

```
git clone https://github.com/ankane/ngt-php.git
cd ngt-php
composer install
composer test
```

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance79

Regular maintenance activity

Popularity29

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~398 days

Total

4

Last Release

125d ago

PHP version history (3 changes)v0.1.0PHP &gt;= 8.0

v0.2.0PHP &gt;= 8.1

v0.3.0PHP &gt;= 8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/220358?v=4)[Andrew Kane](/maintainers/ankane)[@ankane](https://github.com/ankane)

---

Top Contributors

[![ankane](https://avatars.githubusercontent.com/u/220358?v=4)](https://github.com/ankane "ankane (32 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ankane-ngt/health.svg)

```
[![Health](https://phpackages.com/badges/ankane-ngt/health.svg)](https://phpackages.com/packages/ankane-ngt)
```

PHPackages © 2026

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