我将 Solarim 用于 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/>';
}
}
}
问题是结果是这样的:
我想直接在文本中突出显示单词。谢谢!