0

I use Solarim for Solr. I want to seach in my documents after a certain expresion and highlight the words. Here is my code:

 // get a select query instance
$query = $client->createSelect();
$query->setQuery('*ac*');

$hl = $query->getHighlighting();
$hl->getField('name')->setSimplePrefix('<b style="background: none repeat scroll 0 0 #6BAE48;color: #FF0000;">')->setSimplePostfix('</b>');
$hl->getField('description')->setSimplePrefix('<u style="background: none repeat scroll 0 0 yellow;">')->setSimplePostfix('</u>');


$resultset = $client->select($query);
$highlighting = $resultset->getHighlighting();
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();

// show documents using the resultset iterator
foreach ($resultset as $document) {

    echo '<hr/><table>';

   foreach($document AS $field => $value)
    {
        $highlightedDoc = $highlighting->getResult($document->id);
        // this converts multivalue fields to a comma-separated string
        if(is_array($value)) $value = implode(', ', $value);

        echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
    }

    echo '</table><br/><b>Highlighting results:</b><br/>';

    // highlighting results can be fetched by document id (the field defined as uniquekey in this schema)
    $highlightedDoc = $highlighting->getResult($document->id);
    if($highlightedDoc){
        foreach($highlightedDoc as $field => $highlight) {
            deg($highlight);
            echo implode(' (...) ', $highlight) . '<br/>';
        }
    }

}

The problem is the result is something like that:

sample

I want to highlight the words directly in the text. Thank you!

Andreea
  • 139
  • 12

1 Answers1

1

Currently Solr, up to v4.9 draft, only supports returning hightlight snippets AFAIK although it serves general-purpose use. I also think this is less practical, however there's maybe some performance issue consideration from the develop team that they don't support directly embedding hightlight tags into all text in result set. (note: Correct me if I'm wrong or misunderstanding)

However following link may help you: http://www.beingjavaguys.com/2013/01/how-to-integrate-highlighting-in-search.html

Scott Chu
  • 972
  • 14
  • 26