0
我使用Solarim for Solr。 我想在某個表達式之後在我的文檔中進行搜索並突出顯示這些單詞。這裏是我的代碼:Solr直接在文本上突出顯示
// 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/>';
}
}
}
的問題是,結果類似的東西:
我想直接突出詞語的文本。 謝謝!