public function closeConnection() { if (!$this->isConnected) { throw new ESphinxException("Sphinx client is already closed"); } else { $this->sphinxClient->Open(); $this->isConnected = false; } }
/** * Open Sphinx persistent connection. * * @throws ESphinxException if client is already connected. * @throws ESphinxException if client has connection error. * @link http://sphinxsearch.com/docs/current.html#api-func-open */ public function openConnection() { if ($this->isConnected) { throw new ESphinxException("Sphinx client is already opened"); } $this->sphinxClient->Open(); if ($this->sphinxClient->IsConnectError()) { throw new ESphinxException("Sphinx exception: " . $this->sphinxClient->GetLastError()); } $this->isConnected = true; }
public static function getInstance($sphinxconf, $type = 'media') { $time = microtime(true); $rand = intval(substr($time, 11, 4)); $num = count($sphinxconf); $index = $rand % $num; $ret = false; if (null == self::$_sphinxpool[$type]) { $try_count = 0; while ($try_count < 3 && $ret != true) { $client = new SphinxClient(); $index = $index % $num; try { $client->setServer($sphinxconf[$index]['host'], $sphinxconf[$index]['port']); self::$_index[$type] = $sphinxconf[$index]['indexer']; $ret = $client->Open(); if ($ret == true) { self::$_conf_num[$type] = $index; break; } Systemlog::fatal("sphinx connect fail time:" . $client->_host . " " . $client->_port); $try_count++; $index++; } catch (Exception $e) { $index++; $try_count++; } } self::$_sphinxpool[$type] = $client; } return self::$_sphinxpool[$type]; }
<?php require 'sphinxapi.php'; $dbLocal = new PDO('mysql:host=localhost;dbname=words', 'root', 'root'); $dbData = new PDO('mysql:host=localhost;dbname=words', 'root', 'root'); $sql = 'set names utf8'; $dbLocal->exec($sql); $dbData->exec($sql); $sphinx = new SphinxClient(); @$sphinx->SetServer('127.0.0.1', '9312'); $sphinx->Open(); $spamArray = array(); $healthArray = array(); function getAllKey($sql, $dbData, $sphinx) { $allKey = array(); $result = $dbData->query($sql); while ($row = $result->fetch()) { //$pattern = '/[a-zA-Z0-9\x{4e00}-\x{9fa5}]+/u'; //$pattern = '/[\x{4e00}-\x{9fa5}]+/u'; //preg_match_all($pattern, $row['content'], $content); //$content = join('', $content[0]); $key = $sphinx->buildKeywords($row['content'], 'mysql', false); $num = count($key); if ($num) { for ($i = 0; $i < $num; $i++) { $key[$i] = $key[$i]['tokenized']; } } $key = array_unique($key); $allKey = array_merge($key, $allKey);
<?php require "spec/fixtures/sphinxapi.php"; $cl = new SphinxClient(); $cl->Open();