|
9 | 9 | $client = new Client($config); |
10 | 10 |
|
11 | 11 | // get a morelikethis query instance |
12 | | -$query = $client->createMoreLikeThis(); |
| 12 | +$query = $client->createSelect() |
| 13 | + // Unfortunately the /mlt handler of the techproducts examlpe doesn't exist anymore. |
| 14 | + // Therefore we have to use the /browse handler and turn of velocity by forcing json as response writer. |
| 15 | + ->setHandler('browse') |
| 16 | + ->setResponseWriter(\Solarium\Core\Query\AbstractQuery::WT_JSON); |
13 | 17 |
|
14 | | -$query->setQuery('id:SP2514N'); |
15 | | -$query->setMltFields('manu,cat'); |
16 | | -$query->setMinimumDocumentFrequency(1); |
17 | | -$query->setMinimumTermFrequency(1); |
18 | | -$query->createFilterQuery('stock')->setQuery('inStock:true'); |
19 | | -$query->setInterestingTerms('details'); |
20 | | -$query->setMatchInclude(true); |
| 18 | +$query->getMoreLikeThis() |
| 19 | + ->setFields('manu,cat') |
| 20 | + ->setMinimumDocumentFrequency(1) |
| 21 | + ->setMinimumTermFrequency(1) |
| 22 | + ->setInterestingTerms('details') |
| 23 | + ->setMatchInclude(true); |
| 24 | + |
| 25 | +$query->setQuery('id:SP2514N') |
| 26 | + ->createFilterQuery('stock')->setQuery('inStock:true'); |
21 | 27 |
|
22 | 28 | // this executes the query and returns the result |
23 | 29 | $resultset = $client->select($query); |
24 | 30 |
|
25 | | -echo 'Document used for matching:<br/><table>'; |
26 | | -foreach ($resultset->getMatch() as $field => $value) { |
27 | | - // this converts multivalue fields to a comma-separated string |
28 | | - if (is_array($value)) { |
29 | | - $value = implode(', ', $value); |
30 | | - } |
31 | | - |
32 | | - echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; |
33 | | -} |
34 | | -echo '</table><hr/>'; |
35 | | - |
36 | | -// display the total number of MLT documents found by solr |
37 | | -echo 'Number of MLT matches found: '.$resultset->getNumFound().'<br/><br/>'; |
38 | | -echo '<b>Listing of matched docs:</b>'; |
39 | | - |
40 | | -// show MLT documents using the resultset iterator |
| 31 | +echo 'Documents used for matching:<br/>'; |
| 32 | +// show documents using the resultset iterator |
41 | 33 | foreach ($resultset as $document) { |
42 | 34 |
|
43 | | - echo '<hr/><table>'; |
| 35 | + echo '<table>'; |
44 | 36 |
|
45 | 37 | // the documents are also iterable, to get all fields |
46 | 38 | foreach ($document as $field => $value) { |
|
55 | 47 | echo '</table>'; |
56 | 48 | } |
57 | 49 |
|
| 50 | +echo '<hr/>'; |
| 51 | + |
| 52 | +$mlt = $resultset->getMoreLikeThis(); |
| 53 | + |
| 54 | +// display the total number of MLT documents found by solr |
| 55 | +echo 'Number of MLT matches found: '.$resultset->getNumFound().'<br/><br/>'; |
| 56 | +echo '<b>Listing of matched docs:</b>'; |
| 57 | + |
| 58 | +// show MLT documents using the resultset iterator |
| 59 | +foreach ($mlt as $results) { |
| 60 | + foreach ($results as $document) { |
| 61 | + echo '<hr/><table>'; |
| 62 | + |
| 63 | + // the documents are also iterable, to get all fields |
| 64 | + foreach ($document as $field => $value) { |
| 65 | + // this converts multivalue fields to a comma-separated string |
| 66 | + if (is_array($value)) { |
| 67 | + $value = implode(', ', $value); |
| 68 | + } |
| 69 | + |
| 70 | + echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; |
| 71 | + } |
| 72 | + |
| 73 | + echo '</table>'; |
| 74 | + } |
| 75 | +} |
| 76 | + |
58 | 77 | htmlFooter(); |
0 commit comments