hejunjie / milvus
一个轻量级的 PHP 库,用来通过 RESTful API 调用 Milvus 向量数据库,可以用它来管理集合、插入数据,以及进行向量搜索 | A lightweight PHP library for interacting with the Milvus vector database via RESTful API. It allows you to manage collections, insert data, and perform vector searches
Requires
- php: ^8.0
README
English | 简体中文
A lightweight PHP library for interacting with the Milvus vector database via RESTful API — manage collections, insert data, and perform vector searches, all in one place.
This project has been parsed by Zread. Click here for a quick overview of the project structure.
Requirements
- PHP 8.0+
- ext-curl
- ext-json
Installation
composer require hejunjie/milvus
Quick Start
use Hejunjie\Milvus; // Initialize client (get the URL and API Key from Zilliz Cloud console) $client = new Milvus\Client('https://xxx.api.zillizcloud.com', 'your-api-key'); // Create a collection $client->collections()->create('my_collection', [ 'autoID' => true, 'fields' => [ ['fieldName' => 'id', 'dataType' => 'Int64', 'isPrimary' => true], ['fieldName' => 'vector', 'dataType' => 'FloatVector', 'elementTypeParams' => ['dim' => 128]], ['fieldName' => 'title', 'dataType' => 'VarChar', 'elementTypeParams' => ['max_length' => 256]], ] ]); // Create an index (vector fields require an index before searching) $client->indexes()->create('my_collection', [[ 'fieldName' => 'vector', 'indexName' => 'vector_idx', 'metricType' => 'COSINE', 'indexConfig' => ['index_type' => 'AUTOINDEX'], ]]); // Load the collection into memory $client->collections()->load('my_collection'); // Insert data $client->entities()->insert('my_collection', [ ['vector' => array_fill(0, 128, 0.1), 'title' => 'First record'], ['vector' => array_fill(0, 128, 0.2), 'title' => 'Second record'], ]); // Vector search $results = $client->entities()->search('my_collection', [[ 'vector' => array_fill(0, 128, 0.15), ]], 'vector', limit: 10);
API Overview
All methods are accessed via chained calls, organized by module:
Client
| Method | Description |
|---|---|
customize($url, $params) |
Custom request for calling APIs not yet wrapped |
Collections
| Method | Description |
|---|---|
create($name, $schema, $indexParams) |
Create a collection |
describe($name) |
Get collection details |
drop($name) |
Drop a collection |
get_load_state($name) |
Get collection load state |
get_stats($name) |
Get collection statistics |
has($name) |
Check if a collection exists |
list() |
List all collections |
load($name) |
Load a collection into memory |
release($name) |
Release a collection from memory |
rename($name, $newName) |
Rename a collection |
alter_properties($name, $properties) |
Set collection properties (e.g., TTL, mmap) |
drop_properties($name, $keys) |
Remove collection properties |
Partitions
| Method | Description |
|---|---|
create($collectionName, $partitionName) |
Create a partition |
drop($collectionName, $partitionName) |
Drop a partition |
get_stats($collectionName, $partitionName) |
Get partition load state |
has($collectionName, $partitionName) |
Check if a partition exists |
list($collectionName) |
List all partitions |
load($collectionName, $partitionNames) |
Load partitions into memory |
release($collectionName, $partitionNames) |
Release partitions from memory |
Indexes
| Method | Description |
|---|---|
create($collectionName, $indexParams) |
Create an index |
describe($collectionName, $indexName) |
Get index details |
drop($collectionName, $indexName) |
Drop an index |
list($collectionName) |
List all indexes |
Aliases
| Method | Description |
|---|---|
alter($collectionName, $aliasName) |
Alter an alias |
create($collectionName, $aliasName) |
Create an alias |
describe($collectionName, $aliasName) |
Get alias details |
drop($collectionName, $aliasName) |
Drop an alias |
list($collectionName) |
List all aliases |
Entities
| Method | Description |
|---|---|
insert($collectionName, $data, $partitionName) |
Insert data |
upsert($collectionName, $data, $partitionName) |
Insert or update data |
delete($collectionName, $filter, $partitionName) |
Delete data by filter |
get($collectionName, $ids, $outputFields, $partitionNames) |
Get data by primary key |
query($collectionName, $filter, $limit, $offset, $outputFields, $partitionNames) |
Query data by filter |
search($collectionName, $data, $annsField, $limit, $offset, $partitionNames) |
Vector similarity search |
Note
All methods have full PHPDoc comments with parameter details. For more information on Milvus RESTful API parameters, refer to the Zilliz official documentation.
Notes
- This library is built on Milvus RESTful API v2. Make sure your Milvus / Zilliz Cloud instance supports v2 endpoints.
- Before performing vector search, the collection must have an index created and be loaded into memory (via
load()), otherwise the search will fail. - Default request timeout is 10 seconds. Large data inserts may require batching.
- API Key can be
null(for self-hosted Milvus without authentication), but is required for Zilliz Cloud managed service.
Motivation
I was reading short stories on a novel site that didn't support search and had daily reading limits. Frustrated, I wrote a crawler to download all the stories — but then I couldn't find anything in them locally.
My tiny 2-core 2GB server couldn't run a Milvus instance, but Zilliz Cloud offers a free tier. I run vectorization on my Mac, and use SiliconFlow's free quota for vector queries during search. I just needed a PHP library to tie it all together — and that's how this library came to be.
Contributing
If you find this library useful, give it a ⭐ Star, or submit an Issue / PR to share your ideas.