2.4.1-analysis-document.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. require(__DIR__.'/init.php');
  3. htmlHeader();
  4. // create a client instance
  5. $client = new Solarium\Client($config);
  6. // get an analysis document query
  7. $query = $client->createAnalysisDocument();
  8. $query->setShowMatch(true);
  9. $query->setQuery('ipod');
  10. $doc = new Solarium\QueryType\Update\Query\Document(
  11. array(
  12. 'id' => 'MA147LL',
  13. 'name' => 'Apple 60 GB iPod with Video Playback Black',
  14. 'manu' => 'Apple Computer Inc.',
  15. 'cat' => 'electronics',
  16. 'cat' => 'music',
  17. 'features' => 'iTunes, Podcasts, Audiobooks',
  18. 'features' => 'Stores up to 15,000 songs, 25,000 photos, or 150 hours of video',
  19. 'features' => '2.5-inch, 320x240 color TFT LCD display with LED backlight',
  20. 'features' => 'Up to 20 hours of battery life',
  21. 'features' => 'Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video',
  22. 'features' => 'Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, '.
  23. 'JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, '.
  24. 'Rechargeable capability, Battery level indication',
  25. 'includes' => 'earbud headphones, USB cable',
  26. 'weight' => 5.5,
  27. 'price' => 399.00,
  28. 'popularity' => 10,
  29. 'inStock' => true,
  30. )
  31. );
  32. $query->addDocument($doc);
  33. // this executes the query and returns the result
  34. $result = $client->analyze($query);
  35. // show the results
  36. foreach ($result as $document) {
  37. echo '<hr><h2>Document: ' . $document->getName() . '</h2>';
  38. foreach ($document as $field) {
  39. echo '<h3>Field: ' . $field->getName() . '</h3>';
  40. $indexAnalysis = $field->getIndexAnalysis();
  41. if (!empty($indexAnalysis)) {
  42. echo '<h4>Index Analysis</h4>';
  43. foreach ($indexAnalysis as $classes) {
  44. echo '<h5>'.$classes->getName().'</h5>';
  45. foreach ($classes as $result) {
  46. echo 'Text: ' . $result->getText() . '<br/>';
  47. echo 'Raw text: ' . $result->getRawText() . '<br/>';
  48. echo 'Start: ' . $result->getStart() . '<br/>';
  49. echo 'End: ' . $result->getEnd() . '<br/>';
  50. echo 'Position: ' . $result->getPosition() . '<br/>';
  51. echo 'Position history: ' . implode(', ', $result->getPositionHistory()) . '<br/>';
  52. echo 'Type: ' . htmlspecialchars($result->getType()) . '<br/>';
  53. echo 'Match: ' . var_export($result->getMatch(), true) . '<br/>';
  54. echo '-----------<br/>';
  55. }
  56. }
  57. }
  58. $queryAnalysis = $field->getQueryAnalysis();
  59. if (!empty($queryAnalysis)) {
  60. echo '<h4>Query Analysis</h4>';
  61. foreach ($queryAnalysis as $classes) {
  62. echo '<h5>'.$classes->getName().'</h5>';
  63. foreach ($classes as $result) {
  64. echo 'Text: ' . $result->getText() . '<br/>';
  65. echo 'Raw text: ' . $result->getRawText() . '<br/>';
  66. echo 'Start: ' . $result->getStart() . '<br/>';
  67. echo 'End: ' . $result->getEnd() . '<br/>';
  68. echo 'Position: ' . $result->getPosition() . '<br/>';
  69. echo 'Position history: ' . implode(', ', $result->getPositionHistory()) . '<br/>';
  70. echo 'Type: ' . htmlspecialchars($result->getType()) . '<br/>';
  71. echo 'Match: ' . var_export($result->getMatch(), true) . '<br/>';
  72. echo '-----------<br/>';
  73. }
  74. }
  75. }
  76. }
  77. }
  78. htmlFooter();