[7576] | 1 | <?php |
---|
| 2 | |
---|
| 3 | require('init.php'); |
---|
| 4 | htmlHeader(); |
---|
| 5 | |
---|
| 6 | // create a client instance |
---|
| 7 | $client = new Solarium_Client($config); |
---|
| 8 | |
---|
| 9 | // get a select query instance |
---|
| 10 | $query = $client->createSelect(); |
---|
| 11 | $query->setQuery('memory'); |
---|
| 12 | |
---|
| 13 | // get highlighting component and apply settings |
---|
| 14 | $hl = $query->getHighlighting(); |
---|
| 15 | $hl->setFields('name, features'); |
---|
| 16 | $hl->setSimplePrefix('<b>'); |
---|
| 17 | $hl->setSimplePostfix('</b>'); |
---|
| 18 | |
---|
| 19 | // this executes the query and returns the result |
---|
| 20 | $resultset = $client->select($query); |
---|
| 21 | $highlighting = $resultset->getHighlighting(); |
---|
| 22 | // display the total number of documents found by solr |
---|
| 23 | echo 'NumFound: '.$resultset->getNumFound(); |
---|
| 24 | |
---|
| 25 | // show documents using the resultset iterator |
---|
| 26 | foreach ($resultset as $document) { |
---|
| 27 | |
---|
| 28 | echo '<hr/><table>'; |
---|
| 29 | |
---|
| 30 | // the documents are also iterable, to get all fields |
---|
| 31 | foreach($document AS $field => $value) |
---|
| 32 | { |
---|
| 33 | // this converts multivalue fields to a comma-separated string |
---|
| 34 | if(is_array($value)) $value = implode(', ', $value); |
---|
| 35 | |
---|
| 36 | echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | echo '</table><br/><b>Highlighting results:</b><br/>'; |
---|
| 40 | |
---|
| 41 | // highlighting results can be fetched by document id (the field defined as uniquekey in this schema) |
---|
| 42 | $highlightedDoc = $highlighting->getResult($document->id); |
---|
| 43 | if($highlightedDoc){ |
---|
| 44 | foreach($highlightedDoc as $field => $highlight) { |
---|
| 45 | echo implode(' (...) ', $highlight) . '<br/>'; |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | htmlFooter(); |
---|