ankane/ngt
Composer 安装命令:
composer require ankane/ngt
包简介
High-speed approximate nearest neighbors for PHP
README 文档
README
NGT - high-speed approximate nearest neighbors - for PHP
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
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
$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
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
This library is modeled after NGT’s Python API.
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- 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
ankane/ngt 适用场景与选型建议
ankane/ngt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.73k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2022 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ankane/ngt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ankane/ngt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2022-11-19