PHP code example of mathsgod / milvus-client-php

1. Go to this page and download the library: Download mathsgod/milvus-client-php 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/ */

    

mathsgod / milvus-client-php example snippets


use 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
));

$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",
    reqs: [
        new AnnSearchRequest(
            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 AnnSearchRequest(
            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",
    reqs: [
        new AnnSearchRequest(
            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"]
);
bash
composer