Skip to content

Commit

Permalink
added stats to pivots
Browse files Browse the repository at this point in the history
  • Loading branch information
wickedOne committed Mar 18, 2015
1 parent e993186 commit f59fa30
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ build
phpunit.xml
composer.phar
composer.lock
vendor
vendor
106 changes: 104 additions & 2 deletions library/Solarium/QueryType/Select/Query/Component/Facet/Pivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,29 @@ class Pivot extends Facet
*/
protected $fields = array();

/**
* Optional stats
*
* @var array
*/
protected $stats = array();

/**
* Initialize options
*
* @return void
*/
protected function init()
{
if (isset($this->options['fields'])) {
$this->addFields($this->options['fields']);
foreach ($this->options as $name => $value) {
switch ($name) {
case 'fields':
$this->addFields($value);
break;
case 'stats':
$this->setStats($value);
break;
}
}
}

Expand Down Expand Up @@ -184,4 +198,92 @@ public function setFields($fields)

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;
}
}
98 changes: 98 additions & 0 deletions library/Solarium/QueryType/Select/Query/Component/Stats/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Field extends Configurable
*/
protected $facets = array();

/**
* pivot facets for these stats
*
* @var array
*/
protected $pivots = array();

/**
* Initialize options
*
Expand All @@ -67,6 +74,9 @@ protected function init()
case 'facet':
$this->setFacets($value);
break;
case 'pivot':
$this->setPivots($value);
break;
}
}
}
Expand Down Expand Up @@ -179,4 +189,92 @@ public function setFacets($facets)

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,22 @@ public function addFacetRange($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(
'facet.pivot',
$this->renderLocalParams(
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ public function buildComponent($component, $request)

// add fields
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
foreach ($field->getFacets() as $facet) {
Expand Down
23 changes: 23 additions & 0 deletions library/Solarium/QueryType/Select/Result/Facet/Pivot/PivotItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
*/
namespace Solarium\QueryType\Select\Result\Facet\Pivot;

use Solarium\QueryType\Select\Result\Stats\Stats;

/**
* Select field pivot result
*
Expand Down Expand Up @@ -65,6 +67,13 @@ class PivotItem extends Pivot
*/
protected $count;

/**
* Field stats
*
* @var mixed
*/
protected $stats;

/**
* Constructor
*
Expand All @@ -81,6 +90,10 @@ public function __construct($data)
$this->pivot[] = new PivotItem($pivotData);
}
}

if (isset($data['stats'])) {
$this->stats = new Stats($data['stats']);
}
}

/**
Expand Down Expand Up @@ -112,4 +125,14 @@ public function getCount()
{
return $this->count;
}

/**
* Get stats
*
* @return Stats
*/
public function getStats()
{
return $this->stats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public function testGetType()
);
}

public function testSetMinCount()
{
$this->facet->setMinCount(5);

$this->assertEquals(5, $this->facet->getMinCount());
}

public function testAddField()
{
$expectedFields = $this->facet->getFields();
Expand Down Expand Up @@ -113,4 +120,51 @@ public function testSetFields()
$this->facet->setFields(array('field3', 'field4'));
$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());
}
}
Loading

0 comments on commit f59fa30

Please sign in to comment.