Sphinx\SphinxClient::query PHP Method

query() public method

Connect to searchd server, run given search query through given indexes, and return the results
public query ( string $query, string $index = '*', string $comment = '' ) : array | false
$query string query string
$index string index name
$comment string optional comment
return array | false Results array, or false upon error.
    public function query($query, $index = '*', $comment = '')
    {
        if (!empty($this->reqs)) {
            throw new \ErrorException('Request array must be empty.');
        }
        $this->addQuery($query, $index, $comment);
        $results = $this->runQueries();
        // just in case it failed too early
        $this->reqs = array();
        if (!is_array($results)) {
            // probably network error; error message should be already filled
            return false;
        }
        $this->error = $results[0]['error'];
        $this->warning = $results[0]['warning'];
        if ($results[0]['status'] === self::SEARCHD_ERROR) {
            return false;
        } else {
            return $results[0];
        }
    }

Usage Example

 /**
  * Search for a query string
  * @param  string  $query   Search query
  * @param  array   $indexes Index list to perform the search
  * @param  boolean $escape  Should the query to be escaped?
  *
  * @return array           Search results
  *
  * @throws EmptyIndexException If $indexes is empty
  * @throws \RuntimeException If seaarch failed
  */
 public function search($query, array $indexes, $escape = true)
 {
     if (empty($indexes)) {
         throw new EmptyIndexException('Try to search with empty indexes');
     }
     if ($escape) {
         $query = $this->sphinx->escapeString($query);
     }
     $qIndex = implode(' ', $indexes);
     $results = $this->sphinx->query($query, $qIndex);
     if (!is_array($results)) {
         throw new \RuntimeException(sprintf('Searching for "%s" failed. Result is not an array. Error "%s"', $query, $this->sphinx->getLastError()));
     }
     if (!isset($results['status'])) {
         throw new \RuntimeException(sprintf('Searching for "%s" failed. Result with no status. Error "%s"', $query, $this->sphinx->getLastError()));
     }
     if ($results['status'] !== SphinxClient::SEARCHD_OK && $results['status'] !== SphinxClient::SEARCHD_WARNING) {
         throw new \RuntimeException(sprintf('Searching for "%s" failed. Result has bad status. Error "%s"', $query, $this->sphinx->getLastError()));
     }
     return $results;
 }
All Usage Examples Of Sphinx\SphinxClient::query