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

Merge pull request #318 from Stratinc/develop

Facet interval support
parents df289677 4c7e202e
<?php
require(__DIR__.'/init.php');
htmlHeader();
// create a client instance
$client = new Solarium\Client($config);
// get a select query instance
$query = $client->createSelect();
// get the facetset component
$facetSet = $query->getFacetSet();
// create a facet field instance and set options
$facet = $facetSet->createFacetInterval('price');
$facet->setField('price');
$facet->setSet(array('1-9' => '[1,10)', '10-49' => '[10,50)', '49>' => '[50,*)'));
// this executes the query and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// display facet counts
echo '<hr/>Facet intervals:<br/>';
$facet = $resultset->getFacetSet()->getFacet('price');
foreach ($facet as $range => $count) {
echo $range . ' to ' . ($range + 100) . ' [' . $count . ']<br/>';
}
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
echo '</table>';
}
htmlFooter();
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
<li><a href="2.1.5.1.3-facet-multiquery.php">2.1.5.1.3 Facet multiquery</a></li> <li><a href="2.1.5.1.3-facet-multiquery.php">2.1.5.1.3 Facet multiquery</a></li>
<li><a href="2.1.5.1.4-facet-range.php">2.1.5.1.4 Facet range</a></li> <li><a href="2.1.5.1.4-facet-range.php">2.1.5.1.4 Facet range</a></li>
<li><a href="2.1.5.1.5-facet-pivot.php">2.1.5.1.5 Facet pivot</a></li> <li><a href="2.1.5.1.5-facet-pivot.php">2.1.5.1.5 Facet pivot</a></li>
<li><a href="2.1.5.1.6-facet-interval.php">2.1.5.1.6 Facet interval</a></li>
</ul> </ul>
<li><a href="2.1.5.2-morelikethis.php">2.1.5.2 MoreLikeThis</a></li> <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> <li><a href="2.1.5.3-highlighting.php">2.1.5.3 Highlighting</a></li>
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\Select\Query\Component\Facet;
use Solarium\QueryType\Select\Query\Component\FacetSet;
/**
* Facet interval
*
* @link http://wiki.apache.org/solr/SimpleFacetParameters#Interval_Faceting
*/
class Interval extends Facet
{
/**
* Initialize options
*
* Several options need some extra checks or setup work, for these options
* the setters are called.
*
* @return void
*/
protected function init()
{
parent::init();
foreach ($this->options as $name => $value) {
switch ($name) {
case 'set':
$this->setSet($value);
break;
}
}
}
/**
* Get the facet type
*
* @return string
*/
public function getType()
{
return FacetSet::FACET_INTERVAL;
}
/**
* Set the field name
*
* @param string $field
* @return self Provides fluent interface
*/
public function setField($field)
{
return $this->setOption('field', $field);
}
/**
* Get the field name
*
* @return string
*/
public function getField()
{
return $this->getOption('field');
}
/**
* Set set counts
*
* Use one of the constants as value.
* If you want to use multiple values supply an array or comma separated string
*
* @param string|array $set
* @return self Provides fluent interface
*/
public function setSet($set)
{
if (is_string($set)) {
$set = explode(',', $set);
$set = array_map('trim', $set);
}
return $this->setOption('set', $set);
}
/**
* Get set counts
*
* @return array
*/
public function getSet()
{
$set = $this->getOption('set');
if ($set === null) {
$set = array();
}
return $set;
}
}
...@@ -77,6 +77,11 @@ class FacetSet extends Component ...@@ -77,6 +77,11 @@ class FacetSet extends Component
*/ */
const FACET_PIVOT = 'pivot'; const FACET_PIVOT = 'pivot';
/**
* Facet type interval
*/
const FACET_INTERVAL = 'interval';
/** /**
* Facet type mapping * Facet type mapping
* *
...@@ -88,6 +93,7 @@ class FacetSet extends Component ...@@ -88,6 +93,7 @@ class FacetSet extends Component
self::FACET_MULTIQUERY => 'Solarium\QueryType\Select\Query\Component\Facet\MultiQuery', self::FACET_MULTIQUERY => 'Solarium\QueryType\Select\Query\Component\Facet\MultiQuery',
self::FACET_RANGE => 'Solarium\QueryType\Select\Query\Component\Facet\Range', self::FACET_RANGE => 'Solarium\QueryType\Select\Query\Component\Facet\Range',
self::FACET_PIVOT => 'Solarium\QueryType\Select\Query\Component\Facet\Pivot', self::FACET_PIVOT => 'Solarium\QueryType\Select\Query\Component\Facet\Pivot',
self::FACET_INTERVAL => 'Solarium\QueryType\Select\Query\Component\Facet\Interval',
); );
/** /**
...@@ -526,4 +532,16 @@ class FacetSet extends Component ...@@ -526,4 +532,16 @@ class FacetSet extends Component
{ {
return $this->createFacet(self::FACET_PIVOT, $options, $add); return $this->createFacet(self::FACET_PIVOT, $options, $add);
} }
/**
* Get a facet interval instance
*
* @param mixed $options
* @param bool $add
* @return \Solarium\QueryType\Select\Query\Component\Facet\Interval
*/
public function createFacetInterval($options = null, $add = true)
{
return $this->createFacet(self::FACET_INTERVAL, $options, $add);
}
} }
...@@ -46,6 +46,7 @@ use Solarium\QueryType\Select\Query\Component\Facet\MultiQuery as FacetMultiQuer ...@@ -46,6 +46,7 @@ use Solarium\QueryType\Select\Query\Component\Facet\MultiQuery as FacetMultiQuer
use Solarium\QueryType\Select\Query\Component\Facet\Query as FacetQuery; use Solarium\QueryType\Select\Query\Component\Facet\Query as FacetQuery;
use Solarium\QueryType\Select\Query\Component\Facet\Range as FacetRange; use Solarium\QueryType\Select\Query\Component\Facet\Range as FacetRange;
use Solarium\QueryType\Select\Query\Component\Facet\Pivot as FacetPivot; use Solarium\QueryType\Select\Query\Component\Facet\Pivot as FacetPivot;
use Solarium\QueryType\Select\Query\Component\Facet\Interval as FacetInterval;
use Solarium\Exception\UnexpectedValueException; use Solarium\Exception\UnexpectedValueException;
/** /**
...@@ -93,6 +94,9 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac ...@@ -93,6 +94,9 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac
case FacetsetComponent::FACET_PIVOT: case FacetsetComponent::FACET_PIVOT:
$this->addFacetPivot($request, $facet); $this->addFacetPivot($request, $facet);
break; break;
case FacetsetComponent::FACET_INTERVAL:
$this->addFacetInterval($request, $facet);
break;
default: default:
throw new UnexpectedValueException('Unknown facet type'); throw new UnexpectedValueException('Unknown facet type');
} }
...@@ -225,4 +229,32 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac ...@@ -225,4 +229,32 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac
); );
$request->addParam('facet.pivot.mincount', $facet->getMinCount(), true); $request->addParam('facet.pivot.mincount', $facet->getMinCount(), true);
} }
/**
* Add params for a interval facet to request
*
* @param Request $request
* @param FacetInterval $facet
* @return void
*/
public function addFacetInterval($request, $facet)
{
$field = $facet->getField();
$request->addParam(
'facet.interval',
$this->renderLocalParams(
$field
// key & ex not supported for interval
//,array('key' => $facet->getKey(), 'ex' => $facet->getExcludes())
)
);
foreach ($facet->getSet() as $key => $setValue) {
if(is_string($key)) {
$setValue = '{!key="'.$key.'"}'.$setValue;
}
$request->addParam("f.$field.facet.interval.set", $setValue);
}
}
} }
...@@ -51,6 +51,7 @@ use Solarium\QueryType\Select\Result\Facet\Query as ResultFacetQuery; ...@@ -51,6 +51,7 @@ use Solarium\QueryType\Select\Result\Facet\Query as ResultFacetQuery;
use Solarium\QueryType\Select\Result\Facet\MultiQuery as ResultFacetMultiQuery; use Solarium\QueryType\Select\Result\Facet\MultiQuery as ResultFacetMultiQuery;
use Solarium\QueryType\Select\Result\Facet\Range as ResultFacetRange; use Solarium\QueryType\Select\Result\Facet\Range as ResultFacetRange;
use Solarium\QueryType\Select\Result\Facet\Pivot\Pivot as ResultFacetPivot; use Solarium\QueryType\Select\Result\Facet\Pivot\Pivot as ResultFacetPivot;
use Solarium\QueryType\Select\Result\Facet\Interval as ResultFacetInterval;
use Solarium\Exception\RuntimeException; use Solarium\Exception\RuntimeException;
use Solarium\Core\Query\ResponseParser as ResponseParserAbstract; use Solarium\Core\Query\ResponseParser as ResponseParserAbstract;
...@@ -86,6 +87,9 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac ...@@ -86,6 +87,9 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
case 'facet_pivot': case 'facet_pivot':
$method = 'createFacetPivot'; $method = 'createFacetPivot';
break; break;
case 'facet_interval':
$method = 'createFacetInterval';
break;
default: default:
throw new RuntimeException('Unknown facet class identifier'); throw new RuntimeException('Unknown facet class identifier');
} }
...@@ -118,6 +122,9 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac ...@@ -118,6 +122,9 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
case QueryFacetSet::FACET_PIVOT: case QueryFacetSet::FACET_PIVOT:
$result = $this->facetPivot($query, $facet, $data); $result = $this->facetPivot($query, $facet, $data);
break; break;
case QueryFacetSet::FACET_INTERVAL:
$result = $this->facetInterval($query, $facet, $data);
break;
default: default:
throw new RuntimeException('Unknown facet type'); throw new RuntimeException('Unknown facet type');
} }
...@@ -237,6 +244,24 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac ...@@ -237,6 +244,24 @@ class FacetSet extends ResponseParserAbstract implements ComponentParserInterfac
return new ResultFacetRange($data['counts'], $before, $after, $between, $start, $end, $gap); return new ResultFacetRange($data['counts'], $before, $after, $between, $start, $end, $gap);
} }
/**
* Add a facet result for a interval facet
*
* @param Query $query
* @param QueryFacetInterval $facet
* @param array $data
* @return ResultFacetInterval|null
*/
protected function facetInterval($query, $facet, $data)
{
$key = $facet->getKey();
if (!isset($data['facet_counts']['facet_intervals'][$key])) {
return null;
}
return new ResultFacetInterval($data['facet_counts']['facet_intervals'][$key]);
}
/** /**
* Add a facet result for a range facet * Add a facet result for a range facet
* *
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\Select\Result\Facet;
/**
* Select interval facet result
*
* A interval facet will usually return a dataset of multiple rows, in each row a
* value and its count. You can access the values as an array using
* {@link getValues()} or iterate this object.
*/
class Interval extends Field
{
}
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