1 | <?php |
---|
2 | |
---|
3 | require('init.php'); |
---|
4 | htmlHeader(); |
---|
5 | |
---|
6 | // This is a custom query class that could have some customized logic |
---|
7 | class MyQuery extends Solarium_Query_Select |
---|
8 | { |
---|
9 | // ...customization here... |
---|
10 | } |
---|
11 | |
---|
12 | // And this is the extended client, that modifies the default query mapping |
---|
13 | // for select queries to our custom query class. |
---|
14 | // BTW, the same could also be done using a plugin, see example 5.3.2 |
---|
15 | class MyClient extends Solarium_Client |
---|
16 | { |
---|
17 | /** |
---|
18 | * Querytype mappings |
---|
19 | */ |
---|
20 | protected $_queryTypes = array( |
---|
21 | self::QUERYTYPE_SELECT => array( |
---|
22 | 'query' => 'MyQuery', |
---|
23 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select', |
---|
24 | 'responseparser' => 'Solarium_Client_ResponseParser_Select' |
---|
25 | ), |
---|
26 | self::QUERYTYPE_UPDATE => array( |
---|
27 | 'query' => 'Solarium_Query_Update', |
---|
28 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Update', |
---|
29 | 'responseparser' => 'Solarium_Client_ResponseParser_Update' |
---|
30 | ), |
---|
31 | self::QUERYTYPE_PING => array( |
---|
32 | 'query' => 'Solarium_Query_Ping', |
---|
33 | 'requestbuilder' => 'Solarium_Client_RequestBuilder_Ping', |
---|
34 | 'responseparser' => 'Solarium_Client_ResponseParser_Ping' |
---|
35 | ), |
---|
36 | ); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | // create a client instance |
---|
41 | $client = new MyClient($config); |
---|
42 | |
---|
43 | // create a select query instance |
---|
44 | $query = $client->createSelect(); |
---|
45 | |
---|
46 | // check the query class, it should be our custom query class |
---|
47 | echo 'Query class: ' . get_class($query) . '<br/>'; |
---|
48 | |
---|
49 | // execute query |
---|
50 | $result = $client->execute($query); |
---|
51 | |
---|
52 | // display the total number of documents found by solr |
---|
53 | echo 'NumFound: '.$result->getNumFound(); |
---|
54 | |
---|
55 | // show documents using the resultset iterator |
---|
56 | foreach ($result as $document) { |
---|
57 | |
---|
58 | echo '<hr/><table>'; |
---|
59 | |
---|
60 | // the documents are also iterable, to get all fields |
---|
61 | foreach($document AS $field => $value) |
---|
62 | { |
---|
63 | // this converts multivalue fields to a comma-separated string |
---|
64 | if(is_array($value)) $value = implode(', ', $value); |
---|
65 | |
---|
66 | echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; |
---|
67 | } |
---|
68 | |
---|
69 | echo '</table>'; |
---|
70 | } |
---|
71 | |
---|
72 | htmlFooter(); |
---|