<?php require __DIR__ . '/init.php'; htmlHeader(); // create a client instance $client = new Solarium\Client($config); // get a select query instance $query = $client->createSelect(); // add distributed search settings // see http://wiki.apache.org/solr/DistributedSearch#Distributed_Search_Example for setting up two solr instances $distributedSearch = $query->getDistributedSearch(); $distributedSearch->addShard('shard1', 'localhost:8983/solr'); $distributedSearch->addShard('shard2', 'localhost:7574/solr'); // this executes the query and returns the result $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>'; } echo '</table>'; } htmlFooter();
<?php require __DIR__ . '/init.php'; htmlHeader(); // In this case an array is used for configuration to keep the example simple. // For an easier to use config file you are probably better of with another format, like Zend_Config_Ini // See the documentation for more info about this. $select = array('query' => '*:*', 'start' => 2, 'rows' => 20, 'fields' => array('id', 'name', 'price'), 'sort' => array('price' => 'asc'), 'filterquery' => array('maxprice' => array('query' => 'price:[1 TO 300]')), 'component' => array('facetset' => array('facet' => array(array('type' => 'field', 'key' => 'stock', 'field' => 'inStock'))))); // create a client instance $client = new Solarium\Client($config); // get a select query instance based on the config $query = $client->createSelect($select); // this executes the query and returns the result $resultset = $client->select($query); // display the total number of documents found by solr echo 'NumFound: ' . $resultset->getNumFound(); // display facet counts echo '<hr/>Facet counts for field "inStock":<br/>'; $facet = $resultset->getFacetSet()->getFacet('stock'); foreach ($facet as $value => $count) { echo $value . ' [' . $count . ']<br/>'; } // show documents using the resultset iterator foreach ($resultset as $document) { echo '<hr/><table>'; echo '<tr><th>id</th><td>' . $document->id . '</td></tr>'; echo '<tr><th>name</th><td>' . $document->name . '</td></tr>'; echo '<tr><th>price</th><td>' . $document->price . '</td></tr>'; echo '</table>'; } htmlFooter();
<?php require __DIR__ . '/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
/** * @param Solarium\Client $solr * @param BenutzerIn $benutzerIn * @return \Solarium\QueryType\Select\Query\Query */ protected function getAlleSuchergebnisse(&$solr, $benutzerIn) { $select = $solr->createSelect(); $select->addSort('sort_datum', $select::SORT_DESC); $select->setRows(100); /** @var Solarium\QueryType\Select\Query\Component\DisMax $dismax */ $dismax = $select->getDisMax(); $dismax->setQueryParser('edismax'); $dismax->setQueryFields("text text_ocr"); $benachrichtigungen = $benutzerIn->getBenachrichtigungen(); $krits_solr = []; foreach ($benachrichtigungen as $ben) { $krits_solr[] = "(" . $ben->getSolrQueryStr($select) . ")"; } $querystr = implode(" OR ", $krits_solr); $select->setQuery($querystr); /** @var Solarium\QueryType\Select\Query\Component\Highlighting\Highlighting $hl */ $hl = $select->getHighlighting(); $hl->setFields('text, text_ocr, antrag_betreff'); $hl->setSimplePrefix('<b>'); $hl->setSimplePostfix('</b>'); return $select; }