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

Merge branch 'feature/statscomponent' into develop

parents 69604f3f 420b16a6
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
$query->setRows(0);
// add stats settings
$stats = $query->getStats();
$stats->addFacet('inStock');
$stats->createField('popularity');
$stats->createField('price')->addFacet('price')->addFacet('popularity');
// this executes the query and returns the result
$resultset = $client->select($query);
$statsResult = $resultset->getStats();
// display the stats results
foreach ($statsResult as $field) {
echo '<h1>' . $field->getName() . '</h1>';
echo 'Min: ' . $field->getMin() . '<br/>';
echo 'Max: ' . $field->getMax() . '<br/>';
echo 'Sum: ' . $field->getSum() . '<br/>';
echo 'Count: ' . $field->getCount() . '<br/>';
echo 'Missing: ' . $field->getMissing() . '<br/>';
echo 'SumOfSquares: ' . $field->getSumOfSquares() . '<br/>';
echo 'Mean: ' . $field->getMean() . '<br/>';
echo 'Stddev: ' . $field->getStddev() . '<br/>';
echo '<h2>Field facets</h2>';
foreach ($field->getFacets() as $field => $facet) {
echo '<h3>Facet ' . $field . '</h3>';
foreach ($facet AS $facetStats) {
echo '<h4>Value: ' . $facetStats->getValue() . '</h4>';
echo 'Min: ' . $facetStats->getMin() . '<br/>';
echo 'Max: ' . $facetStats->getMax() . '<br/>';
echo 'Sum: ' . $facetStats->getSum() . '<br/>';
echo 'Count: ' . $facetStats->getCount() . '<br/>';
echo 'Missing: ' . $facetStats->getMissing() . '<br/>';
echo 'SumOfSquares: ' . $facetStats->getSumOfSquares() . '<br/>';
echo 'Mean: ' . $facetStats->getMean() . '<br/>';
echo 'Stddev: ' . $facetStats->getStddev() . '<br/>';
}
}
echo '<hr/>';
}
htmlFooter();
\ No newline at end of file
...@@ -9,7 +9,7 @@ htmlHeader(); ...@@ -9,7 +9,7 @@ htmlHeader();
// create a client instance // create a client instance
$client = new Solarium_Client($config); $client = new Solarium_Client($config);
// set the adapter to peclhttp // set the adapter to curl
$client->setAdapter('Solarium_Client_Adapter_Curl'); $client->setAdapter('Solarium_Client_Adapter_Curl');
// get a select query instance // get a select query instance
......
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
<li><a href="2.1.5.7-grouping-by-query.php">2.1.5.7 Grouping by query</a></li> <li><a href="2.1.5.7-grouping-by-query.php">2.1.5.7 Grouping by query</a></li>
<li><a href="2.1.5.8-distributed-search.php">2.1.5.8 Distributed search (sharding)</a></li> <li><a href="2.1.5.8-distributed-search.php">2.1.5.8 Distributed search (sharding)</a></li>
<li><a href="2.1.5.9-spellcheck.php">2.1.5.9 Spellcheck</a></li> <li><a href="2.1.5.9-spellcheck.php">2.1.5.9 Spellcheck</a></li>
<li><a href="2.1.5.10-stats.php">2.1.5.10 Stats</a></li>
</ul> </ul>
<li><a href="2.1.6-helper-functions.php">2.1.6 Helper functions</a></li> <li><a href="2.1.6-helper-functions.php">2.1.6 Helper functions</a></li>
<li><a href="2.1.7-query-reuse.php">2.1.7 Query re-use</a></li> <li><a href="2.1.7-query-reuse.php">2.1.7 Query re-use</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/
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component stats to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_Stats
{
/**
* Add request settings for the stats component
*
* @param Solarium_Query_Select_Component_Stats $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable stats
$request->addParam('stats', 'true');
// add fields
foreach ($component->getFields() as $field) {
$request->addParam('stats.field', $field->getKey());
// add field specific facet stats
foreach ($field->getFacets() as $facet) {
$request->addParam('f.'.$field->getKey().'.stats.facet', $facet);
}
}
// add facet stats for all fields
foreach ($component->getFacets() as $facet) {
$request->addParam('stats.facet', $facet);
}
return $request;
}
}
\ No newline at end of file
<?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/
*
* @package Solarium
* @subpackage Client
*/
/**
* Parse select component Stats result from the data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select_Component_Stats
{
/**
* Parse result data into result objects
*
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_Stats $stats
* @param array $data
* @return Solarium_Result_Select_Stats
*/
public function parse($query, $stats, $data)
{
$results = array();
if (isset($data['stats']['stats_fields'])) {
$statResults = $data['stats']['stats_fields'];
foreach ($statResults AS $field => $stats) {
if (isset($stats['facets'])) {
foreach($stats['facets'] as $facetField => $values) {
foreach ($values as $value => $valueStats) {
$stats['facets'][$facetField][$value] = new Solarium_Result_Select_Stats_FacetValue($value, $valueStats);
}
}
}
$results[$field] = new Solarium_Result_Select_Stats_Result($field, $stats);
}
}
return new Solarium_Result_Select_Stats($results);
}
}
\ No newline at end of file
...@@ -91,7 +91,9 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract ...@@ -91,7 +91,9 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract
public function postCreateRequest($query, $request) public function postCreateRequest($query, $request)
{ {
$queryString = $request->getQueryString(); $queryString = $request->getQueryString();
if (strlen($queryString) > $this->getMaxQueryStringLength()) { if ($request->getMethod() == Solarium_Client_Request::METHOD_GET &&
strlen($queryString) > $this->getMaxQueryStringLength()) {
$request->setMethod(Solarium_Client_Request::METHOD_POST); $request->setMethod(Solarium_Client_Request::METHOD_POST);
$request->setRawData($queryString); $request->setRawData($queryString);
$request->clearParams(); $request->clearParams();
......
...@@ -94,6 +94,11 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -94,6 +94,11 @@ class Solarium_Query_Select extends Solarium_Query
*/ */
const COMPONENT_DISTRIBUTEDSEARCH = 'distributedsearch'; const COMPONENT_DISTRIBUTEDSEARCH = 'distributedsearch';
/**
* Query component stats
*/
const COMPONENT_STATS = 'stats';
/** /**
* Get type for this query * Get type for this query
* *
...@@ -160,6 +165,11 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -160,6 +165,11 @@ class Solarium_Query_Select extends Solarium_Query
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_DistributedSearch', 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_DistributedSearch',
'responseparser' => null, 'responseparser' => null,
), ),
self::COMPONENT_STATS => array(
'component' => 'Solarium_Query_Select_Component_Stats',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Stats',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Stats',
),
); );
/** /**
...@@ -877,4 +887,16 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -877,4 +887,16 @@ class Solarium_Query_Select extends Solarium_Query
return $this->getComponent(Solarium_Query_Select::COMPONENT_DISTRIBUTEDSEARCH, true); return $this->getComponent(Solarium_Query_Select::COMPONENT_DISTRIBUTEDSEARCH, true);
} }
/**
* Get a Stats component instance
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Query_Select_Component_Stats
*/
public function getStats()
{
return $this->getComponent(Solarium_Query_Select::COMPONENT_STATS, true);
}
} }
<?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/
*
* @package Solarium
* @subpackage Query
*/
/**
* Stats component
*
* @link http://wiki.apache.org/solr/StatsComponent
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Select_Component_Stats extends Solarium_Query_Select_Component
{
/**
* Component type
*
* @var string
*/
protected $_type = Solarium_Query_Select::COMPONENT_STATS;
/**
* Stats facets for all fields
*
* @var array
*/
protected $_facets = array();
/**
* Fields
*
* @var array
*/
protected $_fields = array();
/**
* Initialize options
*
* Several options need some extra checks or setup work, for these options
* the setters are called.
*
* @return void
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'field':
$this->setFields($value);
break;
case 'facet':
$this->setFacets($value);
break;
}
}
}
/**
* Create a field instance
*
* If you supply a string as the first arguments ($options) it will be used as the key for the field
* and it will be added to this query component.
* If you supply an options array/object that contains a key the field will also be added to the component.
*
* When no key is supplied the field cannot be added, in that case you will need to add it manually
* after setting the key, by using the addField method.
*
* @param mixed $options
* @return Solarium_Query_Select_Component_Stats_Field
*/
public function createField($options = null)
{
if (is_string($options)) {
$fq = new Solarium_Query_Select_Component_Stats_Field;
$fq->setKey($options);
} else {
$fq = new Solarium_Query_Select_Component_Stats_Field($options);
}
if ($fq->getKey() !== null) {
$this->addField($fq);
}
return $fq;
}
/**
* Add a field
*
* Supports a field instance or a config array, in that case a new
* field instance wil be created based on the options.
*
* @param Solarium_Query_Select_Component_Stats_Field|array $field
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function addField($field)
{
if (is_array($field)) {
$field = new Solarium_Query_Select_Component_Stats_Field($field);
}
$key = $field->getKey();
if (0 === strlen($key)) {
throw new Solarium_Exception('A field must have a key value');
}
if (array_key_exists($key, $this->_fields)) {
if ($this->_fields[$key] === $field) {
//double add calls for the same FQ are ignored
//@todo add trigger_error with a notice?
} else {
throw new Solarium_Exception('A field must have a unique key value');
}
} else {
$this->_fields[$key] = $field;
}
return $this;
}
/**
* Add multiple fields
*
* @param array $fields
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function addFields(array $fields)
{
foreach ($fields AS $key => $field) {
// in case of a config array: add key to config
if (is_array($field) && !isset($field['key'])) {
$field['key'] = $key;
}
$this->addField($field);
}
return $this;
}
/**
* Get a field
*
* @param string $key
* @return string
*/
public function getField($key)
{
if (isset($this->_fields[$key])) {
return $this->_fields[$key];
} else {
return null;
}
}
/**
* Get all fields
*
* @return array
*/
public function getFields()
{
return $this->_fields;
}
/**
* Remove a single field
*
* You can remove a field by passing it's key, or by passing the field instance
*
* @param string|Solarium_Query_Select_Component_Stats_Field $field
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function removeField($field)
{
if (is_object($field)) {
$field = $field->getKey();
}
if (isset($this->_fields[$field])) {
unset($this->_fields[$field]);
}
return $this;
}
/**
* Remove all fields
*
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function clearFields()
{
$this->_fields = array();
return $this;
}
/**
* Set multiple fields
*
* This overwrites any existing fields
*
* @param array $fields
*/
public function setFields($fields)
{
$this->clearFields();
$this->addFields($fields);
}
/**
* Specify a facet to return in the resultset
*
* @param string $facet
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function addFacet($facet)
{
$this->_facets[$facet] = true;
return $this;
}
/**
* Specify multiple facets to return in the resultset
*
* @param string|array $facets can be an array or string with comma
* separated facetnames
*
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function addFacets($facets)
{
if (is_string($facets)) {
$facets = explode(',', $facets);
$facets = array_map('trim', $facets);
}
foreach ($facets AS $facet) {
$this->addFacet($facet);
}
return $this;
}
/**
* Remove a facet from the facet list
*
* @param string $facet
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function removeFacet($facet)
{
if (isset($this->_facets[$facet])) {
unset($this->_facets[$facet]);
}
return $this;
}
/**
* Remove all facets from the facet list.
*
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function clearFacets()
{
$this->_facets = array();
return $this;
}
/**
* Get the list of facets
*
* @return array
*/
public function getFacets()
{
return array_keys($this->_facets);
}
/**
* Set multiple facets
*
* This overwrites any existing facets
*
* @param array $facets
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function setFacets($facets)
{
$this->clearFacets();
$this->addFacets($facets);
return $this;
}
}
\ No newline at end of file
<?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/
*
* @package Solarium
* @subpackage Query
*
* TODO
* Voorbeeld request:
* http://localhost:8983/solr/select?q=*:*&stats=true&stats.field=price&stats.field=popularity
* &stats.twopass=true&rows=0&indent=true&stats.facet=inStock&f.price.stats.facet=price
* &f.price.stats.facet=popularity
*/
/**
* Stats component field class
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Select_Component_Stats_Field extends Solarium_Configurable
{
/**
* Field facets (for stats)
*
* @var array
*/
protected $_facets = array();
/**
* Initialize options
*
* Several options need some extra checks or setup work, for these options
* the setters are called.
*
* @return void
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'facet':
$this->setFacets($value);
break;
}
}
}
/**
* Get key value
*
* @return string
*/
public function getKey()
{
return $this->getOption('key');
}
/**
* Set key value
*
* @param string $value
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function setKey($value)
{
return $this->_setOption('key', $value);
}
/**
* Specify a facet to return in the resultset
*
* @param string $facet
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function addFacet($facet)
{
$this->_facets[$facet] = true;
return $this;
}
/**
* Specify multiple facets to return in the resultset
*
* @param string|array $facets can be an array or string with comma
* separated facetnames
*
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function addFacets($facets)
{
if (is_string($facets)) {
$facets = explode(',', $facets);
$facets = array_map('trim', $facets);
}
foreach ($facets AS $facet) {
$this->addFacet($facet);
}
return $this;
}
/**
* Remove a facet from the facet list
*
* @param string $facet
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function removeFacet($facet)
{
if (isset($this->_facets[$facet])) {
unset($this->_facets[$facet]);
}
return $this;
}
/**
* Remove all facets from the facet list.
*
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function clearFacets()
{
$this->_facets = array();
return $this;
}
/**
* Get the list of facets
*
* @return array
*/
public function getFacets()
{
return array_keys($this->_facets);
}
/**
* Set multiple facets
*
* This overwrites any existing facets
*
* @param array $facets
* @return Solarium_Query_Select_Component_Stats Provides fluent interface
*/
public function setFacets($facets)
{
$this->clearFacets();
$this->addFacets($facets);
return $this;
}
}
\ No newline at end of file
...@@ -269,4 +269,16 @@ class Solarium_Result_Select extends Solarium_Result_QueryType ...@@ -269,4 +269,16 @@ class Solarium_Result_Select extends Solarium_Result_QueryType
{ {
return $this->getComponent(Solarium_Query_Select::COMPONENT_SPELLCHECK); return $this->getComponent(Solarium_Query_Select::COMPONENT_SPELLCHECK);
} }
/**
* Get stats component result
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Result_Select_Stats
*/
public function getStats()
{
return $this->getComponent(Solarium_Query_Select::COMPONENT_STATS);
}
} }
\ No newline at end of file
<?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/
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component stats result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Stats
implements IteratorAggregate, Countable
{
/**
* Result array
*
* @var array
*/
protected $_results;
/**
* Constructor
*
* @param array $results
* @return void
*/
public function __construct($results)
{
$this->_results = $results;
}
/**
* Get a result by key
*
* @param mixed $key
* @return Solarium_Result_Select_Stats_Result|null
*/
public function getResult($key)
{
if (isset($this->_results[$key])) {
return $this->_results[$key];
} else {
return null;
}
}
/**
* Get all results
*
* @return array
*/
public function getResults()
{
return $this->_results;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_results);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_results);
}
}
<?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/
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component stats facet value
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Stats_FacetValue
{
/**
* Facet value
*
* @var string
*/
protected $_value;
/**
* Stats data
*
* @var array
*/
protected $_stats;
/**
* Constructor
*
* @param string $value
* @param array $stats
* @return void
*/
public function __construct($value, $stats)
{
$this->_value = $value;
$this->_stats = $stats;
}
/**
* Get facet value
*
* @return string
*/
public function getValue()
{
return $this->_value;
}
/**
* Get min value
*
* @return string
*/
public function getMin()
{
return $this->_stats['min'];
}
/**
* Get max value
*
* @return string
*/
public function getMax()
{
return $this->_stats['max'];
}
/**
* Get sum value
*
* @return string
*/
public function getSum()
{
return $this->_stats['sum'];
}
/**
* Get count value
*
* @return string
*/
public function getCount()
{
return $this->_stats['count'];
}
/**
* Get missing value
*
* @return string
*/
public function getMissing()
{
return $this->_stats['missing'];
}
/**
* Get sumOfSquares value
*
* @return string
*/
public function getSumOfSquares()
{
return $this->_stats['sumOfSquares'];
}
/**
* Get mean value
*
* @return string
*/
public function getMean()
{
return $this->_stats['mean'];
}
/**
* Get stddev value
*
* @return string
*/
public function getStddev()
{
return $this->_stats['stddev'];
}
/**
* Get facet stats
*
* @return array
*/
public function getFacets()
{
return $this->_stats['facets'];
}
}
<?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/
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component stats field result item
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Stats_Result
{
/**
* Field name
*
* @var string
*/
protected $_field;
/**
* Stats data
*
* @var array
*/
protected $_stats;
/**
* Constructor
*
* @param string $field
* @param array $stats
* @return void
*/
public function __construct($field, $stats)
{
$this->_field = $field;
$this->_stats = $stats;
}
/**
* Get field name
*
* @return string
*/
public function getName()
{
return $this->_field;
}
/**
* Get min value
*
* @return string
*/
public function getMin()
{
return $this->_stats['min'];
}
/**
* Get max value
*
* @return string
*/
public function getMax()
{
return $this->_stats['max'];
}
/**
* Get sum value
*
* @return string
*/
public function getSum()
{
return $this->_stats['sum'];
}
/**
* Get count value
*
* @return string
*/
public function getCount()
{
return $this->_stats['count'];
}
/**
* Get missing value
*
* @return string
*/
public function getMissing()
{
return $this->_stats['missing'];
}
/**
* Get sumOfSquares value
*
* @return string
*/
public function getSumOfSquares()
{
return $this->_stats['sumOfSquares'];
}
/**
* Get mean value
*
* @return string
*/
public function getMean()
{
return $this->_stats['mean'];
}
/**
* Get stddev value
*
* @return string
*/
public function getStddev()
{
return $this->_stats['stddev'];
}
/**
* Get facet stats
*
* @return array
*/
public function getFacets()
{
return $this->_stats['facets'];
}
}
<?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.
*/
class Solarium_Client_RequestBuilder_Select_Component_StatsTest extends PHPUnit_Framework_TestCase
{
public function testBuild()
{
$builder = new Solarium_Client_RequestBuilder_Select_Component_Stats();
$request = new Solarium_Client_Request();
$component = new Solarium_Query_Select_Component_Stats();
$component->createField('fieldA')->addFacet('fieldFacetA');
$component->createField('fieldB');
$component->addFacets(array('facetA', 'facetB'));
$request = $builder->build($component, $request);
$this->assertEquals(
array(
'stats' => true,
'stats.facet' => array(
'facetA',
'facetB',
),
'stats.field' => array(
'fieldA',
'fieldB',
),
'f.fieldA.stats.facet' => 'fieldFacetA',
),
$request->getParams()
);
}
}
\ No newline at end of file
<?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.
*/
class Solarium_Client_ResponseParser_Select_Component_StatsTest extends PHPUnit_Framework_TestCase
{
protected $_parser;
public function setUp()
{
$this->_parser = new Solarium_Client_ResponseParser_Select_Component_Stats();
}
public function testParse()
{
$data = array(
'stats' => array(
'stats_fields' => array(
'fieldA' => array(
'min' => 3,
),
'fieldB' => array(
'min' => 4,
'facets' => array(
'fieldC' => array(
'value1' => array(
'min' => 5,
)
)
)
)
)
)
);
$result = $this->_parser->parse(null, null, $data);
$this->assertEquals(3, $result->getResult('fieldA')->getMin());
$this->assertEquals(4, $result->getResult('fieldB')->getMin());
$facets = $result->getResult('fieldB')->getFacets();
$this->assertEquals(5, $facets['fieldC']['value1']->getMin());
}
public function testParseNoData()
{
$result = $this->_parser->parse(null, null, array());
$this->assertEquals(0, count($result));
}
}
...@@ -90,4 +90,16 @@ class Solarium_Plugin_PostBigRequestTest extends PHPUnit_Framework_TestCase ...@@ -90,4 +90,16 @@ class Solarium_Plugin_PostBigRequestTest extends PHPUnit_Framework_TestCase
$this->assertEquals($requestInput, $requestOutput); $this->assertEquals($requestInput, $requestOutput);
} }
public function testPostCreateRequestUnalteredPostRequest()
{
$query = $this->_client->createUpdate();
$query->addDeleteById(1);
$requestOutput = $this->_client->createRequest($query);
$requestInput = clone $requestOutput;
$this->_plugin->postCreateRequest($query, $requestOutput);
$this->assertEquals($requestInput, $requestOutput);
}
} }
\ No newline at end of file
<?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.
*/
class Solarium_Query_Select_Component_Stats_FieldTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Select_Component_Stats_Field
*/
protected $_field;
public function setUp()
{
$this->_field = new Solarium_Query_Select_Component_Stats_Field;
}
public function testConfigMode()
{
$options = array(
'facet' => 'field1, field2',
);
$this->_field->setOptions($options);
$this->assertEquals(array('field1','field2'), $this->_field->getFacets());
}
public function testSetAndGetKey()
{
$this->_field->setKey('testkey');
$this->assertEquals('testkey', $this->_field->getKey());
}
public function testAddFacet()
{
$expectedFacets = $this->_field->getFacets();
$expectedFacets[] = 'newfacet';
$this->_field->addFacet('newfacet');
$this->assertEquals($expectedFacets, $this->_field->getFacets());
}
public function testClearFacets()
{
$this->_field->addFacet('newfacet');
$this->_field->clearFacets();
$this->assertEquals(array(), $this->_field->getFacets());
}
public function testAddFacets()
{
$facets = array('facet1','facet2');
$this->_field->clearFacets();
$this->_field->addFacets($facets);
$this->assertEquals($facets, $this->_field->getFacets());
}
public function testAddFacetsAsStringWithTrim()
{
$this->_field->clearFacets();
$this->_field->addFacets('facet1, facet2');
$this->assertEquals(array('facet1','facet2'), $this->_field->getFacets());
}
public function testRemoveFacet()
{
$this->_field->clearFacets();
$this->_field->addFacets(array('facet1','facet2'));
$this->_field->removeFacet('facet1');
$this->assertEquals(array('facet2'), $this->_field->getFacets());
}
public function testSetFacets()
{
$this->_field->clearFacets();
$this->_field->addFacets(array('facet1','facet2'));
$this->_field->setFacets(array('facet3','facet4'));
$this->assertEquals(array('facet3','facet4'), $this->_field->getFacets());
}
}
<?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.
*/
class Solarium_Query_Select_Component_StatsTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Select_Component_Stats
*/
protected $_stats;
public function setUp()
{
$this->_stats = new Solarium_Query_Select_Component_Stats;
}
public function testGetType()
{
$this->assertEquals(Solarium_Query_Select::COMPONENT_STATS, $this->_stats->getType());
}
public function testConfigMode()
{
$options = array(
'facet' => 'field1, field2',
'field' => array(
'f1' => array(),
'f2' => array(),
)
);
$this->_stats->setOptions($options);
$this->assertEquals(array('field1','field2'), $this->_stats->getFacets());
$this->assertEquals(array('f1','f2'), array_keys($this->_stats->getFields()));
}
public function testCreateFieldWithKey()
{
$field = $this->_stats->createField('mykey');
// check class
$this->assertThat($field, $this->isInstanceOf('Solarium_Query_Select_Component_Stats_Field'));
$this->assertEquals(
'mykey',
$field->getKey()
);
}
public function testCreateFieldWithOptions()
{
$options = array('key' => 'testkey');
$field = $this->_stats->createField($options);
// check class
$this->assertThat($field, $this->isInstanceOf('Solarium_Query_Select_Component_Stats_Field'));
// check option forwarding
$fieldOptions = $field->getOptions();
$this->assertEquals(
$options['key'],
$field->getKey()
);
}
public function testAddAndGetField()
{
$field = new Solarium_Query_Select_Component_Stats_Field;
$field->setKey('f1');
$this->_stats->addField($field);
$this->assertEquals(
$field,
$this->_stats->getField('f1')
);
}
public function testAddFieldWithOptions()
{
$this->_stats->addField(array('key' => 'f1'));
$this->assertEquals(
'f1',
$this->_stats->getField('f1')->getKey()
);
}
public function testAddAndGetFieldWithKey()
{
$key = 'f1';
$fld = $this->_stats->createField($key, true);
$this->assertEquals(
$key,
$fld->getKey()
);
$this->assertEquals(
$fld,
$this->_stats->getField('f1')
);
}
public function testAddFieldWithoutKey()
{
$fld = new Solarium_Query_Select_Component_Stats_Field;
$this->setExpectedException('Solarium_Exception');
$this->_stats->addField($fld);
}
public function testAddFieldWithUsedKey()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f1');
$this->_stats->addField($f1);
$this->setExpectedException('Solarium_Exception');
$this->_stats->addField($f2);
}
public function testGetInvalidField()
{
$this->assertEquals(
null,
$this->_stats->getField('invalidkey')
);
}
public function testAddFields()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f2');
$fields = array('f1' => $f1, 'f2' => $f2);
$this->_stats->addFields($fields);
$this->assertEquals(
$fields,
$this->_stats->getFields()
);
}
public function testAddFieldsWithOptions()
{
$fields = array(
'f1' => array(''),
array('key' => 'f2')
);
$this->_stats->addFields($fields);
$fields = $this->_stats->getFields();
$this->assertEquals( array('f1', 'f2'), array_keys($fields));
}
public function testRemoveField()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f2');
$fields = array('f1' => $f1, 'f2' => $f2);
$this->_stats->addFields($fields);
$this->_stats->removeField('f1');
$this->assertEquals(
array('f2' => $f2),
$this->_stats->getFields()
);
}
public function testRemoveFieldWithObjectInput()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f2');
$fields = array($f1, $f2);
$this->_stats->addFields($fields);
$this->_stats->removeField($f1);
$this->assertEquals(
array('f2' => $f2),
$this->_stats->getFields()
);
}
public function testRemoveInvalidField()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f2');
$fields = array('f1' => $f1, 'f2' => $f2);
$this->_stats->addFields($fields);
$this->_stats->removeField('f3'); //continue silently
$this->assertEquals(
$fields,
$this->_stats->getFields()
);
}
public function testClearFields()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f2');
$fields = array($f1, $f2);
$this->_stats->addFields($fields);
$this->_stats->clearFields();
$this->assertEquals(
array(),
$this->_stats->getFields()
);
}
public function testSetFields()
{
$f1 = new Solarium_Query_Select_Component_Stats_Field;
$f1->setKey('f1');
$f2 = new Solarium_Query_Select_Component_Stats_Field;
$f2->setKey('f2');
$fields = array($f1, $f2);
$this->_stats->addFields($fields);
$f3 = new Solarium_Query_Select_Component_Stats_Field;
$f3->setKey('f3');
$f4 = new Solarium_Query_Select_Component_Stats_Field;
$f4->setKey('f4');
$fields2 = array('f3' => $f3, 'f4' => $f4);
$this->_stats->setFields($fields2);
$this->assertEquals(
$fields2,
$this->_stats->getFields()
);
}
public function testAddFacet()
{
$expectedFacets = $this->_stats->getFacets();
$expectedFacets[] = 'newfacet';
$this->_stats->addFacet('newfacet');
$this->assertEquals($expectedFacets, $this->_stats->getFacets());
}
public function testClearFacets()
{
$this->_stats->addFacet('newfacet');
$this->_stats->clearFacets();
$this->assertEquals(array(), $this->_stats->getFacets());
}
public function testAddFacets()
{
$facets = array('facet1','facet2');
$this->_stats->clearFacets();
$this->_stats->addFacets($facets);
$this->assertEquals($facets, $this->_stats->getFacets());
}
public function testAddFacetsAsStringWithTrim()
{
$this->_stats->clearFacets();
$this->_stats->addFacets('facet1, facet2');
$this->assertEquals(array('facet1','facet2'), $this->_stats->getFacets());
}
public function testRemoveFacet()
{
$this->_stats->clearFacets();
$this->_stats->addFacets(array('facet1','facet2'));
$this->_stats->removeFacet('facet1');
$this->assertEquals(array('facet2'), $this->_stats->getFacets());
}
public function testSetFacets()
{
$this->_stats->clearFacets();
$this->_stats->addFacets(array('facet1','facet2'));
$this->_stats->setFacets(array('facet3','facet4'));
$this->assertEquals(array('facet3','facet4'), $this->_stats->getFacets());
}
}
...@@ -601,4 +601,14 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase ...@@ -601,4 +601,14 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
get_class($spellcheck) get_class($spellcheck)
); );
} }
public function testGetStats()
{
$stats = $this->_query->getStats();
$this->assertEquals(
'Solarium_Query_Select_Component_Stats',
get_class($stats)
);
}
} }
<?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.
*/
class Solarium_Result_Select_Stats_FacetValueTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Stats_FacetValue
*/
protected $_result;
protected $_value, $_stats;
public function setUp()
{
$this->_value = 'myvalue';
$this->_stats = array(
'min' => 'dummyMin',
'max' => 'dummyMax',
'sum' => 'dummySum',
'count' => 'dummyCount',
'missing' => 'dummyMissing',
'sumOfSquares' => 'dummySos',
'mean' => 'dummyMean',
'stddev' => 'dummyStddev',
'facets' => 'dummyFacets',
);
$this->_result = new Solarium_Result_Select_Stats_FacetValue($this->_value, $this->_stats);
}
public function testGetValue()
{
$this->assertEquals($this->_value, $this->_result->getValue());
}
public function testGetMin()
{
$this->assertEquals($this->_stats['min'], $this->_result->getMin());
}
public function testGetMax()
{
$this->assertEquals($this->_stats['max'], $this->_result->getMax());
}
public function testGetSum()
{
$this->assertEquals($this->_stats['sum'], $this->_result->getSum());
}
public function testGetCount()
{
$this->assertEquals($this->_stats['count'], $this->_result->getCount());
}
public function testGetMissing()
{
$this->assertEquals($this->_stats['missing'], $this->_result->getMissing());
}
public function testGetSumOfSquares()
{
$this->assertEquals($this->_stats['sumOfSquares'], $this->_result->getSumOfSquares());
}
public function testGetMean()
{
$this->assertEquals($this->_stats['mean'], $this->_result->getMean());
}
public function testGetStddev()
{
$this->assertEquals($this->_stats['stddev'], $this->_result->getStddev());
}
public function testGetFacets()
{
$this->assertEquals($this->_stats['facets'], $this->_result->getFacets());
}
}
<?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.
*/
class Solarium_Result_Select_Stats_ResultTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Stats_Result
*/
protected $_result;
protected $_field, $_stats;
public function setUp()
{
$this->_field = 'myfield';
$this->_stats = array(
'min' => 'dummyMin',
'max' => 'dummyMax',
'sum' => 'dummySum',
'count' => 'dummyCount',
'missing' => 'dummyMissing',
'sumOfSquares' => 'dummySos',
'mean' => 'dummyMean',
'stddev' => 'dummyStddev',
'facets' => 'dummyFacets',
);
$this->_result = new Solarium_Result_Select_Stats_Result($this->_field, $this->_stats);
}
public function testGetName()
{
$this->assertEquals($this->_field, $this->_result->getName());
}
public function testGetMin()
{
$this->assertEquals($this->_stats['min'], $this->_result->getMin());
}
public function testGetMax()
{
$this->assertEquals($this->_stats['max'], $this->_result->getMax());
}
public function testGetSum()
{
$this->assertEquals($this->_stats['sum'], $this->_result->getSum());
}
public function testGetCount()
{
$this->assertEquals($this->_stats['count'], $this->_result->getCount());
}
public function testGetMissing()
{
$this->assertEquals($this->_stats['missing'], $this->_result->getMissing());
}
public function testGetSumOfSquares()
{
$this->assertEquals($this->_stats['sumOfSquares'], $this->_result->getSumOfSquares());
}
public function testGetMean()
{
$this->assertEquals($this->_stats['mean'], $this->_result->getMean());
}
public function testGetStddev()
{
$this->assertEquals($this->_stats['stddev'], $this->_result->getStddev());
}
public function testGetFacets()
{
$this->assertEquals($this->_stats['facets'], $this->_result->getFacets());
}
}
<?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.
*/
class Solarium_Result_Select_StatsTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Stats
*/
protected $_result;
public function setUp()
{
$this->_data = array(
'key1' => 'value1',
'key2' => 'value2',
);
$this->_result = new Solarium_Result_Select_Stats($this->_data);
}
public function testGetResult()
{
$this->assertEquals($this->_data['key1'], $this->_result->getResult('key1'));
}
public function testGetInvalidResult()
{
$this->assertEquals(null, $this->_result->getResult('key3'));
}
public function testGetResults()
{
$this->assertEquals($this->_data, $this->_result->getResults());
}
public function testIterator()
{
$items = array();
foreach($this->_result AS $key => $item)
{
$items[$key] = $item;
}
$this->assertEquals($this->_data, $items);
}
public function testCount()
{
$this->assertEquals(count($this->_data), count($this->_result));
}
}
...@@ -37,7 +37,7 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase ...@@ -37,7 +37,7 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase
*/ */
protected $_result; protected $_result;
protected $_numFound, $_docs, $_components, $_facetSet, $_moreLikeThis, $_highlighting, $_grouping; protected $_numFound, $_docs, $_components, $_facetSet, $_moreLikeThis, $_highlighting, $_grouping, $_stats;
public function setUp() public function setUp()
{ {
...@@ -53,6 +53,7 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase ...@@ -53,6 +53,7 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase
$this->_highlighting = 'dummy-highlighting-value'; $this->_highlighting = 'dummy-highlighting-value';
$this->_grouping = 'dummy-grouping-value'; $this->_grouping = 'dummy-grouping-value';
$this->_spellcheck = 'dummy-grouping-value'; $this->_spellcheck = 'dummy-grouping-value';
$this->_stats = 'dummy-stats-value';
$this->_components = array( $this->_components = array(
Solarium_Query_Select::COMPONENT_FACETSET => $this->_facetSet, Solarium_Query_Select::COMPONENT_FACETSET => $this->_facetSet,
...@@ -60,6 +61,7 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase ...@@ -60,6 +61,7 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase
Solarium_Query_Select::COMPONENT_HIGHLIGHTING => $this->_highlighting, Solarium_Query_Select::COMPONENT_HIGHLIGHTING => $this->_highlighting,
Solarium_Query_Select::COMPONENT_GROUPING => $this->_grouping, Solarium_Query_Select::COMPONENT_GROUPING => $this->_grouping,
Solarium_Query_Select::COMPONENT_SPELLCHECK => $this->_spellcheck, Solarium_Query_Select::COMPONENT_SPELLCHECK => $this->_spellcheck,
Solarium_Query_Select::COMPONENT_STATS => $this->_stats,
); );
$this->_result = new Solarium_Result_SelectDummy(1, 12, $this->_numFound, $this->_docs, $this->_components); $this->_result = new Solarium_Result_SelectDummy(1, 12, $this->_numFound, $this->_docs, $this->_components);
...@@ -138,6 +140,14 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase ...@@ -138,6 +140,14 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase
); );
} }
public function testGetStats()
{
$this->assertEquals(
$this->_components[Solarium_Query_Select::COMPONENT_STATS],
$this->_result->getStats()
);
}
public function testIterator() public function testIterator()
{ {
$docs = array(); $docs = array();
......
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