source: sandbox/expresso-solr/expressoMail1_2/solrclient/examples/2.1.5.3.1-per-field-highlighting.php @ 7588

Revision 7588, 1.7 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1<?php
2
3require('init.php');
4htmlHeader();
5
6// create a client instance
7$client = new Solarium_Client($config);
8
9// get a select query instance
10$query = $client->createSelect();
11$query->setQuery('memory');
12
13// get highlighting component and apply settings
14// highlights are applied to three fields with a different markup for each field
15// much more per-field settings are available, see the manual for all options
16$hl = $query->getHighlighting();
17$hl->getField('name')->setSimplePrefix('<b>')->setSimplePostfix('</b>');
18$hl->getField('cat')->setSimplePrefix('<u>')->setSimplePostfix('</u>');
19$hl->getField('features')->setSimplePrefix('<i>')->setSimplePostfix('</i>');
20
21// this executes the query and returns the result
22$resultset = $client->select($query);
23$highlighting = $resultset->getHighlighting();
24// display the total number of documents found by solr
25echo 'NumFound: '.$resultset->getNumFound();
26
27// show documents using the resultset iterator
28foreach ($resultset as $document) {
29
30    echo '<hr/><table>';
31
32    // the documents are also iterable, to get all fields
33    foreach($document AS $field => $value)
34    {
35        // this converts multivalue fields to a comma-separated string
36        if(is_array($value)) $value = implode(', ', $value);
37
38        echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
39    }
40
41    echo '</table><br/><b>Highlighting results:</b><br/>';
42
43    // highlighting results can be fetched by document id (the field defined as uniquekey in this schema)
44    $highlightedDoc = $highlighting->getResult($document->id);
45    if($highlightedDoc){
46        foreach($highlightedDoc as $field => $highlight) {
47            echo implode(' (...) ', $highlight) . '<br/>';
48        }
49    }
50
51}
52
53htmlFooter();
Note: See TracBrowser for help on using the repository browser.