/**
  * @param string $prefix The Solr configuration keys' prefix string
  *
  * @throws \RuntimeException If the Solr extension is not available or is too old
  */
 public function __construct($prefix = 'solr')
 {
     if (!is_string($prefix) || !trim($prefix)) {
         throw new \InvalidArgumentException('The prefix must be a non-empty string');
     }
     if (!function_exists('solr_get_version')) {
         throw new \RuntimeException('Looks like the Solr extension is not available');
     }
     if (version_compare(self::MINIMUM_EXT_VERSION, solr_get_version()) > 0) {
         throw new \RuntimeException('This version (' . solr_get_version() . ') of the Solr extension is too old');
     }
     $this->prefix = $prefix;
 }
Ejemplo n.º 2
0
 /**
  * Simple Search suggestions interface
  *
  * @deprecated after 1.9.0.0 - integrated into $this->_search()
  *
  * @param string $query The raw query string
  * @param array $params
  * @param int|bool $limit
  * @param bool $withResultsCounts
  * @return array
  */
 public function _searchSuggestions($query, $params = array(), $limit = false, $withResultsCounts = false)
 {
     /**
      * @see self::_search()
      */
     if ((int) ('1' . str_replace('.', '', solr_get_version())) < 1099) {
         $this->_connect();
     }
     $query = $this->_escapePhrase($query);
     if (!$query) {
         return false;
     }
     $_params = array();
     $languageSuffix = $this->_getLanguageSuffix($params['locale_code']);
     $solrQuery = new SolrQuery($query);
     $_params['solr_params'] = array('spellcheck' => 'true', 'spellcheck.collate' => 'true', 'spellcheck.dictionary' => 'magento_spell' . $languageSuffix, 'spellcheck.extendedResults' => 'true', 'spellcheck.count' => $limit ? $limit : 1);
     /**
      * Specific Solr params
      */
     if (!empty($_params['solr_params'])) {
         foreach ($_params['solr_params'] as $name => $value) {
             $solrQuery->setParam($name, $value);
         }
     }
     $this->_client->setServlet(SolrClient::SEARCH_SERVLET_TYPE, 'spell');
     /**
      * Store filtering
      */
     if (!empty($params['store_id'])) {
         $solrQuery->addFilterQuery('store_id:' . $params['store_id']);
     }
     if (!Mage::helper('cataloginventory')->isShowOutOfStock()) {
         $solrQuery->addFilterQuery('in_stock:true');
     }
     try {
         $this->ping();
         $response = $this->_client->query($solrQuery);
         $result = $this->_prepareSuggestionsQueryResponse($response->getResponse());
         $resultLimit = array();
         // Calc results count for each suggestion
         if ($withResultsCounts && $limit) {
             $tmp = $this->_lastNumFound;
             //Temporary store value for main search query
             $this->_lastNumFound = 0;
             foreach ($result as $key => $item) {
                 $this->search($item['word'], $params);
                 if ($this->_lastNumFound) {
                     $result[$key]['num_results'] = $this->_lastNumFound;
                     $resultLimit[] = $result[$key];
                     $limit--;
                 }
                 if ($limit <= 0) {
                     break;
                 }
             }
             $this->_lastNumFound = $tmp;
             //Revert store value for main search query
         } else {
             $resultLimit = array_slice($result, 0, $limit);
         }
         return $resultLimit;
     } catch (Exception $e) {
         Mage::logException($e);
         return array();
     }
 }
Ejemplo n.º 3
0
 /**
  * Adds a document to the solr index
  * @param ASolrDocument|SolrInputDocument $document the document to add to the index
  * @param integer $commitWithin the number of milliseconds to commit within after indexing the document
  * @return boolean true if the document was indexed successfully
  */
 public function index($document, $commitWithin = null)
 {
     // When we add documents we want to overwrite existing documents and avoid duplicates (several documents with the same ID).
     $overwrite = true;
     if (version_compare(solr_get_version(), '2.0.0', '<')) {
         // PECL Solr < 2.0 $allowDups was used instead of $overwrite, which does the same functionality with exact opposite bool flag.
         // See http://www.php.net/manual/en/solrclient.adddocument.php
         $overwrite = false;
         // Equivalent of $allowDups = false;
     }
     if ($document instanceof IASolrDocument) {
         if ($commitWithin === null && $document->getCommitWithin() > 0) {
             $commitWithin = $document->getCommitWithin();
         }
         $document = $document->getInputDocument();
     } elseif (is_array($document) || $document instanceof Traversable) {
         if ($commitWithin === null) {
             $commitWithin = 0;
         }
         $document = (array) $document;
         foreach ($document as $key => $value) {
             if ($value instanceof IASolrDocument) {
                 $document[$key] = $value->getInputDocument();
             }
         }
         Yii::trace('Adding ' . count($document) . " documents to the solr index", 'packages.solr.ASolrConnection');
         return $this->getClient()->addDocuments($document, $overwrite, $commitWithin)->success();
     }
     if ($commitWithin === null) {
         $commitWithin = 0;
     }
     Yii::trace('Adding 1 document to the solr index', 'packages.solr.ASolrConnection');
     $response = $this->getClient()->addDocument($document, $overwrite, $commitWithin);
     return $response->success();
 }