PHP code example of voyanara / milvus-php-sdk
1. Go to this page and download the library: Download voyanara/milvus-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
voyanara / milvus-php-sdk example snippets
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();
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();
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);
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";
}
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);
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";
bash
composer bash
# After installation, publish the configuration file
php artisan milvus-php-sdk:install