voyanara/milvus-php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

voyanara/milvus-php-sdk

Composer 安装命令:

composer require voyanara/milvus-php-sdk

包简介

A modern, type-safe PHP SDK for Milvus vector database. This library provides a clean, intuitive interface for managing collections, users, roles, and privileges in Milvus through its REST API.

README 文档

README

PHP Version License Latest Version on Packagist Total Downloads

A modern, type-safe PHP SDK for Milvus vector database API v2. This library provides a clean, intuitive interface for managing collections, users, roles, and privileges in Milvus through its REST API.

Built with Saloon HTTP for robust API communication, this SDK focuses on developer experience with proper type hints, comprehensive error handling, and a fluent API design.

Perfect for applications requiring vector similarity search, AI/ML workflows, and large-scale data processing with semantic search capabilities.

Requirements

  • PHP 8.1 or higher
  • Milvus 2.6.x (or compatible versions)
  • Laravel 10.x, 11.x, 12.x (for Laravel integration)

Versions

Milvus Version SDK Version
v2.6.x v1.0.x

Documentation

Installation

You can install the package via Composer:

composer require voyanara/milvus-php-sdk

Docker Development Environment

For development and testing, you can quickly spin up a Milvus instance using the included Docker Compose configuration:

docker-compose up -d

This will start Milvus with all necessary dependencies (etcd, MinIO) and expose it on the default port 19530.

Laravel Integration

This package includes Laravel service provider for seamless integration:

# After installation, publish the configuration file
php artisan milvus-php-sdk:install

This command will publish the configuration file to config/milvus-php-sdk.php where you can set your Milvus connection parameters.

Configuration

Add the following environment variables to your .env file:

# For local development with Docker
MILVUS_TOKEN=root:Milvus
MILVUS_HOST=http://localhost
MILVUS_PORT=19530

# For Zilliz Cloud (hosted Milvus)
MILVUS_TOKEN=db_randomstring:your_password
MILVUS_HOST=https://in03.serverless.gcp-us-west1.cloud.zilliz.com
MILVUS_PORT=443

Roadmap

✅ Implemented Features

  • User Management - Complete user operations (create, describe, drop, list, update password)
  • Role Management - Full role-based access control (create, drop, describe, list, grant/revoke privileges)
  • Collection Management - Complete collection operations and schema management (create, drop, describe, list, load, release, rename, etc.)
  • Vector Operations - Complete vector data operations (insert, search, query, upsert, get, delete, hybrid search)

🚧 In Development

  • Index Management - Vector index creation and optimization

📋 Planned Features

  • Alias Management - Collection alias operations
  • Database Management - Multi-database support
  • Import Operations - Bulk data import functionality
  • Partition Management - Data partitioning for better performance
  • Resource Group - Resource allocation and management

Quick Start

Basic Usage

<?php

use Voyanara\MilvusSdk\Milvus;

// Initialize the client
$milvus = new Milvus(
    token: 'your_token_here',
    host: 'http://localhost',
    port: '19530'
);

// Example: List all users
$users = $milvus->user()->list();

Using with Zilliz Cloud

For Zilliz Cloud (hosted Milvus), configure the client with your cloud credentials:

<?php

use Voyanara\MilvusSdk\Milvus;

$milvus = new Milvus(
    token: "db_randomstring:your_password",
    host: 'https://in03.serverless.gcp-us-west1.cloud.zilliz.com',
    port: '443'
);

// Now you can use all SDK features with Zilliz Cloud
$collections = $milvus->collection()->list();

Using with Laravel

Once configured, you can use the Milvus facade throughout your Laravel application:

<?php

use Voyanara\MilvusSdk\Facades\Milvus;

// User management
$users = Milvus::user()->list();
$user = Milvus::user()->describe('username');

// Role management  
$roles = Milvus::role()->list();
$role = Milvus::role()->describe('role_name');

// Collection management
$collections = Milvus::collection()->list();
$collection = Milvus::collection()->describe('collection_name');

// Create a new collection with schema and index
$schema = [
    'fields' => [
        [
            'fieldName' => 'id',
            'dataType' => 'Int64', 
            'isPrimary' => true
        ],
        [
            'fieldName' => 'vector',
            'dataType' => 'FloatVector',
            'elementTypeParams' => ['dim' => '128']
        ]
    ]
];

$indexParams = [
    [
        'fieldName' => 'vector',
        'indexName' => 'vector_index', 
        'metricType' => 'L2'
    ]
];

Milvus::collection()->createCollection('my_collection', $schema, $indexParams);

// Vector operations
// Insert vector data
$vectorData = [
    [
        'id' => 1,
        'vector' => [0.1, 0.2, 0.3, 0.4, 0.5],
        'metadata' => 'document1'
    ],
    [
        'id' => 2, 
        'vector' => [0.6, 0.7, 0.8, 0.9, 1.0],
        'metadata' => 'document2'
    ]
];

Milvus::vector()->insert('my_collection', $vectorData);

// Search for similar vectors
$queryVector = [[0.1, 0.2, 0.3, 0.4, 0.5]];
$searchResults = Milvus::vector()->search(
    collectionName: 'my_collection',
    data: $queryVector,
    annsField: 'vector',
    limit: 10,
    outputFields: ['id', 'metadata']
);

// Upsert (insert or update) vector data
$upsertData = [
    [
        'id' => 1,
        'vector' => [0.2, 0.3, 0.4, 0.5, 0.6], // Updated vector
        'metadata' => 'document1_updated'
    ]
];

Milvus::vector()->upsert('my_collection', $upsertData);

Vector Operations Examples

Vector Search

Perform semantic search using vector embeddings:

<?php

use Voyanara\MilvusSdk\Milvus;

// Initialize Milvus client
$milvus = new Milvus(
    token: 'root:Milvus',
    host: 'http://localhost',
    port: '19530'
);

// Prepare query vectors (can be multiple vectors)
$queryVectors = [
    [0.3580376395471989, -0.6023495712049978, 0.18414012509913835],
    [0.19886812562848388, 0.06023560599112088, 0.6976963061752597]
];

// Basic vector search
$response = $milvus->vector()->search(
    collectionName: 'documents_collection',
    data: $queryVectors,
    annsField: 'content_vector',
    limit: 5,
    outputFields: ['id', 'title', 'category']
);

// Search with filtering
$response = $milvus->vector()->search(
    collectionName: 'documents_collection', 
    data: $queryVectors,
    annsField: 'content_vector',
    filter: "category == 'technology' and publish_date >= '2024-01-01'",
    limit: 10,
    outputFields: ['id', 'title', 'content']
);

// Advanced search with custom parameters
$searchParams = [
    'metricType' => 'L2',
    'params' => [
        'radius' => 0.1,
        'range_filter' => 0.9
    ]
];

$response = $milvus->vector()->search(
    collectionName: 'documents_collection',
    data: $queryVectors,
    annsField: 'content_vector', 
    searchParams: $searchParams,
    limit: 20,
    offset: 10  // Pagination support
);

// Process results
foreach ($response->json('data') as $result) {
    echo "Document ID: {$result['id']}, Distance: {$result['distance']}\n";
    echo "Title: {$result['title']}\n";
}

Vector Upsert

Insert new vectors or update existing ones based on primary key:

<?php

use Voyanara\MilvusSdk\Milvus;

// Initialize Milvus client
$milvus = new Milvus(
    token: 'root:Milvus',
    host: 'http://localhost', 
    port: '19530'
);

// Prepare document vectors for upsert
$documents = [
    [
        'id' => 1,
        'content_vector' => [0.1, 0.2, 0.3, 0.4, 0.5],
        'title' => 'Introduction to AI',
        'category' => 'technology',
        'publish_date' => '2024-01-15'
    ],
    [
        'id' => 2,
        'content_vector' => [0.6, 0.7, 0.8, 0.9, 1.0], 
        'title' => 'Machine Learning Basics',
        'category' => 'technology',
        'publish_date' => '2024-02-01'
    ],
    [
        'id' => 3,
        'content_vector' => [0.2, 0.4, 0.6, 0.8, 0.1],
        'title' => 'Deep Learning Guide', 
        'category' => 'technology',
        'publish_date' => '2024-03-10'
    ]
];

// Upsert documents (will insert new or update existing based on ID)
$response = $milvus->vector()->upsert(
    collectionName: 'documents_collection',
    data: $documents
);

echo "Upserted {$response->json('data.upsertCount')} documents\n";
print_r($response->json('data.upsertIds'));

// Upsert to specific partition
$response = $milvus->vector()->upsert(
    collectionName: 'documents_collection',
    data: $documents,
    partitionName: 'tech_partition'
);

// Single document upsert
$singleDocument = [
    [
        'id' => 100,
        'content_vector' => [0.3, 0.1, 0.4, 0.7, 0.2],
        'title' => 'Updated Document Title',
        'category' => 'science'
    ]
];

$milvus->vector()->upsert('documents_collection', $singleDocument);

Creating a Milvus Collection

Quick example of creating a collection with vector field:

<?php

use Voyanara\MilvusSdk\Milvus;

// Initialize client
$milvus = new Milvus(
    token: 'root:Milvus',
    host: 'http://localhost',
    port: '19530'
);

// Define collection schema
$schema = [
    'fields' => [
        [
            'fieldName' => 'id',
            'dataType' => 'Int64',
            'isPrimary' => true
        ],
        [
            'fieldName' => 'title',
            'dataType' => 'VarChar',
            'elementTypeParams' => ['max_length' => 200]
        ],
        [
            'fieldName' => 'content_vector',
            'dataType' => 'FloatVector',
            'elementTypeParams' => ['dim' => 768] // 768-dimensional vectors
        ]
    ]
];

// Define vector index
$indexParams = [
    [
        'fieldName' => 'content_vector',
        'indexName' => 'content_vector_index',
        'metricType' => 'L2'
    ]
];

// Create collection
$response = $milvus->collection()->createCollection(
    collectionName: 'my_documents',
    schema: $schema,
    indexParams: $indexParams
);

// Load collection into memory for operations
$milvus->collection()->loadCollection('my_documents');

echo "Collection 'my_documents' created successfully!\n";

License

This project is licensed under the MIT License - see the LICENSE file for details.

voyanara/milvus-php-sdk 适用场景与选型建议

voyanara/milvus-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 77 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「sdk」 「laravel」 「machine learning」 「ai」 「embeddings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 voyanara/milvus-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 voyanara/milvus-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 voyanara/milvus-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 77
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-20