1 | <?php |
---|
2 | |
---|
3 | require('init.php'); |
---|
4 | htmlHeader(); |
---|
5 | |
---|
6 | // create a client instance |
---|
7 | $client = new Solarium_Client($config); |
---|
8 | |
---|
9 | // get a select query instance |
---|
10 | $query = $client->createSelect(); |
---|
11 | |
---|
12 | // add a query and morelikethis settings (using fluent interface) |
---|
13 | $query->setQuery('apache') |
---|
14 | ->getMoreLikeThis() |
---|
15 | ->setFields('manu,cat') |
---|
16 | ->setMinimumDocumentFrequency(1) |
---|
17 | ->setMinimumTermFrequency(1); |
---|
18 | |
---|
19 | // this executes the query and returns the result |
---|
20 | $resultset = $client->select($query); |
---|
21 | $mlt = $resultset->getMoreLikeThis(); |
---|
22 | |
---|
23 | // display the total number of documents found by solr |
---|
24 | echo 'NumFound: '.$resultset->getNumFound(); |
---|
25 | |
---|
26 | // show documents using the resultset iterator |
---|
27 | foreach ($resultset as $document) { |
---|
28 | |
---|
29 | echo '<hr/><table>'; |
---|
30 | |
---|
31 | // the documents are also iterable, to get all fields |
---|
32 | foreach($document AS $field => $value) |
---|
33 | { |
---|
34 | // this converts multivalue fields to a comma-separated string |
---|
35 | if(is_array($value)) $value = implode(', ', $value); |
---|
36 | |
---|
37 | echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; |
---|
38 | } |
---|
39 | |
---|
40 | echo '</table><br/><b>MLT results:</b><br/>'; |
---|
41 | |
---|
42 | // mlt results can be fetched by document id (the field defined as uniquekey in this schema) |
---|
43 | $mltResult = $mlt->getResult($document->id); |
---|
44 | if($mltResult){ |
---|
45 | echo 'Max score: '.$mltResult->getMaximumScore().'<br/>'; |
---|
46 | echo 'NumFound: '.$mltResult->getNumFound().'<br/>'; |
---|
47 | echo 'Num. fetched: '.count($mltResult).'<br/>'; |
---|
48 | foreach($mltResult AS $mltDoc) { |
---|
49 | echo 'MLT result doc: '. $mltDoc->name . ' (id='. $mltDoc->id . ')<br/>'; |
---|
50 | } |
---|
51 | }else{ |
---|
52 | echo 'No MLT results'; |
---|
53 | } |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | htmlFooter(); |
---|