seijikun/php-sonic

此包已被弃用且不再维护。未建议替代包。

用于 Sonic 全文搜索引擎的 PHP 客户端

0.0.3 2019-03-28 16:18 UTC

This package is auto-updated.

Last update: 2021-11-28 22:27:58 UTC


README

同步 PHP 客户端库,用于 sonic 搜索后端。客户端尽可能简单,仅包含一个文件。

API

Sonic 需要用户在每次连接中选择会话的类型/模式,之后无法更改所选模式。这导致了以下 API 设计:连接到 Sonic 时,用户可在三个类 SearchSessionIngestSessionControlSession 之间选择,每个类都类似于 Sonic 的一种可能的操作模式

导入

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