Esempio n. 1
0
 public function testCreateQueryPostPlugin()
 {
     $type = Solarium_Client::QUERYTYPE_SELECT;
     $options = array('optionA' => 1, 'optionB' => 2);
     $query = $this->_client->createQuery($type, $options);
     $observer = $this->getMock('Solarium_Plugin_Abstract', array(), array($this->_client, array()));
     $observer->expects($this->once())->method('postCreateQuery')->with($this->equalTo($type), $this->equalTo($options), $this->equalTo($query));
     $this->_client->registerPlugin('testplugin', $observer);
     $this->_client->createQuery($type, $options);
 }
    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();
// In total 3 flushes (requests) have been sent to Solr. This should be visible in this output:
$debug->display();
htmlFooter();
// This is a custom query class that could have some customized logic
class MyQuery extends Solarium_Query_Select
{
}
// this very simple plugin that modifies the default querytype mapping
class queryCustomizer extends Solarium_Plugin_Abstract
{
    protected function _initPlugin()
    {
        $this->_client->registerQueryType(Solarium_Client::QUERYTYPE_SELECT, 'MyQuery', 'Solarium_Client_RequestBuilder_Select', 'Solarium_Client_ResponseParser_Select');
    }
}
htmlHeader();
// create a client instance and register the plugin
$client = new Solarium_Client($config);
$client->registerPlugin('querycustomizer', 'queryCustomizer');
// create a select query instance
$query = $client->createSelect();
// check the query class, it should be our custom query class
echo 'Query class: ' . get_class($query) . '<br/>';
// execute the query and display the results
$resultset = $client->select($query);
echo 'NumFound: ' . $resultset->getNumFound();
foreach ($resultset as $document) {
    echo '<hr/><table>';
    foreach ($document as $field => $value) {
        if (is_array($value)) {
            $value = implode(', ', $value);
        }
        echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
    }
        $this->_timer('postExecute');
    }
    public function preCreateQuery()
    {
        $this->_timer('preCreateResult');
    }
    public function postCreateQuery()
    {
        $this->_timer('postCreateResult');
    }
}
htmlHeader();
// create a client instance and register the plugin
$plugin = new basicDebug();
$client = new Solarium_Client($config);
$client->registerPlugin('debugger', $plugin);
// execute a select query and display the results
$query = $client->createSelect();
$resultset = $client->select($query);
echo 'NumFound: ' . $resultset->getNumFound();
foreach ($resultset as $document) {
    echo '<hr/><table>';
    foreach ($document as $field => $value) {
        if (is_array($value)) {
            $value = implode(', ', $value);
        }
        echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
    }
    echo '</table>';
}
// display the debug plugin output