2.1.5.9-spellcheck.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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->setRows(0);
  9. // add spellcheck settings
  10. $spellcheck = $query->getSpellcheck();
  11. $spellcheck->setQuery('tes');
  12. $spellcheck->setCount(10);
  13. $spellcheck->setBuild(true);
  14. $spellcheck->setCollate(true);
  15. $spellcheck->setExtendedResults(true);
  16. $spellcheck->setCollateExtendedResults(true);
  17. // this executes the query and returns the result
  18. $resultset = $client->select($query);
  19. $spellcheckResult = $resultset->getSpellcheck();
  20. echo '<h1>Correctly spelled?</h1>';
  21. if ($spellcheckResult->getCorrectlySpelled()) {
  22. echo 'yes';
  23. } else {
  24. echo 'no';
  25. }
  26. echo '<h1>Suggestions</h1>';
  27. foreach ($spellcheckResult as $suggestion) {
  28. echo 'NumFound: '.$suggestion->getNumFound().'<br/>';
  29. echo 'StartOffset: '.$suggestion->getStartOffset().'<br/>';
  30. echo 'EndOffset: '.$suggestion->getEndOffset().'<br/>';
  31. echo 'OriginalFrequency: '.$suggestion->getOriginalFrequency().'<br/>';
  32. foreach ($suggestion->getWords() as $word) {
  33. echo '-----<br/>';
  34. echo 'Frequency: '.$word['freq'].'<br/>';
  35. echo 'Word: '.$word['word'].'<br/>';
  36. }
  37. echo '<hr/>';
  38. }
  39. $collations = $spellcheckResult->getCollations();
  40. echo '<h1>Collations</h1>';
  41. foreach ($collations as $collation) {
  42. echo 'Query: '.$collation->getQuery().'<br/>';
  43. echo 'Hits: '.$collation->getHits().'<br/>';
  44. echo 'Corrections:<br/>';
  45. foreach ($collation->getCorrections() as $input => $correction) {
  46. echo $input . ' => ' . $correction .'<br/>';
  47. }
  48. echo '<hr/>';
  49. }
  50. htmlFooter();