Skip to content

Instantly share code, notes, and snippets.

@vstarck
Created November 22, 2012 18:26
Show Gist options
  • Save vstarck/4132477 to your computer and use it in GitHub Desktop.
Save vstarck/4132477 to your computer and use it in GitHub Desktop.
solrmarket.php
<?php
// Autoloader
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
// Create a client instance
$client = new Solarium_Client(array(
'adapteroptions' => array(
'host' => '192.168.0.111',
'port' => 8985,
'path' => '/solr',
)
));
define('STORE_ID', 1);
define('FIELD_STORE_ID', 'store_id');
define('FIELD_NAME', 'attr_sort_name_en');
define('FIELD_BRAND', 'category_ids');
define('FIELD_PRICE', 'price_0_1');
define('PRICE_GAP', 100);
define('PER_PAGE', 50);
define('HOST', 'http://localhost/solarium_test/examples/market.php');
function get($value, $default = null)
{
return isset($_GET[$value]) ? $_GET[$value] : $default;
}
function create_link($page = null, $sort = null, $brand = null, $price = null)
{
if (!$page) {
$page = get('page', 1);
}
if (!$sort) {
$sort = get('sort', 'asc');
}
$link = HOST . "?page=$page&sort=$sort";
if (!$brand && get('brand')) {
$link .= '&brand=' . get('brand');
} else if ($brand && $brand != '*') {
$link .= '&brand=' . $brand;
}
if (!$price && get('price')) {
$link .= '&price=' . get('price');
} else if ($price && $price != '*') {
$link .= '&price=' . $price;
}
return $link;
}
function price_link($price)
{
$price = preg_replace('/[^\*\d-]/', '', $price);
return create_link(null, null, null, $price);
}
function brand_link($brand)
{
return create_link(null, null, $brand);
}
function sort_link_asc()
{
return create_link(null, 'asc');
}
function sort_link_desc()
{
return create_link(null, 'desc');
}
function page_link($page)
{
return create_link($page);
}
function setup_facets_query(Solarium_Query_Select $query)
{
$facetSet = $query->getFacetSet();
$facetSet->createFacetField('brand')->setField(FIELD_BRAND);
$facet = $facetSet->createFacetRange('priceranges');
$facet->setField(FIELD_PRICE);
$facet->setStart(1);
$facet->setGap(PRICE_GAP);
$facet->setEnd(1000);
}
function setup_facets_result(Solarium_Result_Select $result, $target)
{
$facets = $result->getFacetSet();
$target->facet = array();
$target->facet['price'] = array();
$target->facet['brand'] = array();
foreach ($facets->getFacet('priceranges') as $range => $count) {
if ($count) {
$range = (int)$range;
$to = $range + PRICE_GAP;
$target->facet['price']["$$range - $$to"] = $count;
}
}
foreach ($facets->getFacet('brand') as $brand => $count) {
if ($count) {
$target->facet['brand'][$brand] = $count;
}
}
}
function setup_filters_query(Solarium_Query_Select $query)
{
if (get('brand')) {
$query->createFilterQuery('brand')->setQuery(FIELD_BRAND . ':' . get('brand'));
}
if (get('price')) {
$price = get('price');
$price = preg_replace('/-/', ' TO ', $price);
$query->createFilterQuery(FIELD_PRICE)->setQuery("price:[$price]");
}
$query->createFilterQuery(FIELD_STORE_ID)->setQuery(FIELD_STORE_ID . ':' . STORE_ID);
}
function get_all(Solarium_Client $client)
{
$all = new stdClass();
// get a select query instance
$query = $client->createSelect();
$page = get('page', 1) - 1;
$query->setStart($page * PER_PAGE);
$query->setRows(PER_PAGE);
$order = get('sort', 'asc') == 'asc' ? Solarium_Query_Select::SORT_ASC : Solarium_Query_Select::SORT_DESC;
$query->addSort(FIELD_PRICE, $order);
setup_facets_query($query);
setup_filters_query($query);
// this executes the query and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
$all->count = $resultset->getNumFound();
$all->items = $resultset->getIterator();
setup_facets_result($resultset, $all);
return $all;
}
$all = get_all($client);
?>
<!DOCTYPE html>
<html>
<head>
<title>SOLRMarket</title>
<link href='http://fonts.googleapis.com/css?family=Audiowide' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Anaheim' rel='stylesheet' type='text/css'>
<style>
* {
margin: 0;
padding: 0;
font-family: 'Anaheim', sans-serif;
font-size: 12px;
}
a {
text-decoration: none;
}
html {
background: #b3bead; /* Old browsers */
background: -moz-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fcfff4), color-stop(40%, #dfe5d7), color-stop(100%, #b3bead)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%); /* IE10+ */
background: linear-gradient(to bottom, #fcfff4 0%, #dfe5d7 40%, #b3bead 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#fcfff4', endColorstr = '#b3bead', GradientType = 0); /* IE6-9 */
}
body {
width: 970px;
margin: 0 auto;
border-left: solid 1px #333;
border-right: solid 1px #333;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 0px 0px 10px #888;
}
.container {
position: relative;
height: 100%;
padding: 0 15px;
background-color: #fff;
}
footer {
clear: both;
bottom: 0;
font-size: 9px;
padding-bottom: 2px;
}
h1, h1 span {
color: #333;
font-family: 'Audiowide', cursive;
font-size: 48pt;
text-shadow: 2px 2px 2px #820000;
}
h1 span {
color: #820000;
text-shadow: 2px 2px 2px #555;
}
.navigation {
float: left;
width: 170px;
}
.main {
float: left;
width: 750px;
}
li {
list-style: none;
}
.products li {
float: left;
width: 164px;
height: 160px;
border: solid 1px #888;
margin: 3px 5px;
padding: 5px;
text-align: center;
border-radius: 3px;
}
.products li:hover {
background-color: #deb6b8;
cursor: pointer;
}
.main li h4 {
font-size: 13px;
}
.main li h3 {
font-size: 20px;
font-family: 'Audiowide', cursive;
}
div.placeholder {
background: none repeat scroll 0 0 #CCCCCC;
height: 80px;
width: 163px;
border: solid 1px #777;
border-radius: 1px;
}
.navigation h3 {
margin-top: 10px;
font-size: 20px;
font-family: 'Audiowide', cursive;
}
li.facet * {
cursor: pointer;
}
li.facet:hover {
color: #820000;
}
.pagination {
clear: both;
}
.pagination li {
float: left;
margin: 0px 2px;
font-size: 15px;
}
.pagination li a {
font-weight: bold;
display: block;
font-size: 14px;
line-height: 20px;
color: #555;
}
.pagination li a.current {
font-size: 20px;
}
.total {
width: 650px;
}
</style>
</head>
<body>
<div class="container">
<h1>SOLR<span>Market</span></h1>
<div class="navigation">
<h3>Price</h3>
<ul>
<li class="facet">
<a href="<?php echo price_link('*') ?>">
<label>View All</label>
</a>
</li>
<?php foreach ($all->facet['price'] as $key => $value): ?>
<li class="facet">
<a href="<?php echo price_link($key) ?>">
<label><?php echo ucfirst($key) ?></label>
<span>(<?php echo $value ?>)</span>
</a>
</li>
<?php endforeach; ?>
</ul>
<h3>Brand</h3>
<ul>
<li class="facet">
<a href="<?php echo brand_link('*') ?>">
<label>View All</label>
</a>
</li>
<?php foreach ($all->facet['brand'] as $key => $value): ?>
<li class="facet">
<a href="<?php echo brand_link($key) ?>">
<label><?php echo ucfirst($key) ?></label>
<span>(<?php echo $value ?>)</span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="main">
<label for="order">Order by</label>
<select id="order" onchange="window.location.href=this.value" autocomplete="off">
<option value="<?php echo sort_link_asc()?>"
<?php if (get('sort', 'asc') == 'asc'): ?>
selected="selected"
<?php endif; ?>
>
Price ASC
</option>
<option
value="<?php echo sort_link_desc()?>"
<?php if (get('sort', 'asc') == 'desc'): ?>
selected="selected"
<?php endif; ?>
>
Price DESC
</option>
</select>
<ul class="products">
<?php foreach ($all->items as $item): ?>
<li data-id="<?php echo $item->id ?>">
<div class="placeholder"></div>
<h4><?php echo $item->{FIELD_NAME} ?></h4>
<h3>$<?php echo number_format($item->{FIELD_PRICE}, 2, '.', ''); ?></h3>
</li>
<?php endforeach; ?>
</ul>
<ul class="pagination">
<li class="total">Total: <?php echo $all->count?></li>
<?php for ($i = 0; $i < ceil($all->count / PER_PAGE); $i++): ?>
<li>
<a
href="<?php echo page_link($i + 1) ?>"
<?php if (get('page', 1) == $i + 1): ?>
class="current"
<?php endif;?>
>
<?php echo $i + 1?>
</a>
</li>
<?php endfor;?>
</ul>
</div>
<footer>
SOLRMarket &copy; 2012
</footer>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment