PHP code example of seijikun / php-sonic

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

    

seijikun / php-sonic example snippets


$client = new \SonicSearch\IngestSession('localhost', 1491, 'SecretPassword');
try {
        $client->connect();
} catch(\SonicSearch\SonicUnreachableException $e) {
        echo "Failed to connect to Sonic:";
        var_dump($e);
        exit;
}

// Ping sonic (to keep the session alive, e.g.)
$client->ping();

/* Ask sonic to clear the messages index */
$client->flush('messages');
// To delete more specific paths:
$client->flush('messages', 'default');
$client->flush('messages', 'default', 'conversation:x1337x');

/* Let's imagine having a chat conversation and push some messages into Sonic's index */
$client->push('messages', 'default', 'conversation:x1337x', 'Sonic sounds interesting. Lets see how it performs as backend for nextcluods fulltextsearch framework!');
$client->push('messages', 'default', 'conversation:x1337x', 'Haha nice idea! Elasticsearch is damned slow!');

/* Let's ask Sonic to count some stuff for us */
echo "Amount of collections: " . $client->count('messages');
echo "Amount of conversations: " . $client->count('messages', 'default');
echo "Amount of Terms in conversation: " . $client->count('messages', 'default', 'conversation:x1337x');

$client = new \SonicSearch\SearchSession('localhost', 1491, 'SecretPassword');
try {
        $client->connect();
} catch(\SonicSearch\SonicUnreachableException $e) {
        echo "Failed to connect to Sonic:";
        var_dump($e);
        exit;
}

// Ping sonic (to keep the session alive, e.g.)
$client->ping();

// Query Sonic
$results = $client->query('messages', 'default', 'Elasticsearch slow');
var_dump($results);

// Let Sonic suggest auto-completion for some words
var_dump($client->suggest('messages', 'default', 'So', 1));

$client = new \SonicSearch\SearchSession('localhost', 1491, 'SecretPassword');
try {
        $client->connect();
} catch(\SonicSearch\SonicUnreachableException $e) {
        echo "Failed to connect to Sonic:";
        var_dump($e);
        exit;
}

// Ping sonic (to keep the session alive, e.g.)
$client->ping();

// Query Sonic
$results = $client->query('messages', 'default', 'Elasticsearch slow');
var_dump($results);

// Let Sonic suggest auto-completion for some words
var_dump($client->suggest('messages', 'default', 'So', 1));