PHP code example of tenqz / qdrant

1. Go to this page and download the library: Download tenqz/qdrant 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/ */

    

tenqz / qdrant example snippets




use Tenqz\Qdrant\QdrantClient;
use Tenqz\Qdrant\Transport\Infrastructure\Factory\CurlHttpClientFactory;

// 1. Create client
$factory = new CurlHttpClientFactory();
$httpClient = $factory->create('localhost', 6333);
$client = new QdrantClient($httpClient);

// 2. Create collection
$client->createCollection('my_collection', 4, 'Cosine');

// 3. Add vectors with data
$client->upsertPoints('my_collection', [
    ['id' => 1, 'vector' => [0.1, 0.2, 0.3, 0.4], 'payload' => ['city' => 'Berlin']],
    ['id' => 2, 'vector' => [0.5, 0.6, 0.7, 0.8], 'payload' => ['city' => 'London']],
]);

// 4. Search for similar vectors
$results = $client->search('my_collection', [0.2, 0.1, 0.9, 0.7], 5);

use Tenqz\Qdrant\Transport\Domain\Exception\TransportException;
use Tenqz\Qdrant\Transport\Domain\Exception\HttpException;
use Tenqz\Qdrant\Transport\Domain\Exception\NetworkException;

try {
    $result = $client->search('my_collection', [0.1, 0.2], 10);
    
} catch (HttpException $e) {
    // HTTP errors (404, 500, etc.)
    echo "HTTP Error {$e->getStatusCode()}: {$e->getMessage()}\n";
    print_r($e->getResponse());
    
} catch (NetworkException $e) {
    // Network problems (connection failed, timeout)
    echo "Network Error: {$e->getMessage()}\n";
    
} catch (TransportException $e) {
    // Other transport errors
    echo "Error: {$e->getMessage()}\n";
}
bash
php examples/basicUsage.php