Commit ab585d41 authored by Bas de Nooijer's avatar Bas de Nooijer

Added per-field highlighting example

parent b9a315fb
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
$query->setQuery('memory');
// get highlighting component and apply settings
// highlights are applied to three fields with a different markup for each field
// much more per-field settings are available, see the manual for all options
$hl = $query->getHighlighting();
$hl->getField('name')->setSimplePrefix('<b>')->setSimplePostfix('</b>');
$hl->getField('cat')->setSimplePrefix('<u>')->setSimplePostfix('</u>');
$hl->getField('features')->setSimplePrefix('<i>')->setSimplePostfix('</i>');
// this executes the query and returns the result
$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>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// 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) {
echo implode(' (...) ', $highlight) . '<br/>';
}
}
}
htmlFooter();
\ No newline at end of file
......@@ -16,7 +16,7 @@
<b>Important:</b> This code is intended to demonstrate the usage of Solarium. It is not intended for real-world use. For instance user input validation is missing or very limited, as the best way to do this will depend on your application and is not Solarium functionality.<br>
It's advised not to deploy the examples to a public environment, or at least make sure the example directory is not available via your webserver.
</p>
<ul style="list-style:none;">
<li>1. Basic usage</li>
......@@ -44,6 +44,10 @@
</ul>
<li><a href="2.1.5.2-morelikethis.php">2.1.5.2 MoreLikeThis</a></li>
<li><a href="2.1.5.3-highlighting.php">2.1.5.3 Highlighting</a></li>
<ul style="list-style:none;">
<li><a href="2.1.5.3.1-per-field-highlighting.php">2.1.5.3.1 Per-field highlighting options</a></li>
</ul>
<li><a href="2.1.5.4-dismax.php">2.1.5.4 Dismax</a></li>
<li><a href="2.1.5.5-edismax.php">2.1.5.5 Edismax</a></li>
<li><a href="2.1.5.6-grouping-by-field.php">2.1.5.5 Grouping by field</a></li>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment