A PHP client for Milvus 2.6.x.
composer require mathsgod/milvus-client-phpuse Milvus\Client;
$client = new Client(
"http://localhost:19530", // Milvus server URI
"username", // Optional: username
"password", // Optional: password
"default", // Optional: database name
"token" // Optional: JWT token
);$client = new Milvus\Client();
$client->createCollection(
collection_name: "testing",
schema: $client->createSchema()
->addField("id", Milvus\DataType::INT64, is_primary: true)
->addField("array", Milvus\DataType::ARRAY, element_type: Milvus\DataType::INT64, max_capacity: 10)
->addField("vector", Milvus\DataType::FLOAT_VECTOR, dim: 5)
->addField("text", Milvus\DataType::VARCHAR, max_length: 1000, nullable: true)
->addField("metadata", Milvus\DataType::JSON, nullable: true),
index_params: $client->prepareIndexParams()
->addIndex(
field_name: "vector",
index_name: "my_index",
index_type: Milvus\IndexType::AUTOINDEX,
metric_type: Milvus\MetricType::COSINE
),
);
// Insert data
$client->insert(
collection_name: "testing",
data: [
[
"id" => 1,
"array" => [1, 2, 3],
"vector" => [0.1, 0.2, 0.3, 0.4, 0.5],
"text" => "Hello World",
"metadata" => ["key1" => "value1", "key2" => "value2"]
],
[
"id" => 2,
"array" => [4, 5, 6],
"vector" => [0.6, 0.7, 0.8, 0.9, 1.0],
"text" => null,
"metadata" => null
]
]
);
print_R($client->search(
collection_name: "testing",
data: [[0.1, 0.2, 0.3, 0.4, 0.5]],
anns_field: "vector",
limit: 3,
output_fields: ["id", "vector", "text", "metadata"],
));Array
(
[0] => Array
(
[distance] => 0.99999994
[id] => 1
[metadata] => {"key1":"value1","key2":"value2"}
[text] => Hello World
[vector] => Array
(
[0] => 0.1
[1] => 0.2
[2] => 0.3
[3] => 0.4
[4] => 0.5
)
)
[1] => Array
(
[distance] => 0.96495044
[id] => 2
[metadata] =>
[text] =>
[vector] => Array
(
[0] => 0.6
[1] => 0.7
[2] => 0.8
[3] => 0.9
[4] => 1
)
)
)$client = new Milvus\Client();
$client->dropCollection("testing2");
// Full-Text Search
$client->createCollection(
collection_name: "testing2",
schema: $client->createSchema()
->addField("id", Milvus\DataType::INT64, is_primary: true)
->addField("text_sparse", Milvus\DataType::SPARSE_FLOAT_VECTOR)
->addField("document", Milvus\DataType::VARCHAR, max_length: 1000, enable_analyzer: true, enable_match: true)
->addFunction(
name: "bm25",
function_type: Milvus\FunctionType::BM25,
input_field_names: ["document"],
output_field_names: ["text_sparse"]
),
index_params: $client->prepareIndexParams()
->addIndex(
field_name: "text_sparse",
index_name: "text_sparse_index",
index_type: Milvus\IndexType::SPARSE_INVERTED_INDEX,
metric_type: Milvus\MetricType::BM25
),
);
// Insert data
$client->insert(
collection_name: "testing2",
data: [
[
"id" => 1,
"document" => "This is a sample document for testing.",
],
[
"id" => 2,
"document" => "Another document for testing purposes.",
],
[
"id" => 3,
"document" => "Milvus is a vector database designed for scalable similarity search.",
],
[
"id" => 4,
"document" => "Full-text search enables users to find relevant documents quickly.",
],
[
"id" => 5,
"document" => "This document contains information about PHP and Milvus integration.",
],
[
"id" => 6,
"document" => "Testing the search functionality with various sample documents.",
],
[
"id" => 7,
"document" => "Another example document to increase the dataset size.",
],
[
"id" => 8,
"document" => "Sample data helps in validating the search and indexing features.",
]
]
);
print_r($client->search(
collection_name: "testing2",
data: ['sample'],
anns_field: "text_sparse",
limit: 5
));For more details about embedding functions, please refer to Milvus Embedding Function Overview.
$client = new Milvus\Client();
// Text Embedding with Azure OpenAI
$client->createCollection(
collection_name: "testing",
schema: $client->createSchema()
->addField("id", Milvus\DataType::INT64, is_primary: true)
->addField("document", Milvus\DataType::VARCHAR, max_length: 1000, enable_analyzer: true, enable_match: true)
->addField("dense", Milvus\DataType::FLOAT_VECTOR, dim: 1536)
->addFunction(
name: "azure_embedding",
function_type: Milvus\FunctionType::TEXT_EMBEDDING,
input_field_names: ["document"],
output_field_names: ["dense"],
params: [
"provider" => "azure_openai",
"model_name" => "text-embedding-3-small"
]
),
index_params: $client->prepareIndexParams()
->addIndex(
field_name: "dense",
index_name: "dense_index",
index_type: Milvus\IndexType::AUTOINDEX,
metric_type: Milvus\MetricType::COSINE
),
);
// Insert data (embeddings will be automatically generated)
$client->insert(
collection_name: "testing",
data: [
[
"id" => 1,
"document" => "This is a sample document for embedding.",
],
[
"id" => 2,
"document" => "Another document to test text embedding functionality.",
],
[
"id" => 3,
"document" => "Milvus supports automatic text embedding generation.",
],
[
"id" => 4,
"document" => "Azure OpenAI provides powerful embedding models.",
]
]
);
// Search using embedding (the query text will be automatically embedded)
print_r($client->search(
collection_name: "testing",
data: ["sample document embedding"],
anns_field: "dense",
limit: 3,
output_fields: ["id", "document"]
));Array
(
[0] => Array
(
[distance] => 0.712204
[document] => This is a sample document for embedding.
[id] => 1
)
[1] => Array
(
[distance] => 0.6992143
[document] => Another document to test text embedding functionality.
[id] => 2
)
[2] => Array
(
[distance] => 0.43827245
[document] => Azure OpenAI provides powerful embedding models.
[id] => 4
)
)$client = new Milvus\Client();
$client->dropCollection("testing");
// Hybrid Search
$client->createCollection(
collection_name: "testing",
schema: $client->createSchema()
->addField("id", Milvus\DataType::INT64, is_primary: true)
->addField("vector", Milvus\DataType::FLOAT_VECTOR, dim: 5)
->addField("document", Milvus\DataType::VARCHAR, max_length: 1000, enable_analyzer: true, enable_match: true)
->addField("text_sparse", Milvus\DataType::SPARSE_FLOAT_VECTOR)
->addFunction(
name: "bm25",
function_type: Milvus\FunctionType::BM25,
input_field_names: ["document"],
output_field_names: ["text_sparse"]
),
index_params: $client->prepareIndexParams()
->addIndex(
field_name: "vector",
index_name: "my_index",
index_type: Milvus\IndexType::AUTOINDEX,
metric_type: Milvus\MetricType::COSINE
)->addIndex(
field_name: "text_sparse",
index_name: "text_sparse_index",
index_type: Milvus\IndexType::SPARSE_INVERTED_INDEX,
metric_type: Milvus\MetricType::BM25
),
);
// Insert data
$client->insert(
collection_name: "testing",
data: [
[
"id" => 1,
"vector" => [0.1, 0.2, 0.3, 0.4, 0.5],
"document" => "This is a sample document for testing.",
],
[
"id" => 2,
"vector" => [0.6, 0.7, 0.8, 0.9, 1.0],
"document" => "Another document for testing purposes.",
],
[
"id" => 3,
"vector" => [0.1, 0.2, 0.3, 0.4, 0.5],
"document" => "Milvus is a vector database designed for scalable similarity search.",
],
[
"id" => 4,
"vector" => [0.6, 0.7, 0.8, 0.9, 1.0],
"document" => "Full-text search enables users to find relevant documents quickly.",
],
]
);
$query = "sample document";
print_r($client->hybridSearch(
collection_name: "testing",
search: [
new HybridSearchRequest(
data: [[0.1, 0.2, 0.3, 0.4, 0.5]], // embedding vector of the query
anns_field: "vector",
limit: 10,
param: ["nprobe" => 10] // search parameters
),
new HybridSearchRequest(
data: [$query], // query string
anns_field: "text_sparse",
limit: 10,
param: ["drop_ratio_search" => 0.2]
)
],
ranker: new WeightedRanker([0.5, 0.5]),
output_fields: ["id", "document"]
));$client->createDatabase("my_db");$client->usingDatabase("my_db");$dbs = $client->listDatabases();$info = $client->describeDatabase("my_db");$client->dropDatabase("my_db");$client->createCollection(
collection_name: "test_collection",
schema: $client->createSchema()
->addField("id", Milvus\DataType::INT64, is_primary: true)
->addField("array", Milvus\DataType::ARRAY, element_type: Milvus\DataType::INT64, max_capacity: 10)
->addField("vector", Milvus\DataType::FLOAT_VECTOR, dim: 5)
->addField("text", Milvus\DataType::VARCHAR, max_length: 1000, nullable: true)
->addField("metadata", Milvus\DataType::JSON, nullable: true),
index_params: $client->prepareIndexParams()
->addIndex(
field_name: "vector",
index_name: "my_index",
index_type: Milvus\IndexType::AUTOINDEX,
metric_type: Milvus\MetricType::COSINE
),
);$collections = $client->listCollections();$desc = $client->describeCollection("test_collection");$client->dropCollection("test_collection");$client->loadCollection("test_collection");
$client->releaseCollection("test_collection");$client->renameCollection("old_name", "new_name");$entities = [
["id" => 1, "vector" => [1.0, 2.0, 3.0, 4.0, 5.0]],
["id" => 2, "vector" => [2.0, 2.0, 3.0, 4.0, 5.0]],
];
$client->insert("test_collection", $entities);$client->upsert("test_collection", $entities);$result = $client->query("test_collection", "id in [1,2]");$client->delete("test_collection", "id in [1]");$result = $client->search(
collection_name: "test_collection",
data: [[1.0, 2.0, 3.0, 4.0, 5.0]],
anns_field: "vector",
limit: 10,
output_fields: ["id", "vector"]
); $indexParams = $client->prepareIndexParams();
$indexParams->addIndex(
field_name: "vector",
index_name: "my_index",
index_type: Milvus\IndexType::AUTOINDEX,
metric_type: Milvus\MetricType::COSINE
);
$client->createIndex("test_collection", $indexParams);$indexes = $client->listIndexes("test_collection");$users = $client->listUsers();$client->createUser("test_user", "password");$userInfo = $client->describeUser("test_user");$client->dropUser("test_user");$client->updatePassword("test_user", "old_password", "new_password");$roles = $client->listRoles();$client->createRole("admin");$roleInfo = $client->describeRole("admin");$client->dropRole("admin");$client->grantPrivilege("admin", "Collection", "Insert", "test_collection");$result = $client->hybridSearch(
collection_name: "test_collection",
search: [
new HybridSearchRequest(
data: [[0.1, 0.2, 0.3, 0.4, 0.5]],
anns_field: "vector",
limit: 10,
param: []
),
],
ranker: new Milvus\RRFRanker(10),
output_fields: ["id", "vector"]
);