<?php

require __DIR__ . '/init.php';
htmlHeader();
// create a client instance and create endpoints
$client = new Solarium\Client($config);
$endpoint1 = $client->createEndpoint('local1');
//normally you would add endpoint specific settings...
$endpoint2 = $client->createEndpoint('local2');
$endpoint3 = $client->createEndpoint('local3');
// get loadbalancer plugin instance and add endpoints
$loadbalancer = $client->getPlugin('loadbalancer');
$loadbalancer->addEndpoint($endpoint1, 100);
$loadbalancer->addEndpoint($endpoint2, 100);
$loadbalancer->addEndpoint($endpoint3, 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->getLastEndpoint() . '<hr/>';
}
// force a server for a query (normally solr 3 is extremely unlikely based on its weight)
$loadbalancer->setForcedEndpointForNextQuery('local3');
$resultset = $client->select($query);
echo 'Query execution with server forced to local3<br/>';
echo 'NumFound: ' . $resultset->getNumFound() . '<br/>';
echo 'Server: ' . $loadbalancer->getLastEndpoint() . '<hr/>';
// test a ping query
<?php

require __DIR__ . '/init.php';
use Solarium\Plugin\BufferedAdd\Event\Events;
use Solarium\Plugin\BufferedAdd\Event\PreFlush as PreFlushEvent;
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 an event hook to display what is happening
$client->getEventDispatcher()->addListener(Events::PRE_FLUSH, function (PreFlushEvent $event) {
    echo 'Flushing buffer (' . count($event->getBuffer()) . 'docs)<br/>';
});
// 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();
// In total 3 flushes (requests) have been sent to Solr. This should be visible in the output of the event hook.
htmlFooter();
<?php

require __DIR__ . '/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 __DIR__ . '/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 __DIR__ . '/init.php';
htmlHeader();
// create a client instance
$client = new Solarium\Client($config);
// enable the filter plugin and get a query instance
$filter = $client->getPlugin('minimumscorefilter');
$query = $client->createQuery($filter::QUERY_TYPE);
$query->setRows(50);
$query->setFields(array('id', 'name', 'score'));
$query->setQuery('memory');
$query->setFilterRatio(0.8);
$query->setFilterMode($query::FILTER_MODE_MARK);
// get grouping component and set a field to group by
$groupComponent = $query->getGrouping();
$groupComponent->addField('inStock');
// maximum number of items per group
$groupComponent->setLimit(10);
// get a group count
$groupComponent->setNumberOfGroups(true);
// this executes the query and returns the result
$resultset = $client->select($query);
$groups = $resultset->getGrouping();
foreach ($groups as $groupKey => $fieldGroup) {
    echo '<h1>' . $groupKey . '</h1>';
    echo 'Matches: ' . $fieldGroup->getMatches() . '<br/>';
    echo 'Number of groups: ' . $fieldGroup->getNumberOfGroups();
    foreach ($fieldGroup as $valueGroup) {
        echo '<h2>' . (int) $valueGroup->getValue() . '</h2>';
        foreach ($valueGroup as $document) {
<?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
<?php

require __DIR__ . '/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();