Using Solarium with SOLR for Search – Advanced

Posted by Unknown on Wednesday, May 7, 2014

This is the fourth and final part of a series on using Apache’s SOLR search implementation along with Solarium, a PHP library to integrate it into your application as if it were native.


In the first three parts we installed and configured SOLR and Solarium and started building an example application for searching movies. We’ve also looked at faceted search.


We’re going to wrap up the series by looking at some more advanced features of SOLR, and how to use them with Solarium.


Highlighting Results with SOLR


The Highlighting component allows you to highlight the parts of a document which have matched your search. Its behavior around what gets shown depends on the field - if it’s a title chances are it’ll show it in its entirety with the matched words present, and longer fields - such as a synopsis or the body of an article - it will highlight the words but using snippets; much like Google’s search results do.


To set up highlighting, you first need to specify the fields to include. Then, you can set a prefix and corresponding postfix for the highlighted words or phrases. So for example, to make highlighted words and phrases bold:



$hl = $query->getHighlighting();
$hl->setFields(array('title', 'synopsis'));
$hl->setSimplePrefix('');
$hl->setSimplePostfix('
');


Alternatively, to add a background color:



$hl = $query->getHighlighting();
$hl->setFields(array('title', 'synopsis'));
$hl->setSimplePrefix('');
$hl->setSimplePostfix('
');


Or you can even use per-field settings:



$hl = $query->getHighlighting();
$hl->getField('title')->setSimplePrefix('')->setSimplePostfix('');
$hl->getField('synopsis')->setSimplePrefix('')->setSimplePostfix('');


Once you’ve configured the highlighting component in your search implementation, there’s a little more work to do involved in displaying it in your search results view.


First, you need to extract the highlighted document from the highlighting component by ID:



$highlightedDoc = $highlighting->getResult($document->id);


Now, you can access all the highlighted fields by iterating through them, as properties of the highlighted document:



if($highlightedDoc){
foreach($highlightedDoc as $field => $highlight) {
echo implode(' (...) ', $highlight) . '
';
}
}


Or, you can use getField():



if($highlightedDoc){
$highlightedTitle = $highlightedDoc->getField('title');
}


Highlighted fields don’t simply return text, however Instead, they’ll return an array of “snippets” of text. If there are no matches for that particular field - for example if your search matched on title but not synopsis - then that array will be empty.


The code above will return a maximum of one snippet. To change this behavior, you can use the setSnippets() method:



$hl = $query->getHighlighting();
$hl->setSnippets(5);
// . . . as before . . .


For example, suppose you search for the word “star”. One of the results has a synopsis that reads as follows:



This not to be missed movie theater event will feature one of the most memorable moments in TV history and exclusive clips about the making of The Best of Both Worlds and Star Trek: The Next Generation Season 3. Set in the 24th century, The Next Generation was created by Gene Roddenberry over 20 years after the original Star Trek series. The Next Generation became the longest running series of the Star Trek franchise, consisting of 178 episodes over 7 seasons. Star Trek: The Next Generation - The Best of Both Worlds is the first opportunity to see The Best of Both Worlds, one of the greatest TV episodes of all time, as a gloriously remastered full-length feature in select movie theaters nationwide.



Continue reading %Using Solarium with SOLR for Search – Advanced%




more

{ 0 comments... » Using Solarium with SOLR for Search – Advanced read them below or add one }

Post a Comment

Popular Posts