Esempio n. 1
0
 /**
  * Gets the solr connection
  * @return ASolrConnection the connection to use for this test
  */
 protected function getConnection()
 {
     static $connection;
     if ($connection === null) {
         $connection = new ASolrConnection();
         $connection->clientOptions->hostname = SOLR_HOSTNAME;
         $connection->clientOptions->port = SOLR_PORT;
         $connection->clientOptions->path = SOLR_PATH;
         ASolrDocument::$solr = $connection;
     }
     return $connection;
 }
Esempio n. 2
0
 /**
  * Constructor.
  * @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance
  * (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>).
  * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
  */
 public function __construct($modelClass, $config = array())
 {
     if ($modelClass instanceof IASolrDocument || $modelClass instanceof CActiveRecord) {
         $this->modelClass = get_class($modelClass);
         $this->model = $modelClass;
     } else {
         $this->modelClass = $modelClass;
         $this->model = ASolrDocument::model($this->modelClass);
     }
     $this->setId($this->modelClass);
     foreach ($config as $key => $value) {
         $this->{$key} = $value;
     }
 }
Esempio n. 3
0
 /**
  * Returns the real definition of an attribute given its name.
  *
  * The resolution is based on {@link attributes} and {@link CActiveRecord::attributeNames}.
  * <ul>
  * <li>When {@link attributes} is an empty array, if the name refers to an attribute of {@link modelClass},
  * then the name is returned back.</li>
  * <li>When {@link attributes} is not empty, if the name refers to an attribute declared in {@link attributes},
  * then the corresponding virtual attribute definition is returned. Starting from version 1.1.3, if {@link attributes}
  * contains a star ('*') element, the name will also be used to match against all model attributes.</li>
  * <li>In all other cases, false is returned, meaning the name does not refer to a valid attribute.</li>
  * </ul>
  * @param string $attribute the attribute name that the user requests to sort on
  * @return mixed the attribute name or the virtual attribute definition. False if the attribute cannot be sorted.
  */
 public function resolveAttribute($attribute)
 {
     if ($this->attributes !== array()) {
         $attributes = $this->attributes;
     } else {
         if ($this->modelClass !== null) {
             $attributes = ASolrDocument::model($this->modelClass)->attributeNames();
         } else {
             return false;
         }
     }
     foreach ($attributes as $name => $definition) {
         if (is_string($name)) {
             if ($name === $attribute) {
                 return $definition;
             }
         } else {
             if ($definition === '*') {
                 if ($this->modelClass !== null && ASolrDocument::model($this->modelClass)->hasAttribute($attribute)) {
                     return $attribute;
                 }
             } else {
                 if ($definition === $attribute) {
                     return $attribute;
                 }
             }
         }
     }
     return false;
 }
Esempio n. 4
0
 /**
  * Returns the solr connection used by solr document.
  * By default, the "solr" application component is used as the solr connection.
  * You may override this method if you want to use a different solr connection.
  * @return ASolrConnection the solr connection used by solr document.
  */
 public function getSolrConnection()
 {
     if ($this->_connection !== null) {
         return $this->_connection;
     } elseif (self::$solr !== null) {
         return self::$solr;
     } else {
         self::$solr = Yii::app()->solr;
         if (self::$solr instanceof IASolrConnection) {
             return self::$solr;
         } else {
             throw new CException(Yii::t('yii', 'Solr Document requires a "solr" ASolrConnection application component.'));
         }
     }
 }
Esempio n. 5
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();
 }
Esempio n. 6
0
 /**
  * Gets the static model
  * @param string $className the model class to instantiate
  * @return ExampleExtendedSolrDocument the nidek
  */
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }