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 in the following query:

  $hl->setFragSize(0);

Set the mergeContiguous flag to merge contiguous fragments into a single fragment as shown in the following code:

  $hl->setMergeContiguous(true);

Set highlightMultiTerm flag to enable highlighting for range, wildcard, fuzzy, and prefix queries as shown in the following query:

  $hl->setHighlightMultiTerm(true);

Once the query is run and the result-set is received, we will need to retrieve the highlighted results from the result-set with the following query:

  $hlresults = $resultSet->getHighlighting();

For each document in the result-set, we will need to get the highlighted document from the highlighted result-set. We will need to pass the unique ID as identifier in the getResult() function to get the highlighted document as shown in the following code:

foreach($resultSet as $doc)
{
  $hldoc = $hlresults->getResult($doc->id);
  $hlname = implode(',',$hldoc->getField('name'));
  $hlseries = implode(',',$hldoc->getField('series_t'));
}

Here the highlighted fields for each document, which we obtain using the getField() method, function is returned as an array. This is why we have to implode it before display. We can see that in the output the fields are highlighted using the bold—<strong> and </strong> tags.

In Solr logs, we can see all the parameters that we have specified in our PHP code as given in the following:

  336647163 [http-bio-8080-exec-1] INFO  org.apache.solr.core.SolrCore  – [collection1] webapp=/solr path=/select params={hl.fragsize=0&hl.mergeContiguous=true&hl.simple.pre=    <strong>&hl.fl=name,series_t&wt=json&hl=true    &rows=25&hl.highlightMultiTerm=true&omitHeader=true&fl=id,name,author,series_t,score,last_modified&hl.snippets=2&start=0&q=harry&hl.simple.post=</strong>} hits=7 status=0 QTime=203

The parameter passed to enable highlighting is hl=true and the fields to be highlighted is specified as hl.fl=name,series_t.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.103.219