[7588] | 1 | <?php |
---|
| 2 | |
---|
| 3 | require('init.php'); |
---|
| 4 | htmlHeader(); |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | // In this case an array is used for configuration to keep the example simple. |
---|
| 8 | // For an easier to use config file you are probably better of with another format, like Zend_Config_Ini |
---|
| 9 | // See the documentation for more info about this. |
---|
| 10 | $select = array( |
---|
| 11 | 'query' => '*:*', |
---|
| 12 | 'start' => 2, |
---|
| 13 | 'rows' => 20, |
---|
| 14 | 'fields' => array('id','name','price'), |
---|
| 15 | 'sort' => array('price' => 'asc'), |
---|
| 16 | 'filterquery' => array( |
---|
| 17 | 'maxprice' => array( |
---|
| 18 | 'query' => 'price:[1 TO 300]' |
---|
| 19 | ), |
---|
| 20 | ), |
---|
| 21 | 'component' => array( |
---|
| 22 | 'facetset' => array( |
---|
| 23 | 'facet' => array( |
---|
| 24 | // notice this config uses an inline key value, instead of array key like the filterquery |
---|
| 25 | array('type' => 'field', 'key' => 'stock', 'field' => 'inStock'), |
---|
| 26 | ) |
---|
| 27 | ), |
---|
| 28 | ), |
---|
| 29 | ); |
---|
| 30 | |
---|
| 31 | // create a client instance |
---|
| 32 | $client = new Solarium_Client($config); |
---|
| 33 | |
---|
| 34 | // get a select query instance based on the config |
---|
| 35 | $query = $client->createSelect($select); |
---|
| 36 | |
---|
| 37 | // this executes the query and returns the result |
---|
| 38 | $resultset = $client->select($query); |
---|
| 39 | |
---|
| 40 | // display the total number of documents found by solr |
---|
| 41 | echo 'NumFound: '.$resultset->getNumFound(); |
---|
| 42 | |
---|
| 43 | // display facet counts |
---|
| 44 | echo '<hr/>Facet counts for field "inStock":<br/>'; |
---|
| 45 | $facet = $resultset->getFacetSet()->getFacet('stock'); |
---|
| 46 | foreach($facet as $value => $count) { |
---|
| 47 | echo $value . ' [' . $count . ']<br/>'; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | // show documents using the resultset iterator |
---|
| 51 | foreach ($resultset as $document) { |
---|
| 52 | |
---|
| 53 | echo '<hr/><table>'; |
---|
| 54 | echo '<tr><th>id</th><td>' . $document->id . '</td></tr>'; |
---|
| 55 | echo '<tr><th>name</th><td>' . $document->name . '</td></tr>'; |
---|
| 56 | echo '<tr><th>price</th><td>' . $document->price . '</td></tr>'; |
---|
| 57 | echo '</table>'; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | htmlFooter(); |
---|