/** * 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; }
/** * {@inheritdoc} */ public function query($query, $offset, $perPage, SearchEngineOptions $options = null) { if (null === $options) { $options = new SearchEngineOptions(); } $this->applyOptions($options); assert(is_int($offset)); assert($offset >= 0); assert(is_int($perPage)); $query = $this->parseQuery($query); $preg = preg_match('/\\s?(recordid|storyid)\\s?=\\s?([0-9]+)/i', $query, $matches, 0, 0); if ($preg > 0) { $this->sphinx->SetFilter('record_id', [$matches[2]]); $query = ''; } $this->sphinx->SetLimits($offset, $perPage); $this->sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); $index = $this->getQueryIndex($query, $options); $res = $this->sphinx->Query($query, $index); $results = new ArrayCollection(); if ($res === false) { if ($this->sphinx->IsConnectError() === true) { $error = $this->app->trans('Sphinx server is offline'); } else { $error = $this->sphinx->GetLastError(); } $warning = $this->sphinx->GetLastWarning(); $total = $available = $duration = 0; $suggestions = new ArrayCollection(); $propositions = []; } else { $error = $res['error']; $warning = $res['warning']; $duration = $res['time']; $total = $res['total_found']; $available = $res['total']; $resultOffset = $offset; if (isset($res['matches'])) { foreach ($res['matches'] as $record_id => $match) { try { $record = new \record_adapter($this->app, $match['attrs']['sbas_id'], $match['attrs']['record_id'], $resultOffset); $results->add($record); } catch (\Exception $e) { } $resultOffset++; } } $suggestions = $this->getSuggestions($query, $options); $propositions = ''; } return new SearchEngineResult($results, $query, $duration, $offset, $available, $total, $error, $warning, $suggestions, $propositions, $index); }
/** * Search for the specified query string. * * @param string $query The query string that we are searching for. * @param array $indexes The indexes to perform the search on. * * @return ResultCollection The results of the search. * * $indexes should have the format: * * $indexes = array( * 'IndexLabel' => array( * 'result_offset' => (int), * 'result_limit' => (int) * ), * ..., * ); */ public function search($query, array $indexes) { // $query = $this->sphinx->escapeString($query); $results = array(); foreach ($indexes as $label => $options) { /** * Ensure that the label corresponds to a defined index. */ if (!isset($this->indexes[$label])) { continue; } /** * Set the offset and limit for the returned results. */ if (isset($options['result_offset']) && isset($options['result_limit']) && is_numeric($options['result_offset']) && is_numeric($options['result_limit'])) { $this->sphinx->setLimits($options['result_offset'], $options['result_limit']); } /** * Weight the individual fields. */ if (!empty($this->indexes[$label]['field_weights'])) { $this->sphinx->setFieldWeights($this->indexes[$label]['field_weights']); } /** * Perform the query. */ $results[$label] = $this->sphinx->query($query, implode(' ', $this->indexes[$label]["index"])); if ($this->sphinx->IsConnectError()) { throw new ConnectionException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError())); } elseif ($results[$label]['status'] !== SEARCHD_OK) { throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError())); } } /** * FIXME: Throw an exception if $results is empty? */ return new ResultCollection($results, $this->mapping, $this->em); }
public function isConnectError() { return $this->_sphinx->IsConnectError(); }
/** * Return last error * @param bool $conn * @return bool|string */ public function getError($conn = false) { return !$conn ? $this->client->GetLastError() : $this->client->IsConnectError(); }