0

我将 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/>';
        }
    }

}

问题是结果是这样的:

样本

我想直接在文本中突出显示单词。谢谢!

4

1 回答 1

0

目前,Solr(最高 v4.9 草案)仅支持返回高亮片段 AFAIK,尽管它服务于通用用途。我也认为这不太实用,但是开发团队可能会考虑一些性能问题,他们不支持将高亮标签直接嵌入结果集中的所有文本中。(注意:如果我错了或误解,请纠正我)

但是,以下链接可能会对您有所帮助:http ://www.beingjavaguys.com/2013/01/how-to-integrate-highlighting-in-search.html

于 2014-06-20T08:52:59.980 回答