Skip to content

Instantly share code, notes, and snippets.

@basdenooijer
Created May 4, 2011 06:26
Show Gist options
  • Save basdenooijer/954832 to your computer and use it in GitHub Desktop.
Save basdenooijer/954832 to your computer and use it in GitHub Desktop.
Solarium example with facet filtering and pagination
<?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()).'">&laquo; previous</a>';
}
if ($result->getNumFound() > ($start+$query->getRows())) {
echo ' <a href="?country=' . $country . '&start='.($start+$query->getRows()).'">next &raquo;</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>
@ambaldinesh44
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment