Commit f59fa30e authored by wickedOne's avatar wickedOne

added stats to pivots

parent e993186e
...@@ -2,4 +2,4 @@ build ...@@ -2,4 +2,4 @@ build
phpunit.xml phpunit.xml
composer.phar composer.phar
composer.lock composer.lock
vendor vendor
\ No newline at end of file
...@@ -54,6 +54,13 @@ class Pivot extends Facet ...@@ -54,6 +54,13 @@ class Pivot extends Facet
*/ */
protected $fields = array(); protected $fields = array();
/**
* Optional stats
*
* @var array
*/
protected $stats = array();
/** /**
* Initialize options * Initialize options
* *
...@@ -61,8 +68,15 @@ class Pivot extends Facet ...@@ -61,8 +68,15 @@ class Pivot extends Facet
*/ */
protected function init() protected function init()
{ {
if (isset($this->options['fields'])) { foreach ($this->options as $name => $value) {
$this->addFields($this->options['fields']); switch ($name) {
case 'fields':
$this->addFields($value);
break;
case 'stats':
$this->setStats($value);
break;
}
} }
} }
...@@ -184,4 +198,92 @@ class Pivot extends Facet ...@@ -184,4 +198,92 @@ class Pivot extends Facet
return $this; return $this;
} }
/**
* Add stat
*
* @param string $stat
* @return self Provides fluent interface
*/
public function addStat($stat)
{
$this->stats[$stat] = true;
return $this;
}
/**
* Specify multiple Stats
*
* @param string|array $stats can be an array or string with comma
* separated statnames
*
* @return self Provides fluent interface
*/
public function addStats($stats)
{
if (is_string($stats)) {
$stats = explode(',', $stats);
$stats = array_map('trim', $stats);
}
foreach ($stats as $stat) {
$this->addStat($stat);
}
return $this;
}
/**
* Remove a stat from the stats list
*
* @param string $stat
* @return self Provides fluent interface
*/
public function removeStat($stat)
{
if (isset($this->stats[$stat])) {
unset($this->stats[$stat]);
}
return $this;
}
/**
* Remove all stats from the stats list.
*
* @return self Provides fluent interface
*/
public function clearStats()
{
$this->stats = array();
return $this;
}
/**
* Get the list of stats
*
* @return array
*/
public function getStats()
{
return array_keys($this->stats);
}
/**
* Set multiple stats
*
* This overwrites any existing stats
*
* @param array $stats
* @return self Provides fluent interface
*/
public function setStats($stats)
{
$this->clearStats();
$this->addStats($stats);
return $this;
}
} }
...@@ -52,6 +52,13 @@ class Field extends Configurable ...@@ -52,6 +52,13 @@ class Field extends Configurable
*/ */
protected $facets = array(); protected $facets = array();
/**
* pivot facets for these stats
*
* @var array
*/
protected $pivots = array();
/** /**
* Initialize options * Initialize options
* *
...@@ -67,6 +74,9 @@ class Field extends Configurable ...@@ -67,6 +74,9 @@ class Field extends Configurable
case 'facet': case 'facet':
$this->setFacets($value); $this->setFacets($value);
break; break;
case 'pivot':
$this->setPivots($value);
break;
} }
} }
} }
...@@ -179,4 +189,92 @@ class Field extends Configurable ...@@ -179,4 +189,92 @@ class Field extends Configurable
return $this; return $this;
} }
/**
* Add pivot
*
* @param string $pivot
* @return self Provides fluent interface
*/
public function addPivot($pivot)
{
$this->pivots[$pivot] = true;
return $this;
}
/**
* Specify multiple Pivots
*
* @param string|array $pivots can be an array or string with comma
* separated facetnames
*
* @return self Provides fluent interface
*/
public function addPivots($pivots)
{
if (is_string($pivots)) {
$pivots = explode(',', $pivots);
$pivots = array_map('trim', $pivots);
}
foreach ($pivots as $facet) {
$this->addPivot($facet);
}
return $this;
}
/**
* Remove a pivot facet from the pivot list
*
* @param string $pivot
* @return self Provides fluent interface
*/
public function removePivot($pivot)
{
if (isset($this->pivots[$pivot])) {
unset($this->pivots[$pivot]);
}
return $this;
}
/**
* Remove all pivot facets from the pivot list.
*
* @return self Provides fluent interface
*/
public function clearPivots()
{
$this->pivots = array();
return $this;
}
/**
* Get the list of pivot facets
*
* @return array
*/
public function getPivots()
{
return array_keys($this->pivots);
}
/**
* Set multiple pivot facets
*
* This overwrites any existing pivots
*
* @param array $pivots
* @return self Provides fluent interface
*/
public function setPivots($pivots)
{
$this->clearPivots();
$this->addPivots($pivots);
return $this;
}
} }
...@@ -205,11 +205,22 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac ...@@ -205,11 +205,22 @@ class FacetSet extends RequestBuilder implements ComponentRequestBuilderInterfac
*/ */
public function addFacetPivot($request, $facet) public function addFacetPivot($request, $facet)
{ {
$stats = $facet->getStats();
if (count($stats) > 0) {
$key = array('stats' => implode('', $stats));
// when specifying stats, solr sets the field as key
$facet->setKey(implode(',', $facet->getFields()));
} else {
$key = array('key' => $facet->getKey());
}
$request->addParam( $request->addParam(
'facet.pivot', 'facet.pivot',
$this->renderLocalParams( $this->renderLocalParams(
implode(',', $facet->getFields()), implode(',', $facet->getFields()),
array('key' => $facet->getKey(), 'ex' => $facet->getExcludes()) array_merge($key, array('ex' => $facet->getExcludes()))
) )
); );
$request->addParam('facet.pivot.mincount', $facet->getMinCount(), true); $request->addParam('facet.pivot.mincount', $facet->getMinCount(), true);
......
...@@ -60,8 +60,10 @@ class Stats implements ComponentRequestBuilderInterface ...@@ -60,8 +60,10 @@ class Stats implements ComponentRequestBuilderInterface
// add fields // add fields
foreach ($component->getFields() as $field) { foreach ($component->getFields() as $field) {
$pivots = $field->getPivots();
$request->addParam('stats.field', $field->getKey()); $prefix = (count($pivots) > 0) ? '{!tag='.implode(',', $pivots).'}' : '';
$request->addParam('stats.field', $prefix . $field->getKey());
// add field specific facet stats // add field specific facet stats
foreach ($field->getFacets() as $facet) { foreach ($field->getFacets() as $facet) {
......
...@@ -38,6 +38,8 @@ ...@@ -38,6 +38,8 @@
*/ */
namespace Solarium\QueryType\Select\Result\Facet\Pivot; namespace Solarium\QueryType\Select\Result\Facet\Pivot;
use Solarium\QueryType\Select\Result\Stats\Stats;
/** /**
* Select field pivot result * Select field pivot result
* *
...@@ -65,6 +67,13 @@ class PivotItem extends Pivot ...@@ -65,6 +67,13 @@ class PivotItem extends Pivot
*/ */
protected $count; protected $count;
/**
* Field stats
*
* @var mixed
*/
protected $stats;
/** /**
* Constructor * Constructor
* *
...@@ -81,6 +90,10 @@ class PivotItem extends Pivot ...@@ -81,6 +90,10 @@ class PivotItem extends Pivot
$this->pivot[] = new PivotItem($pivotData); $this->pivot[] = new PivotItem($pivotData);
} }
} }
if (isset($data['stats'])) {
$this->stats = new Stats($data['stats']);
}
} }
/** /**
...@@ -112,4 +125,14 @@ class PivotItem extends Pivot ...@@ -112,4 +125,14 @@ class PivotItem extends Pivot
{ {
return $this->count; return $this->count;
} }
/**
* Get stats
*
* @return Stats
*/
public function getStats()
{
return $this->stats;
}
} }
...@@ -67,6 +67,13 @@ class PivotTest extends \PHPUnit_Framework_TestCase ...@@ -67,6 +67,13 @@ class PivotTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testSetMinCount()
{
$this->facet->setMinCount(5);
$this->assertEquals(5, $this->facet->getMinCount());
}
public function testAddField() public function testAddField()
{ {
$expectedFields = $this->facet->getFields(); $expectedFields = $this->facet->getFields();
...@@ -113,4 +120,51 @@ class PivotTest extends \PHPUnit_Framework_TestCase ...@@ -113,4 +120,51 @@ class PivotTest extends \PHPUnit_Framework_TestCase
$this->facet->setFields(array('field3', 'field4')); $this->facet->setFields(array('field3', 'field4'));
$this->assertEquals(array('field3', 'field4'), $this->facet->getFields()); $this->assertEquals(array('field3', 'field4'), $this->facet->getFields());
} }
public function testAddStat()
{
$expectedStats = $this->facet->getStats();
$expectedStats[] = 'newstat';
$this->facet->addStat('newstat');
$this->assertEquals($expectedStats, $this->facet->getStats());
}
public function testClearStats()
{
$this->facet->addStat('newstat');
$this->facet->clearStats();
$this->assertEquals(array(), $this->facet->getStats());
}
public function testAddStats()
{
$stats = array('stat1', 'stat2');
$this->facet->clearStats();
$this->facet->addStats($stats);
$this->assertEquals($stats, $this->facet->getStats());
}
public function testAddStatsAsStringWithTrim()
{
$this->facet->clearStats();
$this->facet->addStats('stat1, stat2');
$this->assertEquals(array('stat1', 'stat2'), $this->facet->getStats());
}
public function testRemoveStat()
{
$this->facet->clearStats();
$this->facet->addStats(array('stat1', 'stat2'));
$this->facet->removeStat('stat1');
$this->assertEquals(array('stat2'), $this->facet->getstats());
}
public function testSetStats()
{
$this->facet->clearStats();
$this->facet->addStats(array('stat1', 'stat2'));
$this->facet->setStats(array('stat3', 'stat4'));
$this->assertEquals(array('stat3', 'stat4'), $this->facet->getStats());
}
} }
...@@ -82,6 +82,13 @@ class RangeTest extends \PHPUnit_Framework_TestCase ...@@ -82,6 +82,13 @@ class RangeTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testSetMinCount()
{
$this->facet->setMinCount(5);
$this->assertEquals(5, $this->facet->getMinCount());
}
public function testSetAndGetField() public function testSetAndGetField()
{ {
$this->facet->setField('price'); $this->facet->setField('price');
......
...@@ -49,6 +49,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase ...@@ -49,6 +49,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase
{ {
$options = array( $options = array(
'facet' => 'field1, field2', 'facet' => 'field1, field2',
'pivot' => 'piv1'
); );
$this->field->setOptions($options); $this->field->setOptions($options);
...@@ -107,4 +108,51 @@ class FieldTest extends \PHPUnit_Framework_TestCase ...@@ -107,4 +108,51 @@ class FieldTest extends \PHPUnit_Framework_TestCase
$this->field->setFacets(array('facet3', 'facet4')); $this->field->setFacets(array('facet3', 'facet4'));
$this->assertEquals(array('facet3', 'facet4'), $this->field->getFacets()); $this->assertEquals(array('facet3', 'facet4'), $this->field->getFacets());
} }
public function testAddPivot()
{
$expectedPivots = $this->field->getPivots();
$expectedPivots[] = 'newpivot';
$this->field->addPivot('newpivot');
$this->assertEquals($expectedPivots, $this->field->getPivots());
}
public function testClearPivots()
{
$this->field->addPivot('newpivot');
$this->field->clearPivots();
$this->assertEquals(array(), $this->field->getPivots());
}
public function testAddPivots()
{
$pivots = array('pivot1', 'pivot2');
$this->field->clearPivots();
$this->field->addPivots($pivots);
$this->assertEquals($pivots, $this->field->getPivots());
}
public function testAddPivotsAsStringWithTrim()
{
$this->field->clearPivots();
$this->field->addPivots('pivot1, pivot2');
$this->assertEquals(array('pivot1', 'pivot2'), $this->field->getPivots());
}
public function testRemovePivot()
{
$this->field->clearPivots();
$this->field->addPivots(array('pivot1', 'pivot2'));
$this->field->removePivot('pivot1');
$this->assertEquals(array('pivot2'), $this->field->getPivots());
}
public function testSetPivots()
{
$this->field->clearPivots();
$this->field->addPivots(array('pivot1', 'pivot2'));
$this->field->setPivots(array('pivot3', 'pivot4'));
$this->assertEquals(array('pivot3', 'pivot4'), $this->field->getPivots());
}
} }
...@@ -208,6 +208,30 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -208,6 +208,30 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
urldecode($request->getUri()) urldecode($request->getUri())
); );
} }
public function testBuildWithPivotStatFacet()
{
$facet = new FacetPivot(
array(
'key' => 'f1',
'fields' => 'cat,inStock',
'stats' => 'piv1'
)
);
$this->component->addFacet($facet);
$request = $this->builder->buildComponent($this->component, $this->request);
$this->assertEquals(
null,
$request->getRawData()
);
$this->assertEquals(
'?facet=true&facet.pivot={!stats=piv1}cat,inStock',
urldecode($request->getUri())
);
}
} }
class UnknownFacet extends FacetField class UnknownFacet extends FacetField
......
...@@ -201,6 +201,10 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -201,6 +201,10 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
'pivot' => array( 'pivot' => array(
array('field' => 'price', 'value' => 1, 'count' => 12), array('field' => 'price', 'value' => 1, 'count' => 12),
array('field' => 'price', 'value' => 2, 'count' => 8), array('field' => 'price', 'value' => 2, 'count' => 8),
),
'stats' => array(
'min' => 4,
'max' => 6,
) )
) )
), ),
...@@ -262,6 +266,13 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase ...@@ -262,6 +266,13 @@ class FacetSetTest extends \PHPUnit_Framework_TestCase
count($facets['cat,price']) count($facets['cat,price'])
); );
$pivots = $facets['cat,price']->getPivot();
$this->assertEquals(
2,
count($pivots[0]->getStats())
);
$this->query = new Query; $this->query = new Query;
} }
......
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