Highlighting in Solr using PHP and Solarium
Let us try regular highlighting using PHP. Search for harry
in our index and highlight two fields—name
and series_t
as shown in the following code:
$query->setQuery('harry'); $query->setFields(array('id','name','author','series_t','score','last_modified'));
First get the highlighting component from the following query:
$hl = $query->getHighlighting();
Set fields we want to highlight using the following query:
$hl->setFields('name,series_t');
Set the highlighting HTML tags as bold using the following query:
$hl->setSimplePrefix('<strong>'); $hl->setSimplePostfix('</strong>');
Set the maximum number of highlighted snippets to be generated per field. In this case any number of highlighted snippets from 0 to 2 can be generated as shown in the following query:
$hl->setSnippets(2);
Set the size in characters of fragments to consider for highlighting. 0 uses the whole field value without any fragmentation as shown...