Created
May 4, 2011 06:26
-
-
Save basdenooijer/954832 to your computer and use it in GitHub Desktop.
Solarium example with facet filtering and pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('../library/Solarium/Autoloader.php'); | |
Solarium_Autoloader::register(); | |
$client = new Solarium_Client(); | |
$client->setHost('192.168.1.2'); | |
$client->setCore('geonames'); | |
$query = new Solarium_Query_Select; | |
$query->setRows(10); | |
$query->addSortField('name',Solarium_Query_Select::SORT_ASC); | |
// add a facet field | |
$countryFacet = new Solarium_Query_Select_Facet_Field; | |
$countryFacet->setKey('countries') | |
->setField('countrycode'); | |
$query->addFacet($countryFacet); | |
// handle facet choice by adding the value as a filterquery | |
$country = null; | |
if(isset($_GET['country']) && !empty($_GET['country'])) { | |
$country = $_GET['country']; | |
$fq = new Solarium_Query_Select_FilterQuery; | |
$fq->setKey('countryfilter'); | |
$fq->setQuery('countrycode:'.Solarium_Escape::term($country)); | |
$query->addFilterQuery($fq); | |
} | |
// handle paginating | |
$start = 0; | |
if(isset($_GET['start']) && !empty($_GET['start'])) { | |
$start = $_GET['start']; | |
$query->setStart($start); | |
} | |
$result = $client->select($query); | |
?> | |
<html> | |
<head> | |
<title>Facet interface demo</title> | |
</head> | |
<body> | |
<h1>Facet interface demo</h1> | |
<hr/> | |
<div style="float:left;"> | |
<h3>Country filter</h3> | |
<?php if($country !== null) { ?> | |
<a href="?">reset filter</a> | |
<?php } ?> | |
<ul> | |
<?php | |
// facet field is iterable | |
foreach ($result->getFacet('countries') AS $value => $count) { | |
echo '<li><a href="?country=' . $value . '">' . $value . ' (' . $count . ')</a></li>'; | |
} | |
?> | |
</ul> | |
</div> | |
<div style="float:left;margin-left:30px;"> | |
<?php | |
echo '<h3>Results found: '.$result->getNumFound().' (showing '.($start+1).' to '.($start+$query->getRows()).')</h3>'; | |
if ($start !== 0) { | |
echo '<a href="?country=' . $country . '&start='.($start-$query->getRows()).'">« previous</a>'; | |
} | |
if ($result->getNumFound() > ($start+$query->getRows())) { | |
echo ' <a href="?country=' . $country . '&start='.($start+$query->getRows()).'">next »</a>'; | |
} | |
// resultset is also iterable | |
foreach ($result AS $doc) { | |
echo '<table style="margin-bottom:20px; text-align:left; border:1px solid black; width:500px">'; | |
foreach ($doc AS $field => $value) { | |
echo '<tr><th>'.$field.'</th><td>'.$value.'</td></tr>'; | |
} | |
} | |
echo '</table>'; | |
?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Am getting This error
Severity: Warning
Message: require(/solarium/library/Solarium_Document_ReadWrite .php): failed to open stream: No such file or directory
Filename: Solarium/Autoloader.php
Line Number: 93
How to fix this error