2.3.1-mlt-query.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. require(__DIR__.'/init.php');
  3. use Solarium\Client;
  4. htmlHeader();
  5. // create a client instance
  6. $client = new Client($config);
  7. // get a morelikethis query instance
  8. $query = $client->createMoreLikeThis();
  9. $query->setQuery('id:SP2514N');
  10. $query->setMltFields('manu,cat');
  11. $query->setMinimumDocumentFrequency(1);
  12. $query->setMinimumTermFrequency(1);
  13. $query->createFilterQuery('stock')->setQuery('inStock:true');
  14. $query->setInterestingTerms('details');
  15. $query->setMatchInclude(true);
  16. // this executes the query and returns the result
  17. $resultset = $client->select($query);
  18. echo 'Document used for matching:<br/><table>';
  19. foreach ($resultset->getMatch() as $field => $value) {
  20. // this converts multivalue fields to a comma-separated string
  21. if (is_array($value)) {
  22. $value = implode(', ', $value);
  23. }
  24. echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
  25. }
  26. echo '</table><hr/>';
  27. // display the total number of MLT documents found by solr
  28. echo 'Number of MLT matches found: '.$resultset->getNumFound().'<br/><br/>';
  29. echo '<b>Listing of matched docs:</b>';
  30. // show MLT documents using the resultset iterator
  31. foreach ($resultset as $document) {
  32. echo '<hr/><table>';
  33. // the documents are also iterable, to get all fields
  34. foreach ($document as $field => $value) {
  35. // this converts multivalue fields to a comma-separated string
  36. if (is_array($value)) {
  37. $value = implode(', ', $value);
  38. }
  39. echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
  40. }
  41. echo '</table>';
  42. }
  43. htmlFooter();