/** * Indexes the given entity * * @param object $entity */ public function indexEntity($entity) { $document = $this->createSolrDocument($entity); $this->client->addDocument($document); $this->client->commit(); $this->fields = $this->getEntityFields(get_class($entity)); }
/** * @param \SolrInputDocument $doc */ private function addDocumentToIndex($doc) { try { $updateResponse = $this->solrClient->addDocument($doc); $this->solrClient->commit(); } catch (\Exception $e) { $errorEvent = new ErrorEvent(null, null, 'add-document'); $errorEvent->setException($e); $this->eventManager->handle(EventManager::ERROR, $errorEvent); } }
/** * Index the current search result. * * @param array $fields * @return bool */ public function index(array $fields = array(), array $options = array()) { $document = new SolrInputDocument(); $document->addField('id', $fields['id']); $document->addField('private', $fields['private']); $document->addField('username', $fields['username']); $document->addField('createdon', '' . strftime('%Y-%m-%dT%H:%M:%SZ', strtotime($fields['createdon']))); $document->addField('board', $fields['board']); $document->addField('author', $fields['author']); $document->addField('thread', $fields['thread']); $document->addField('title', $fields['title'], 2); $document->addField('message', $fields['message'], 2); $document->addField('url', $fields['url']); if (!empty($fields['replies'])) { $document->addField('replies', $fields['replies']); } if (!empty($fields['board_name'])) { $document->addField('board_name', $fields['board_name']); } if (!empty($fields['category'])) { $document->addField('category', $fields['category']); } if (!empty($fields['category_name'])) { $document->addField('category_name', $fields['category_name']); } if (!empty($fields['answered_question'])) { $document->addField('answered_question', $fields['answered_question']); } $response = false; try { $response = $this->client->addDocument($document); if (isset($options['commit']) && $options['commit'] !== false) { $this->commit(); } } catch (Exception $e) { $response = $e->getMessage(); } return $response; }
<?php function commit2() { $solrAddress = 'http://' . SOLR_SERVER_HOSTNAME . ':' . SOLR_SERVER_PORT . '/solr/collection1'; $response = file_get_contents($solrAddress . '/update?commit=true'); } include "bootstrap.php"; $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT); $client = new SolrClient($options); $doc = new SolrInputDocument(); $doc->addField('id', 334456); $doc->addField('cat', 'Software'); $doc->addField('cat', 'Lucene'); $updateResponse = $client->addDocument($doc); print_r($updateResponse->getResponse()); // Broken! //$client->commit(); // Use our own solution instead! commit2();
<?php include "bootstrap.php"; $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH); $client = new SolrClient($options); $doc = new SolrInputDocument(); $doc->addField('id', 334455); $doc->addField('cat', 'Software'); $doc->addField('cat', 'Lucene'); // No need to call commit() because $commitWithin is passed, so Solr Server will auto commit within 10 seconds $updateResponse = $client->addDocument($doc, false, 10000); print_r($updateResponse->getResponse());
public function addKeywordsintoSolrDocument($id, $name, $alias, $priority, $id_news, $type) { try { if (empty($id) === FALSE && is_numeric($id)) { $this->config->load('solr'); $solr = $this->config->item('solr'); $options = array('hostname' => $solr['SOLR_SERVER_HOSTNAME'], 'login' => $solr['SOLR_SERVER_USERNAME'], 'password' => $solr['SOLR_SERVER_PASSWORD'], 'port' => $solr['SOLR_SERVER_PORT'], 'path' => 'solr/keywords'); $client = new SolrClient($options); $doc = new SolrInputDocument(); $doc->addField('id', $id); $doc->addField('name', $name); $doc->addField('alias', $alias); $doc->addField('type', $type); $doc->addField('priority', $priority); $doc->addField('id_news', $id_news); $updateResponse = $client->addDocument($doc); $client->commit(); return $updateResponse->getResponse(); } else { return FALSE; } } catch (Exception $e) { return FALSE; } }
public function addListKeywordtoSolr() { try { $keywords = $this->m_backend->jqxGets('keywords'); if (empty($keywords) === FALSE) { $this->config->load('solr'); $solr = $this->config->item('solr'); $options = array('hostname' => $solr['SOLR_SERVER_HOSTNAME'], 'login' => $solr['SOLR_SERVER_USERNAME'], 'password' => $solr['SOLR_SERVER_PASSWORD'], 'port' => $solr['SOLR_SERVER_PORT'], 'path' => 'solr/keywords'); $client = new SolrClient($options); foreach ($keywords as $keyword) { $doc = new SolrInputDocument(); $doc->addField('id', $keyword['id']); $doc->addField('name', $keyword['name']); $doc->addField('alias', $keyword['alias']); $doc->addField('id_news', $keyword['id_news']); $doc->addField('type', $keyword['type']); $doc->addField('priority', $keyword['priority']); $updateResponse = $client->addDocument($doc); $client->commit(); } } else { return FALSE; } } catch (Exception $e) { return FALSE; } }
$small->addField('inventory', 100); $medium = new SolrInputDocument(); $medium->addField('id', 'TS-BLK-M'); $medium->addField('content_type', 'sku'); $medium->addField('size', 'M'); $medium->addField('inventory', 200); $large = new SolrInputDocument(); $large->addField('id', 'TS-BLK-L'); $large->addField('content_type', 'sku'); $large->addField('size', 'L'); $large->addField('inventory', 300); // add child documents $product->addChildDocument($small); $product->addChildDocument($medium); $product->addChildDocument($large); // or // $skus = [$small, $medium, $large]; // $product->addChildDocuments($skus); // add product document to the index $updateResponse = $client->addDocument($product, true, 10000); print_r($updateResponse->getResponse()); /* OUTPUT SIMILAR TO: SolrObject Object ( [responseHeader] => SolrObject Object ( [status] => 0 [QTime] => 5 ) ) */