/** * 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(); }