[7588] | 1 | <?php |
---|
| 2 | |
---|
| 3 | require('init.php'); |
---|
| 4 | htmlHeader(); |
---|
| 5 | |
---|
| 6 | // In most cases using the API or config is advisable, however in some cases it can make sense to extend classes. |
---|
| 7 | // This makes it possible to create 'query inheritance' like in this example |
---|
| 8 | class ProductQuery extends Solarium_Query_Select{ |
---|
| 9 | |
---|
| 10 | protected function _init() |
---|
| 11 | { |
---|
| 12 | parent::_init(); |
---|
| 13 | |
---|
| 14 | // basic params |
---|
| 15 | $this->setQuery('*:*'); |
---|
| 16 | $this->setStart(2)->setRows(20); |
---|
| 17 | $this->setFields(array('id','name','price')); |
---|
| 18 | $this->addSort('price', Solarium_Query_Select::SORT_ASC); |
---|
| 19 | |
---|
| 20 | // create a facet field instance and set options |
---|
| 21 | $facetSet = $this->getFacetSet(); |
---|
| 22 | $facetSet->createFacetField('stock')->setField('inStock'); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | // This query inherits all of the query params of it's parent (using parent::_init) and adds some more |
---|
| 28 | // Ofcourse it could also alter or remove settings |
---|
| 29 | class ProductPriceLimitedQuery extends ProductQuery{ |
---|
| 30 | |
---|
| 31 | protected function _init() |
---|
| 32 | { |
---|
| 33 | parent::_init(); |
---|
| 34 | |
---|
| 35 | // create a filterquery |
---|
| 36 | $this->createFilterQuery('maxprice')->setQuery('price:[1 TO 300]'); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | // create a client instance |
---|
| 42 | $client = new Solarium_Client($config); |
---|
| 43 | |
---|
| 44 | // create a query instance |
---|
| 45 | $query = new ProductPriceLimitedQuery; |
---|
| 46 | |
---|
| 47 | // this executes the query and returns the result |
---|
| 48 | $resultset = $client->select($query); |
---|
| 49 | |
---|
| 50 | // display the total number of documents found by solr |
---|
| 51 | echo 'NumFound: '.$resultset->getNumFound(); |
---|
| 52 | |
---|
| 53 | // display facet counts |
---|
| 54 | echo '<hr/>Facet counts for field "inStock":<br/>'; |
---|
| 55 | $facet = $resultset->getFacetSet()->getFacet('stock'); |
---|
| 56 | foreach($facet as $value => $count) { |
---|
| 57 | echo $value . ' [' . $count . ']<br/>'; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // show documents using the resultset iterator |
---|
| 61 | foreach ($resultset as $document) { |
---|
| 62 | |
---|
| 63 | echo '<hr/><table>'; |
---|
| 64 | echo '<tr><th>id</th><td>' . $document->id . '</td></tr>'; |
---|
| 65 | echo '<tr><th>name</th><td>' . $document->name . '</td></tr>'; |
---|
| 66 | echo '<tr><th>price</th><td>' . $document->price . '</td></tr>'; |
---|
| 67 | echo '</table>'; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | htmlFooter(); |
---|