2.1.5.3-highlighting.php 1.5 KB

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