/**
  * Reset settings sphinx
  */
 public function resetClient()
 {
     $this->_client->resetFilters();
     $this->_client->resetGroupBy();
     $this->_client->setArrayResult(false);
     //DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API
     //$this->_client->setMatchMode(SPH_MATCH_EXTENDED2);
     $this->_client->setLimits(0, 20, 1000, 0);
     $this->_client->setFieldWeights(array());
     $this->_client->setSortMode(SPH_SORT_RELEVANCE, '');
     $this->_client->_error = '';
     $this->_client->_warning = '';
 }
 /**
  * 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.
  * @param array $options The options for the query.
  * @param bool $escapeQuery Should the query string be escaped?
  *
  * @return array The results of the search.
  */
 public function search($query, array $indexes, array $options = array(), $escapeQuery = true)
 {
     if ($escapeQuery) {
         $query = $this->sphinx->escapeString($query);
     }
     /**
      * Build the list of indexes to be queried.
      */
     $indexNames = '';
     foreach ($indexes as &$label) {
         if (isset($this->indexes[$label])) {
             $indexNames .= $this->indexes[$label] . ' ';
         }
     }
     /**
      * If no valid indexes were specified, return an empty result set.
      *
      * FIXME: This should probably throw an exception.
      */
     if (empty($indexNames)) {
         return array();
     }
     /**
      * Set the offset and limit for the returned results.
      */
     if (isset($options['result_offset']) && isset($options['result_limit'])) {
         $this->sphinx->setLimits($options['result_offset'], $options['result_limit']);
     }
     /**
      * Weight the individual fields.
      */
     if (isset($options['field_weights'])) {
         $this->sphinx->setFieldWeights($options['field_weights']);
     }
     /**
      * Perform the query.
      */
     $start = round(microtime(true) * 1000);
     $results = $this->sphinx->query($query, $indexNames);
     if ($results['status'] === SEARCHD_ERROR) {
         throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError()));
     }
     if ($results['status'] === SEARCHD_RETRY) {
         throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with retry "%s".', $label, $query, $this->sphinx->getLastError()));
     }
     $end = round(microtime(true) * 1000) - $start;
     $event = new SearchEvent($query, $indexes, $end);
     $this->dispatcher->dispatch("sphinx.event.search", $event);
     return $results;
 }
 /**
  * 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 array The results of the search.
  *
  * $indexes should look like:
  *
  * $indexes = array(
  *   'IndexLabel' => array(
  *     'result_offset' => (int), // optional unless result_limit is set
  *     'result_limit'  => (int), // optional unless result_offset is set
  *     'field_weights' => array( // optional
  *       'FieldName'   => (int),
  *       ...,
  *     ),
  *   ),
  *   ...,
  * );
  */
 public function search($query, array $indexes, $escapeQuery = true)
 {
     if ($escapeQuery) {
         $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'])) {
             $this->sphinx->setLimits($options['result_offset'], $options['result_limit']);
         }
         /**
          * Weight the individual fields.
          */
         if (isset($options['field_weights'])) {
             $this->sphinx->setFieldWeights($options['field_weights']);
         }
         /**
          * Perform the query.
          */
         $results[$label] = $this->sphinx->query($query, implode(' ', $this->indexes[$label]["index"]));
         if ($results[$label]['status'] !== SEARCHD_OK) {
             throw new \RuntimeException(sprintf('Searching index "%s" for "%s" failed with error "%s".', $label, $query, $this->sphinx->getLastError()));
         }
     }
     /**
      * If only one index was searched, return that index's results directly.
      */
     if (count($indexes) === 1 && count($results) === 1) {
         $results = reset($results);
     }
     /**
      * FIXME: Throw an exception if $results is empty?
      */
     return $results;
 }
Beispiel #4
0
 public function getSphinxAdapter()
 {
     require_once Mage::getBaseDir('lib') . DIRECTORY_SEPARATOR . 'sphinxapi.php';
     // Connect to our Sphinx Search Engine and run our queries
     $sphinx = new SphinxClient();
     $host = Mage::getStoreConfig('sphinxsearch/server/host');
     $port = Mage::getStoreConfig('sphinxsearch/server/port');
     if (empty($host)) {
         return $sphinx;
     }
     if (empty($port)) {
         $port = 9312;
     }
     $sphinx->SetServer($host, $port);
     $sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
     $sphinx->setFieldWeights(array('name' => 7, 'category' => 1, 'name_attributes' => 1, 'data_index' => 3));
     $sphinx->setLimits(0, 200, 1000, 5000);
     // SPH_RANK_PROXIMITY_BM25 ist default
     $sphinx->SetRankingMode(SPH_RANK_SPH04, "");
     // 2nd parameter is rank expr?
     return $sphinx;
 }
 /**
  * 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);
 }
Beispiel #6
0
 public function fetch()
 {
     if (!class_exists('SphinxClient')) {
         return false;
     }
     $s = new SphinxClient();
     $s->setServer($this->_sphinxHost, $this->_sphinxPort);
     if (count($this->_arrSearchOutRangeColumnMinMax) > 0) {
         foreach ($this->_arrSearchOutRangeColumnMinMax as $value) {
             $d = explode(',', $value);
             $s->setFilterRange($d[0], $d[1], $d[2], true);
         }
     }
     if (count($this->_arrSearchInRangeColumnMinMax) > 0) {
         foreach ($this->_arrSearchInRangeColumnMinMax as $value) {
             $d = explode(',', $value);
             $s->setFilterRange($d[0], $d[1], $d[2], false);
         }
     }
     $s->setConnectTimeout($this->_connectTimeout);
     $s->setMaxQueryTime($this->{$_maxquerytime});
     //            $s->setRetries ( int $this->retriesCount , int $this->retriesDelay  );
     //
     $s->setMatchMode($this->searchMode);
     $s->setFieldWeights($this->_fieldweights);
     //            $s->setFilter ( string $attribute , array $values [, bool $exclude = false ] );
     //            $s->setFilterFloatRange ( string $attribute , float $min , float $max [, bool $exclude = false ] );
     //            $s->setFilterRange ( string $attribute , int $min , int $max [, bool $exclude = false ] );
     //            $s->setGeoAnchor ( string $attrlat , string $attrlong , float $latitude , float $longitude );
     //            $s->setGroupBy ( string $attribute , int $func [, string $groupsort = "@group desc" ] );
     //            $s->setGroupDistinct ( string $attribute );
     //            $s->setIDRange ( int $min , int $max );
     $s->setIndexWeights($this->_arrIndexweights);
     //            $s->setLimits ( int $offset , int $limit [, int $max_matches = 0 [, int $cutoff = 0 ]] );
     $s->setMatchMode($this->searchMode);
     //            $s->setOverride ( string $attribute , int $type , array $values );
     $s->setRankingMode($this->rankMode);
     //            $s->setSelect ( string $clause );
     //            $s->setSortMode ( int $mode [, string $sortby ] );
     return $s->query($this->_query);
 }
Beispiel #7
0
 /**
  * @brief reset search criteria to default
  * @details reset conditions and set default search options
  */
 public function resetCriteria()
 {
     if (is_object($this->criteria)) {
         $this->lastCriteria = clone $this->criteria;
     } else {
         $this->lastCriteria = new stdClass();
     }
     $this->criteria = new stdClass();
     $this->criteria->query = '';
     $this->client->resetFilters();
     $this->client->resetGroupBy();
     $this->client->setArrayResult(false);
     $this->client->setMatchMode($this->matchMode);
     //        $this->client->setRankingMode($this->rankMode);
     $this->client->setSortMode(SPH_SORT_RELEVANCE, '@relevance DESC');
     $this->client->setLimits(0, 1000000, 10000);
     if (!empty($this->fieldWeights)) {
         $this->client->setFieldWeights($this->fieldWeights);
     }
 }
 public static function queryDocument($query, $params = array())
 {
     if (trim($query) == "") {
         return array();
     }
     $sphinx_config = SphinxSearch_Config::getInstance();
     $documents_config = $sphinx_config->getDocumentsAsArray();
     $SphinxClient = new SphinxClient();
     $entries = array();
     $language = "all";
     if (SphinxSearch_Config_Plugin::getValue("documents", "use_i18n") == "true") {
         if (array_key_exists("language", $params)) {
             $language = $params["language"];
         } else {
             $locale = Zend_Registry::get("Zend_Locale");
             $language = $locale->getLanguage();
         }
     }
     $field_weights = array();
     $indexes = array();
     foreach ($documents_config as $document_name => $document_properties) {
         $indexes[] = "idx_document_" . $document_name . "_" . $language;
         foreach ($document_properties["elements"] as $field_name => $field_config) {
             if (array_key_exists("weight", $field_config) && intval($field_config["weight"]) > 0) {
                 $field_weights[$field_name] = intval($field_config["weight"]);
             }
         }
     }
     if (sizeof($field_weights) > 0) {
         $SphinxClient->setFieldWeights($field_weights);
     }
     $search_result = $SphinxClient->Query($query, implode(", ", $indexes));
     if ($search_result === false) {
         throw new Exception($SphinxClient->GetLastError());
     }
     if ($search_result["total_found"] > 0) {
         foreach ($search_result["matches"] as $id => $meta) {
             $entries[] = array("result" => Document::getById($id), "id" => $id, "meta" => $meta, "type" => "document");
         }
     }
     return $entries;
 }