1 | <?php |
---|
2 | require('init.php'); |
---|
3 | |
---|
4 | // this very simple plugin is used to show some events |
---|
5 | class simpleDebug extends Solarium_Plugin_Abstract |
---|
6 | { |
---|
7 | protected $_output = array(); |
---|
8 | |
---|
9 | public function display() |
---|
10 | { |
---|
11 | echo implode('<br/>', $this->_output); |
---|
12 | } |
---|
13 | |
---|
14 | public function eventBufferedAddFlushStart($buffer) { |
---|
15 | $this->_output[] = 'Flushing buffer (' . count($buffer) . 'docs)'; |
---|
16 | } |
---|
17 | } |
---|
18 | |
---|
19 | htmlHeader(); |
---|
20 | |
---|
21 | // create a client instance and autoload the buffered add plugin |
---|
22 | $client = new Solarium_Client($config); |
---|
23 | $buffer = $client->getPlugin('bufferedadd'); |
---|
24 | $buffer->setBufferSize(10); // this is quite low, in most cases you can use a much higher value |
---|
25 | |
---|
26 | // also register a plugin for outputting events |
---|
27 | $debug = new simpleDebug(); |
---|
28 | $client->registerPlugin('debugger', $debug); |
---|
29 | |
---|
30 | // let's insert 25 docs |
---|
31 | for ($i=1; $i<=25; $i++) { |
---|
32 | |
---|
33 | // create a new document with dummy data and add it to the buffer |
---|
34 | $data = array( |
---|
35 | 'id' => 'test_'.$i, |
---|
36 | 'name' => 'test for buffered add', |
---|
37 | 'price' => $i, |
---|
38 | ); |
---|
39 | $buffer->createDocument($data); |
---|
40 | |
---|
41 | // alternatively you could create document instances yourself and use the addDocument(s) method |
---|
42 | } |
---|
43 | |
---|
44 | // At this point two flushes will already have been done by the buffer automatically (at the 10th and 20th doc), now |
---|
45 | // manually flush the remainder. Alternatively you can use the commit method if you want to include a commit command. |
---|
46 | $buffer->flush(); |
---|
47 | |
---|
48 | // In total 3 flushes (requests) have been sent to Solr. This should be visible in this output: |
---|
49 | $debug->display(); |
---|
50 | |
---|
51 | htmlFooter(); |
---|