/** The constructor * */ public function __construct() { $this->_cache = Zend_Registry::get('cache'); $this->_config = Zend_Registry::get('config'); $this->_solrConfig = array('adapteroptions' => $this->_config->solr->toArray()); $this->_solr = new Solarium_Client($this->_solrConfig); $this->_solr->setAdapter('Solarium_Client_Adapter_ZendHttp'); $loadbalancer = $this->_solr->getPlugin('loadbalancer'); $master = $this->_config->solr->master->toArray(); $slave = $this->_config->solr->slave->toArray(); $loadbalancer->addServer('master', $master, 100); $loadbalancer->addServer('slave', $slave, 200); $loadbalancer->setFailoverEnabled(true); }
public function testRemoveAndGetPlugins() { $options = array('option1' => 1); $this->_client->registerPlugin('testplugin', 'MyClientPlugin', $options); $plugin = $this->_client->getPlugin('testplugin'); $plugins = $this->_client->getPlugins(); $this->assertEquals(array('testplugin' => $plugin), $plugins); $this->_client->removePlugin('testplugin'); $plugins = $this->_client->getPlugins(); $this->assertEquals(array(), $plugins); }
/** Get the solr object for querying cores * @access public * @return \Solarium_Client */ public function getSolr() { $this->_solr = new Solarium_Client($this->getSolrConfig()); $this->_solr->setAdapter('Solarium_Client_Adapter_ZendHttp'); $this->_solr->getAdapter()->getZendHttp(); $loadbalancer = $this->_solr->getPlugin('loadbalancer'); $master = $this->getConfig()->solr->master->toArray(); $asgard = $this->getConfig()->solr->asgard->toArray(); $valhalla = $this->getConfig()->solr->valhalla->toArray(); $loadbalancer->addServer('objects', $master, 100); $loadbalancer->addServer('asgard', $asgard, 200); $loadbalancer->addServer('valhalla', $valhalla, 150); $loadbalancer->setFailoverEnabled(true); $this->_solr->getAdapter()->getZendHttp(); $this->_loadbalancer = $loadbalancer; return $this->_solr; }
class simpleDebug extends Solarium_Plugin_Abstract { protected $_output = array(); public function display() { echo implode('<br/>', $this->_output); } public function eventBufferedAddFlushStart($buffer) { $this->_output[] = 'Flushing buffer (' . count($buffer) . 'docs)'; } } htmlHeader(); // create a client instance and autoload the buffered add plugin $client = new Solarium_Client($config); $buffer = $client->getPlugin('bufferedadd'); $buffer->setBufferSize(10); // this is quite low, in most cases you can use a much higher value // also register a plugin for outputting events $debug = new simpleDebug(); $client->registerPlugin('debugger', $debug); // let's insert 25 docs for ($i = 1; $i <= 25; $i++) { // create a new document with dummy data and add it to the buffer $data = array('id' => 'test_' . $i, 'name' => 'test for buffered add', 'price' => $i); $buffer->createDocument($data); // alternatively you could create document instances yourself and use the addDocument(s) method } // At this point two flushes will already have been done by the buffer automatically (at the 10th and 20th doc), now // manually flush the remainder. Alternatively you can use the commit method if you want to include a commit command. $buffer->flush();
<?php require 'init.php'; htmlHeader(); // create a client instance $client = new Solarium_Client($config); // get a select query instance $query = $client->createSelect(); $query->setFields(array('id')); // get a plugin instance and apply settings $prefetch = $client->getPlugin('prefetchiterator'); $prefetch->setPrefetch(2); //fetch 2 rows per query (for real world use this can be way higher) $prefetch->setQuery($query); // display the total number of documents found by solr echo 'NumFound: ' . count($prefetch); // show document IDs using the resultset iterator foreach ($prefetch as $document) { echo '<hr/>ID: ' . $document->id; } htmlFooter();
<?php require 'init.php'; htmlHeader(); // create a client instance and get loadbalancer plugin instance $client = new Solarium_Client($config); $loadbalancer = $client->getPlugin('loadbalancer'); // apply loadbalancer settings $optionsSolrOne = array('host' => '127.0.0.1', 'port' => 8983); $optionsSolrTwo = array('host' => '127.0.0.1', 'port' => 7574); $loadbalancer->addServer('solr1', $optionsSolrOne, 100); $loadbalancer->addServer('solr2', $optionsSolrTwo, 200); $loadbalancer->addServer('solr3', $optionsSolrTwo, 1); // create a basic query to execute $query = $client->createSelect(); // execute the query multiple times, displaying the server for each execution for ($i = 1; $i <= 8; $i++) { $resultset = $client->select($query); echo 'Query execution #' . $i . '<br/>'; echo 'NumFound: ' . $resultset->getNumFound() . '<br/>'; echo 'Server: ' . $loadbalancer->getLastServerKey() . '<hr/>'; } // force a server for a query (normally solr 3 is extremely unlikely based on it's weight) $loadbalancer->setForcedServerForNextQuery('solr3'); $resultset = $client->select($query); echo 'Query execution with server forced to solr3<br/>'; echo 'NumFound: ' . $resultset->getNumFound() . '<br/>'; echo 'Server: ' . $loadbalancer->getLastServerKey() . '<hr/>'; // test a ping query $query = $client->createPing(); $client->ping($query);
/** List of the papers available */ public function indexAction() { $params = array_slice($this->_getAllParams(), 3); if (sizeof($params) > 0) { $limit = 20; $page = $this->_getParam('page'); if (!isset($page)) { $start = 0; } else { unset($params['page']); $start = ($page - 1) * 20; } $q = ''; if (array_key_exists('q', $params)) { $q .= $params['q'] . ' '; unset($params['q']); } if (array_key_exists('images', $params)) { $images = (int) 1; unset($params['images']); } if (array_key_exists('facet', $params)) { $facetQuery = $params['facet']; unset($params['facet']); $this->view->facet = 'facet/' . $facetQuery; } $params = array_filter($params); foreach ($params as $k => $v) { $q .= $k . ':"' . $v . '" '; } $config = array('adapteroptions' => array('host' => '127.0.0.1', 'port' => 8983, 'path' => '/solr/', 'core' => 'beocontent')); $select = array('query' => $q, 'start' => $start, 'rows' => $limit, 'fields' => array('*'), 'filterquery' => array()); $client = new Solarium_Client($config); $customizer = $client->getPlugin('customizerequest'); // $customizer->createCustomization('transform') // ->setType('param') // ->setName('tr') // ->setValue('example.xsl'); // $customizer->createCustomization('format') // ->setType('param') // ->setName('wt') // ->setValue('csv'); $query = $client->createSelect($select); $query->addSort('score', Solarium_Query_Select::SORT_ASC); if (isset($facetQuery)) { $query->createFilterQuery('sectionType')->setQuery('section:' . $facetQuery); } $facetSet = $query->getFacetSet(); $facetSet->createFacetField('section')->setField('section'); $resultset = $client->select($query); // echo $resultset->getData(); $this->view->sectionFacet = $resultset->getFacetSet()->getFacet('section'); $pagination = array('page' => $page, 'per_page' => $limit, 'total_results' => $resultset->getNumFound()); $data = array(); foreach ($resultset as $doc) { foreach ($doc as $key => $value) { $fields = array(); $fields[$key] = $value; } $data[] = $fields; } $paginator = Zend_Paginator::factory($resultset->getNumFound()); $paginator->setCurrentPageNumber($page)->setItemCountPerPage($limit)->setPageRange(20); $this->view->paginator = $paginator; $this->view->results = $data; $this->view->query = $q; } else { throw new Pas_Exception_Param('Your search has no parameters!', 500); } }
<?php require 'init.php'; htmlHeader(); // create a client instance and autoload the postbigrequest plugin $client = new Solarium_Client($config); $client->getPlugin('postbigrequest'); // create a basic query to execute $query = $client->createSelect(); // add a huge filterquery to create a very long query string // note: normally you would use a range for this, it's just an easy way to create a very big querystring as a test $fq = ''; for ($i = 1; $i <= 1000; $i++) { $fq .= ' OR price:' . $i; } $fq = substr($fq, 4); $query->createFilterQuery('fq')->setQuery($fq); // without the plugin this query would fail as it is bigger than the default servlet container header buffer $resultset = $client->select($query); // display the total number of documents found by solr echo 'NumFound: ' . $resultset->getNumFound(); // show documents using the resultset iterator foreach ($resultset as $document) { echo '<hr/><table>'; // the documents are also iterable, to get all fields foreach ($document as $field => $value) { // this converts multivalue fields to a comma-separated string if (is_array($value)) { $value = implode(', ', $value); } echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
<?php require 'init.php'; htmlHeader(); // create a client instance and autoload the customize request plugin $client = new Solarium_Client($config); $parallel = $client->getPlugin('parallelexecution'); // Add a delay param to better show the effect, as an example Solr install with // only a dozen documents is too fast for good testing // This param only works with the correct Solr plugin, // see http://www.raspberry.nl/2012/01/04/solr-delay-component/ // If you don't have to plugin the example still works, just without the delay. $customizer = $client->getPlugin('customizerequest'); $customizer->createCustomization(array('key' => 'delay', 'type' => 'param', 'name' => 'delay', 'value' => '500', 'persistent' => true)); // create two queries to execute in an array. Keys are important for fetching the results later! $queryInstock = $client->createSelect()->setQuery('inStock:true'); $queryLowprice = $client->createSelect()->setQuery('price:[1 TO 300]'); // first execute the queries the normal way and time it $start = microtime(true); $client->execute($queryInstock); $client->execute($queryLowprice); echo 'Execution time for normal "serial" execution of two queries: ' . round(microtime(true) - $start, 3); echo '<hr/>'; // now execute the two queries parallel and time it $start = microtime(true); $parallel->addQuery('instock', $queryInstock); $parallel->addQuery('lowprice', $queryLowprice); $results = $parallel->execute(); echo 'Execution time for parallel execution of two queries: ' . round(microtime(true) - $start, 3); htmlFooter(); // Note: for this example on a default Solr index (with a tiny index) running on localhost the performance gain is
<?php require 'init.php'; htmlHeader(); // create a client instance and autoload the customize request plugin $client = new Solarium_Client($config); $customizer = $client->getPlugin('customizerequest'); // add a persistent HTTP header (using array input values) $customizer->createCustomization(array('key' => 'auth', 'type' => 'header', 'name' => 'X-my-auth', 'value' => 'mypassword', 'persistent' => true)); // add a persistent GET param (using fluent interface) $customizer->createCustomization('session')->setType('param')->setName('ssid')->setValue('md7Nhd86adye6sad46d')->setPersistent(true); // add a GET param thats only used for a single request (the default setting is no persistence) $customizer->createCustomization('id')->setType('param')->setName('id')->setValue(4576); // create a basic query to execute $query = $client->createSelect(); // execute query (you should be able to see the extra params in the solr log file) $resultset = $client->select($query); // display the total number of documents found by solr echo 'NumFound: ' . $resultset->getNumFound() . '<br/>'; // execute the same query again (this time the 'id' param should no longer show up in the logs) $resultset = $client->select($query); // display the total number of documents found by solr echo 'NumFound: ' . $resultset->getNumFound(); htmlFooter();