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

Merge branch 'feature/group-component' into develop

parents a71f798c 09f161a0
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
$query->setRows(50);
// get grouping component and set a field to group by
$groupComponent = $query->getGrouping();
$groupComponent->addField('inStock');
// maximum number of items per group
$groupComponent->setLimit(3);
// get a group count
$groupComponent->setNumberOfGroups(true);
// this executes the query and returns the result
$resultset = $client->select($query);
$groups = $resultset->getGrouping();
foreach($groups AS $groupKey => $fieldGroup) {
echo '<h1>'.$groupKey.'</h1>';
echo 'Matches: '.$fieldGroup->getMatches().'<br/>';
echo 'Number of groups: '.$fieldGroup->getNumberOfGroups();
foreach($fieldGroup AS $valueGroup) {
echo '<h2>'.(int)$valueGroup->getValue().'</h2>';
foreach($valueGroup AS $document) {
echo '<hr/><table>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
}
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// get grouping component and create two query groups
$groupComponent = $query->getGrouping();
$groupComponent->addQuery('price:[0 TO 99.99]');
$groupComponent->addQuery('price:[100 TO *]');
// sorting inside groups
$groupComponent->setSort('price desc');
// maximum number of items per group
$groupComponent->setLimit(5);
// this executes the query and returns the result
$resultset = $client->select($query);
$groups = $resultset->getGrouping();
foreach($groups AS $groupKey => $group) {
echo '<h1>'.$groupKey.'</h1>';
foreach($group AS $document) {
echo '<hr/><table>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
}
htmlFooter();
\ No newline at end of file
...@@ -43,6 +43,8 @@ ...@@ -43,6 +43,8 @@
<li><a href="2.5.2-morelikethis.php">2.5.2 MoreLikeThis</a></li> <li><a href="2.5.2-morelikethis.php">2.5.2 MoreLikeThis</a></li>
<li><a href="2.5.3-highlighting.php">2.5.3 Highlighting</a></li> <li><a href="2.5.3-highlighting.php">2.5.3 Highlighting</a></li>
<li><a href="2.5.4-dismax.php">2.5.4 Dismax</a></li> <li><a href="2.5.4-dismax.php">2.5.4 Dismax</a></li>
<li><a href="2.5.5-grouping-by-field.php">2.5.5 Grouping by field</a></li>
<li><a href="2.5.6-grouping-by-query.php">2.5.6 Grouping by query</a></li>
</ul> </ul>
<li><a href="2.6-helper-functions.php">2.6 Helper functions</a></li> <li><a href="2.6-helper-functions.php">2.6 Helper functions</a></li>
<li><a href="2.7-query-reuse.php">2.7 Query re-use</a></li> <li><a href="2.7-query-reuse.php">2.7 Query re-use</a></li>
......
...@@ -210,6 +210,10 @@ class Solarium_Client_Request extends Solarium_Configurable ...@@ -210,6 +210,10 @@ class Solarium_Client_Request extends Solarium_Configurable
} }
$this->_params[$key][] = $value; $this->_params[$key][] = $value;
} else { } else {
// not all solr handlers support 0/1 as boolean values...
if($value === true) $value = 'true';
if($value === false) $value = 'false';
$this->_params[$key] = $value; $this->_params[$key] = $value;
} }
} }
......
<?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 Grouping to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_Grouping
{
/**
* Add request settings for Grouping
*
* @param Solarium_Query_Select_Component_Grouping $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable grouping
$request->addParam('group', 'true');
$request->addParam('group.field', $component->getFields());
$request->addParam('group.query', $component->getQueries());
$request->addParam('group.limit', $component->getLimit());
$request->addParam('group.offset', $component->getOffset());
$request->addParam('group.sort', $component->getSort());
$request->addParam('group.main', $component->getMainResult());
$request->addParam('group.ngroups', $component->getNumberOfGroups());
$request->addParam('group.cache.percent', $component->getCachePercentage());
return $request;
}
}
\ No newline at end of file
...@@ -76,11 +76,17 @@ class Solarium_Client_ResponseParser_Select extends Solarium_Client_ResponsePars ...@@ -76,11 +76,17 @@ class Solarium_Client_ResponseParser_Select extends Solarium_Client_ResponsePars
$components[$component->getType()] = $componentParser->parse($query, $component, $data); $components[$component->getType()] = $componentParser->parse($query, $component, $data);
} }
} }
if (isset($data['response']['numFound'])) {
$numFound = $data['response']['numFound'];
} else {
$numFound = null;
}
return array( return array(
'status' => $data['responseHeader']['status'], 'status' => $data['responseHeader']['status'],
'queryTime' => $data['responseHeader']['QTime'], 'queryTime' => $data['responseHeader']['QTime'],
'numfound' => $data['response']['numFound'], 'numfound' => $numFound,
'documents' => $documents, 'documents' => $documents,
'components' => $components, 'components' => $components,
); );
......
<?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 Grouping result from the data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select_Component_Grouping
{
/**
* Parse result data into result objects
*
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_Grouping $grouping
* @param array $data
* @return Solarium_Result_Select_Grouping
*/
public function parse($query, $grouping, $data)
{
$groups = array();
if (isset($data['grouped'])) {
// parse field groups
foreach ($grouping->getFields() as $field) {
if (isset($data['grouped'][$field])) {
$result = $data['grouped'][$field];
$matches = (isset($result['matches'])) ? $result['matches'] : null;
$groupCount = (isset($result['ngroups'])) ? $result['ngroups'] : null;
$valueGroups = array();
foreach ($result['groups'] as $valueGroupResult) {
$value = (isset($valueGroupResult['groupValue'])) ? $valueGroupResult['groupValue'] : null;
$numFound = (isset($valueGroupResult['doclist']['numFound'])) ? $valueGroupResult['doclist']['numFound'] : null;
$start = (isset($valueGroupResult['doclist']['start'])) ? $valueGroupResult['doclist']['start'] : null;
// create document instances
$documentClass = $query->getOption('documentclass');
$documents = array();
if (isset($valueGroupResult['doclist']['docs']) && is_array($valueGroupResult['doclist']['docs'])) {
foreach ($valueGroupResult['doclist']['docs'] as $doc) {
$documents[] = new $documentClass($doc);
}
}
$valueGroups[] = new Solarium_Result_Select_Grouping_ValueGroup($value, $numFound, $start, $documents);
}
$groups[$field] = new Solarium_Result_Select_Grouping_FieldGroup($matches, $groupCount, $valueGroups);
}
}
// parse query groups
foreach ($grouping->getQueries() as $groupQuery) {
if (isset($data['grouped'][$groupQuery])) {
$result = $data['grouped'][$groupQuery];
// get statistics
$matches = (isset($result['matches'])) ? $result['matches'] : null;
$numFound = (isset($result['doclist']['numFound'])) ? $result['doclist']['numFound'] : null;
$start = (isset($result['doclist']['start'])) ? $result['doclist']['start'] : null;
$maxScore = (isset($result['doclist']['maxScore'])) ? $result['doclist']['maxScore'] : null;
// create document instances
$documentClass = $query->getOption('documentclass');
$documents = array();
if (isset($result['doclist']['docs']) && is_array($result['doclist']['docs'])) {
foreach ($result['doclist']['docs'] as $doc) {
$documents[] = new $documentClass($doc);
}
}
// create a group result object
$group = new Solarium_Result_Select_Grouping_QueryGroup($matches, $numFound, $start, $maxScore, $documents);
$groups[$groupQuery] = $group;
}
}
}
return new Solarium_Result_Select_Grouping($groups);
}
}
\ No newline at end of file
...@@ -260,7 +260,8 @@ class Solarium_Query_Helper ...@@ -260,7 +260,8 @@ class Solarium_Query_Helper
return preg_replace_callback( return preg_replace_callback(
$this->_placeHolderPattern, $this->_placeHolderPattern,
array($this, '_renderPlaceHolder'), array($this, '_renderPlaceHolder'),
$query); $query
);
} }
protected function _renderPlaceHolder($matches) protected function _renderPlaceHolder($matches)
......
...@@ -62,6 +62,7 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -62,6 +62,7 @@ class Solarium_Query_Select extends Solarium_Query
const COMPONENT_DISMAX = 'dismax'; const COMPONENT_DISMAX = 'dismax';
const COMPONENT_MORELIKETHIS = 'morelikethis'; const COMPONENT_MORELIKETHIS = 'morelikethis';
const COMPONENT_HIGHLIGHTING = 'highlighting'; const COMPONENT_HIGHLIGHTING = 'highlighting';
const COMPONENT_GROUPING = 'grouping';
/** /**
* Get type for this query * Get type for this query
...@@ -114,6 +115,11 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -114,6 +115,11 @@ class Solarium_Query_Select extends Solarium_Query
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Highlighting', 'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Highlighting',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Highlighting', 'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Highlighting',
), ),
self::COMPONENT_GROUPING => array(
'component' => 'Solarium_Query_Select_Component_Grouping',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Grouping',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Grouping',
),
); );
/** /**
...@@ -755,4 +761,16 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -755,4 +761,16 @@ class Solarium_Query_Select extends Solarium_Query
return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, true); return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, true);
} }
/**
* Get a grouping component instance
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Query_Select_Component_Grouping
*/
public function getGrouping()
{
return $this->getComponent(Solarium_Query_Select::COMPONENT_GROUPING, true);
}
} }
\ 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
*/
/**
* Grouping component
*
* Also known as Result Grouping or Field Collapsing.
* See the Solr wiki for more info about this functionality
*
* @link http://wiki.apache.org/solr/FieldCollapsing
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Select_Component_Grouping extends Solarium_Query_Select_Component
{
/**
* Values for format option
*/
const FORMAT_GROUPED = 'grouped';
const FORMAT_SIMPLE = 'simple';
/**
* Component type
*
* @var string
*/
protected $_type = Solarium_Query_Select::COMPONENT_GROUPING;
/**
* Fields for grouping
*
* @var array
*/
protected $_fields = array();
/**
* Queries for grouping
*
* @var array
*/
protected $_queries = 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 'queries':
$this->setQueries($value);
break;
case 'fields':
$this->setFields($value);
break;
}
}
}
/**
* Add a grouping field
*
* Group based on the unique values of a field
*
* @param string $field
* @return Solarium_Field_Select_Component_Grouping fluent interface
*/
public function addField($field)
{
$this->_fields[] = $field;
return $this;
}
/**
* Add multiple grouping fields
*
* You can use an array or a comma separated string as input
*
* @param array|string $fields
* @return Solarium_Field_Select_Component_Grouping Provides fluent interface
*/
public function addFields($fields)
{
if (is_string($fields)) {
$fields = explode(',', $fields);
$fields = array_map('trim', $fields);
}
$this->_fields = array_merge($this->_fields, $fields);
return $this;
}
/**
* Get all fields
*
* @return array
*/
public function getFields()
{
return $this->_fields;
}
/**
* Remove all fields
*
* @return Solarium_Field_Select_Component_Grouping 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);
}
/**
* Add a grouping query
*
* Group documents that match the given query
*
* @param string $query
* @return Solarium_Query_Select_Component_Grouping fluent interface
*/
public function addQuery($query)
{
$this->_queries[] = $query;
return $this;
}
/**
* Add multiple grouping queries
*
* @param array|string $queries
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function addQueries($queries)
{
if(!is_array($queries)) $queries = array($queries);
$this->_queries = array_merge($this->_queries, $queries);
return $this;
}
/**
* Get all queries
*
* @return array
*/
public function getQueries()
{
return $this->_queries;
}
/**
* Remove all queries
*
* @return Solarium_Query_Select_Component_Grouping fluent interface
*/
public function clearQueries()
{
$this->_queries = array();
return $this;
}
/**
* Set multiple queries
*
* This overwrites any existing queries
*
* @param array $queries
*/
public function setQueries($queries)
{
$this->clearQueries();
$this->addQueries($queries);
}
/**
* Set limit option
*
* The number of results (documents) to return for each group
*
* @param int $limit
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function setLimit($limit)
{
return $this->_setOption('limit', $limit);
}
/**
* Get limit option
*
* @return string|null
*/
public function getLimit()
{
return $this->getOption('limit');
}
/**
* Set offset option
*
* The offset into the document list of each group.
*
* @param int $offset
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function setOffset($offset)
{
return $this->_setOption('offset', $offset);
}
/**
* Get offset option
*
* @return string|null
*/
public function getOffset()
{
return $this->getOption('offset');
}
/**
* Set sort option
*
* How to sort documents within a single group
*
* @param string $sort
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function setSort($sort)
{
return $this->_setOption('sort', $sort);
}
/**
* Get sort option
*
* @return string|null
*/
public function getSort()
{
return $this->getOption('sort');
}
/**
* Set mainresult option
*
* If true, the result of the first field grouping command is used as the main
* result list in the response, using group format 'simple'
*
* @param boolean $value
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function setMainResult($value)
{
return $this->_setOption('mainresult', $value);
}
/**
* Get mainresult option
*
* @return boolean|null
*/
public function getMainResult()
{
return $this->getOption('mainresult');
}
/**
* Set numberofgroups option
*
* If true, includes the number of groups that have matched the query.
*
* @param boolean $value
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function setNumberOfGroups($value)
{
return $this->_setOption('numberofgroups', $value);
}
/**
* Get numberofgroups option
*
* @return boolean|null
*/
public function getNumberOfGroups()
{
return $this->getOption('numberofgroups');
}
/**
* Set cachepercentage option
*
* If > 0 enables grouping cache. Grouping is executed actual two searches.
* This option caches the second search. A value of 0 disables grouping caching.
*
* Tests have shown that this cache only improves search time with boolean queries,
* wildcard queries and fuzzy queries. For simple queries like a term query or
* a match all query this cache has a negative impact on performance
*
* @param integer $value
* @return Solarium_Query_Select_Component_Grouping Provides fluent interface
*/
public function setCachePercentage($value)
{
return $this->_setOption('cachepercentage', $value);
}
/**
* Get cachepercentage option
*
* @return integer|null
*/
public function getCachePercentage()
{
return $this->getOption('cachepercentage');
}
}
\ No newline at end of file
...@@ -234,6 +234,18 @@ class Solarium_Result_Select extends Solarium_Result_QueryType ...@@ -234,6 +234,18 @@ class Solarium_Result_Select extends Solarium_Result_QueryType
return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING); return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING);
} }
/**
* Get grouping component result
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Result_Select_Grouping
*/
public function getGrouping()
{
return $this->getComponent(Solarium_Query_Select::COMPONENT_GROUPING);
}
/** /**
* Get facetset component result * Get facetset component result
* *
......
<?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 grouping result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Grouping implements IteratorAggregate, Countable
{
/**
* Group results array
*
* @var array
*/
protected $_groups;
/**
* Constructor
*
* @param array $groups
* @return void
*/
public function __construct($groups)
{
$this->_groups = $groups;
}
/**
* Get all groups
*
* @return array
*/
public function getGroups()
{
return $this->_groups;
}
/**
* Get a group
*
* @param string $key
* @return Solarium_Result_Select_Grouping_FieldGroup|Solarium_Result_Select_Grouping_QueryGroup
*/
public function getGroup($key)
{
if (isset($this->_groups[$key])) {
return $this->_groups[$key];
} else {
return null;
}
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_groups);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_groups);
}
}
\ 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 grouping field group result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Grouping_FieldGroup implements IteratorAggregate, Countable
{
/**
* Match count
*
* @var int
*/
protected $_matches;
/**
* Number of groups
*
* @var int
*/
protected $_numberOfGroups;
/**
* Value groups
*
* @var array
*/
protected $_valueGroups;
/**
* Constructor
*
* @param int $matches
* @param int $numberOfGroups
* @param array $groups
* @return void
*/
public function __construct($matches, $numberOfGroups, $groups)
{
$this->_matches = $matches;
$this->_numberOfGroups = $numberOfGroups;
$this->_valueGroups = $groups;
}
/**
* Get matches value
*
* @return int
*/
public function getMatches()
{
return $this->_matches;
}
/**
* Get numberOfGroups value
*
* Only available if the numberofgroups option in the query was 'true'
*
* @return int
*/
public function getNumberOfGroups()
{
return $this->_numberOfGroups;
}
/**
* Get all value groups
*
* @return array
*/
public function getValueGroups()
{
return $this->_valueGroups;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_valueGroups);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_valueGroups);
}
}
\ 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 grouping query group result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Grouping_QueryGroup implements IteratorAggregate, Countable
{
/**
* Match count
*
* @var int
*/
protected $_matches;
/**
* NumFound value
*
* @var int
*/
protected $_numFound;
/**
* Start offset
*
* @var int
*/
protected $_start;
/**
* Maximum score in group
*
* @var float
*/
protected $_maximumScore;
/**
* Group documents array
*
* @var array
*/
protected $_documents;
/**
* Constructor
*
* @param array $groups
* @return void
*/
public function __construct($matches, $numFound, $start, $maximumScore, $documents)
{
$this->_matches = $matches;
$this->_numFound = $numFound;
$this->_start = $start;
$this->_maximumScore = $maximumScore;
$this->_documents = $documents;
}
/**
* Get matches value
*
* @return int
*/
public function getMatches()
{
return $this->_matches;
}
/**
* Get numFound value
*
* @return int
*/
public function getNumFound()
{
return $this->_numFound;
}
/**
* Get start value
*
* @return int
*/
public function getStart()
{
return $this->_start;
}
/**
* Get maximumScore value
*
* @return int
*/
public function getMaximumScore()
{
return $this->_maximumScore;
}
/**
* Get all documents
*
* @return array
*/
public function getDocuments()
{
return $this->_documents;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_documents);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_documents);
}
}
\ 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 grouping field value group result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Grouping_ValueGroup implements IteratorAggregate, Countable
{
/**
* Field value
*
* @var string
*/
protected $_value;
/**
* NumFound
*
* @var int
*/
protected $_numFound;
/**
* Start position
*
* @var int
*/
protected $_start;
/**
* Documents in this group
*
* @var array
*/
protected $_documents;
/**
* Constructor
*
* @param string $value
* @param int $numFound
* @param int $start
* @param array $documents
* @return void
*/
public function __construct($value, $numFound, $start, $documents)
{
$this->_value = $value;
$this->_numFound = $numFound;
$this->_start = $start;
$this->_documents = $documents;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->_value;
}
/**
* Get numFound
*
* @return int
*/
public function getNumFound()
{
return $this->_numFound;
}
/**
* Get start
*
* @return int
*/
public function getStart()
{
return $this->_start;
}
/**
* Get all documents
*
* @return array
*/
public function getDocuments()
{
return $this->_documents;
}
/**
* IteratorAggregate implementation
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_documents);
}
/**
* Countable implementation
*
* @return int
*/
public function count()
{
return count($this->_documents);
}
}
\ No newline at end of file
...@@ -126,7 +126,7 @@ class Solarium_Client_RequestBuilder_Select_Component_FacetSetTest extends PHPUn ...@@ -126,7 +126,7 @@ class Solarium_Client_RequestBuilder_Select_Component_FacetSetTest extends PHPUn
); );
$this->assertEquals( $this->assertEquals(
'?facet=true&facet.missing=1&facet.limit=10&facet.field={!key=f1}owner&facet.query={!key=f2}category:23&facet.query={!key=f4}category:40', '?facet=true&facet.missing=true&facet.limit=10&facet.field={!key=f1}owner&facet.query={!key=f2}category:23&facet.query={!key=f4}category:40',
urldecode($request->getUri()) urldecode($request->getUri())
); );
} }
......
<?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_GroupingTest extends PHPUnit_Framework_TestCase
{
public function testBuild()
{
$builder = new Solarium_Client_RequestBuilder_Select_Component_Grouping;
$request = new Solarium_Client_Request();
$component = new Solarium_Query_Select_Component_Grouping();
$component->setFields(array('fieldA','fieldB'));
$component->setQueries(array('cat:1','cat:2'));
$component->setLimit(12);
$component->setOffset(2);
$component->setSort('score desc');
$component->setMainResult(true);
$component->setNumberOfGroups(false);
$component->setCachePercentage(50);
$request = $builder->build($component, $request);
$this->assertEquals(
array(
'group' => 'true',
'group.field' => array('fieldA','fieldB'),
'group.query' => array('cat:1','cat:2'),
'group.limit' => 12,
'group.offset' => 2,
'group.sort' => 'score desc',
'group.main' => 'true',
'group.ngroups' => 'false',
'group.cache.percent' => 50,
),
$request->getParams()
);
}
}
...@@ -63,12 +63,12 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P ...@@ -63,12 +63,12 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
$this->assertEquals( $this->assertEquals(
array( array(
'hl' => true, 'hl' => 'true',
'hl.fl' => 'fieldA,fieldB', 'hl.fl' => 'fieldA,fieldB',
'hl.snippets' => 2, 'hl.snippets' => 2,
'hl.fragsize' => 3, 'hl.fragsize' => 3,
'hl.mergeContiguous' => true, 'hl.mergeContiguous' => 'true',
'hl.requireFieldMatch' => false, 'hl.requireFieldMatch' => 'false',
'hl.maxAnalyzedChars' => 100, 'hl.maxAnalyzedChars' => 100,
'hl.alternateField' => 'fieldC', 'hl.alternateField' => 'fieldC',
'hl.maxAlternateFieldLength' => 5, 'hl.maxAlternateFieldLength' => 5,
...@@ -78,9 +78,9 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P ...@@ -78,9 +78,9 @@ class Solarium_Client_RequestBuilder_Select_Component_HighlightingTest extends P
'hl.fragmenter' => 'myFragmenter', 'hl.fragmenter' => 'myFragmenter',
'hl.fragListBuilder' => 'myFragListBuilder', 'hl.fragListBuilder' => 'myFragListBuilder',
'hl.fragmentsBuilder' => 'myFragmentsBuilder', 'hl.fragmentsBuilder' => 'myFragmentsBuilder',
'hl.useFastVectorHighlighter' => false, 'hl.useFastVectorHighlighter' => 'false',
'hl.usePhraseHighlighter' => true, 'hl.usePhraseHighlighter' => 'true',
'hl.highlightMultiTerm' => true, 'hl.highlightMultiTerm' => 'true',
'hl.regex.slop' => 1.3, 'hl.regex.slop' => 1.3,
'hl.regex.pattern' => 'mypattern', 'hl.regex.pattern' => 'mypattern',
), ),
......
...@@ -172,6 +172,24 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase ...@@ -172,6 +172,24 @@ class Solarium_Client_RequestTest extends PHPUnit_Framework_TestCase
); );
} }
public function testAddParamBoolean()
{
$params = array(
'param1' => true,
'param2' => false,
);
$this->_request->addParams($params);
$this->assertEquals(
array(
'param1' => 'true',
'param2' => 'false',
),
$this->_request->getParams()
);
}
public function testAddParamMultivalue() public function testAddParamMultivalue()
{ {
$params = array( $params = array(
......
<?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_GroupingTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Client_ResponseParser_Select_Component_Grouping
*/
protected $_parser;
/**
* @var Solarium_Query_Select
*/
protected $_query;
/**
* @var Solarium_Query_Select_Component_Grouping
*/
protected $_grouping;
/**
* @var Solarium_Result_Select_Grouping
*/
protected $_result;
public function setUp()
{
$this->_parser = new Solarium_Client_ResponseParser_Select_Component_Grouping;
$this->_query = new Solarium_Query_Select();
$this->_grouping = $this->_query->getGrouping();
$this->_grouping->addField('fieldA');
$this->_grouping->addQuery('cat:1');
$data = array(
'grouped' => array(
'fieldA' => array(
'matches' => 25,
'ngroups' => 12,
'groups' => array(
array(
'groupValue' => 'test value',
'doclist' => array(
'numFound' => 13,
'docs' => array(
array('id' => 1, 'name' => 'test')
)
)
)
)
),
'cat:1' => array(
'matches' => 40,
'doclist' => array(
'numFound' => 22,
'docs' => array(
array('id' => 2, 'name' => 'dummy2'),
array('id' => 5, 'name' => 'dummy5')
)
)
)
)
);
$this->_result = $this->_parser->parse($this->_query, $this->_grouping, $data);
}
public function testGroupParsing()
{
$this->assertEquals(2, count($this->_result->getGroups()));
$fieldGroup = $this->_result->getGroup('fieldA');
$queryGroup = $this->_result->getGroup('cat:1');
$this->assertEquals('Solarium_Result_Select_Grouping_FieldGroup', get_class($fieldGroup));
$this->assertEquals('Solarium_Result_Select_Grouping_QueryGroup', get_class($queryGroup));
}
public function testFieldGroupParsing()
{
$fieldGroup = $this->_result->getGroup('fieldA');
$valueGroups = $fieldGroup->getValueGroups();
$this->assertEquals(25, $fieldGroup->getMatches());
$this->assertEquals(12, $fieldGroup->getNumberOfGroups());
$this->assertEquals(1, count($valueGroups));
$valueGroup = $valueGroups[0];
$this->assertEquals(13, $valueGroup->getNumFound());
$docs = $valueGroup->getDocuments();
$this->assertEquals('test', $docs[0]->name);
}
public function testQueryGroupParsing()
{
$queryGroup = $this->_result->getGroup('cat:1');
$this->assertEquals(40, $queryGroup->getMatches());
$this->assertEquals(22, $queryGroup->getNumFound());
$docs = $queryGroup->getDocuments();
$this->assertEquals('dummy5', $docs[1]->name);
}
public function testParseNoData()
{
$result = $this->_parser->parse($this->_query, $this->_grouping, array());
$this->assertEquals(array(), $result->getGroups());
}
}
...@@ -76,8 +76,40 @@ class Solarium_Client_ResponseParser_SelectTest extends PHPUnit_Framework_TestCa ...@@ -76,8 +76,40 @@ class Solarium_Client_ResponseParser_SelectTest extends PHPUnit_Framework_TestCa
Solarium_Query_Select::COMPONENT_FACETSET => new Solarium_Result_Select_FacetSet(array()) Solarium_Query_Select::COMPONENT_FACETSET => new Solarium_Result_Select_FacetSet(array())
); );
$this->assertEquals($components, $result['components']); $this->assertEquals($components, $result['components']);
}
public function testParseWithoutNumFound()
{
$data = array(
'response' => array(
'docs' => array(
array('fieldA' => 1, 'fieldB' => 'Test'),
array('fieldA' => 2, 'fieldB' => 'Test2')
),
),
'responseHeader' => array(
'status' => 1,
'QTime' => 13,
)
);
$query = new Solarium_Query_Select(array('documentclass' => 'Solarium_Document_ReadWrite'));
$query->getFacetSet();
$resultStub = $this->getMock('Solarium_Result_Select', array(), array(), '', false);
$resultStub->expects($this->once())
->method('getData')
->will($this->returnValue($data));
$resultStub->expects($this->once())
->method('getQuery')
->will($this->returnValue($query));
$parser = new Solarium_Client_ResponseParser_Select;
$result = $parser->parse($resultStub);
$this->assertEquals(1, $result['status']);
$this->assertEquals(13, $result['queryTime']);
$this->assertEquals(null, $result['numfound']);
} }
} }
...@@ -191,4 +191,10 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase ...@@ -191,4 +191,10 @@ class Solarium_Query_HelperTest extends PHPUnit_Framework_TestCase
); );
} }
public function testAssembleInvalidPartNumber()
{
$this->setExpectedException('Solarium_Exception');
$this->_helper->assemble('cat:%1% AND content:%2%',array('value1'));
}
} }
<?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_GroupingTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Query_Select_Component_Grouping
*/
protected $_grouping;
public function setUp()
{
$this->_grouping = new Solarium_Query_Select_Component_Grouping;
}
public function testConfigMode()
{
$options = array(
'fields' => array('fieldA','fieldB'),
'queries' => array('cat:3','cat:4'),
'limit' => 8,
'offset' => 1,
'sort' => 'score desc',
'mainresult' => false,
'numberofgroups' => true,
'cachepercentage' => 45,
);
$this->_grouping->setOptions($options);
$this->assertEquals($options['fields'], $this->_grouping->getFields());
$this->assertEquals($options['queries'], $this->_grouping->getQueries());
$this->assertEquals($options['limit'], $this->_grouping->getLimit());
$this->assertEquals($options['offset'], $this->_grouping->getOffset());
$this->assertEquals($options['sort'], $this->_grouping->getSort());
$this->assertEquals($options['mainresult'], $this->_grouping->getMainResult());
$this->assertEquals($options['numberofgroups'], $this->_grouping->getNumberOfGroups());
$this->assertEquals($options['cachepercentage'], $this->_grouping->getCachePercentage());
}
public function testGetType()
{
$this->assertEquals(Solarium_Query_Select::COMPONENT_GROUPING, $this->_grouping->getType());
}
public function testSetAndGetFieldsSingle()
{
$value = 'fieldC';
$this->_grouping->setFields($value);
$this->assertEquals(
array($value),
$this->_grouping->getFields()
);
}
public function testSetAndGetFieldsCommaSeparated()
{
$value = 'fieldD, fieldE';
$this->_grouping->setFields($value);
$this->assertEquals(
array(
'fieldD',
'fieldE',
),
$this->_grouping->getFields()
);
}
public function testSetAndGetFieldsArray()
{
$values = array('fieldD', 'fieldE');
$this->_grouping->setFields($values);
$this->assertEquals(
$values,
$this->_grouping->getFields()
);
}
public function testSetAndGetQueriesSingle()
{
$value = 'cat:3';
$this->_grouping->setQueries($value);
$this->assertEquals(
array($value),
$this->_grouping->getQueries()
);
}
public function testSetAndGetQueriesArray()
{
$values = array('cat:5', 'cat:6');
$this->_grouping->setQueries($values);
$this->assertEquals(
$values,
$this->_grouping->getQueries()
);
}
public function testSetAndGetLimit()
{
$value = '12';
$this->_grouping->setLimit($value);
$this->assertEquals(
$value,
$this->_grouping->getLimit()
);
}
public function testSetAndGetOffset()
{
$value = '2';
$this->_grouping->setOffset($value);
$this->assertEquals(
$value,
$this->_grouping->getOffset()
);
}
public function testSetAndGetSort()
{
$value = 'price desc';
$this->_grouping->setSort($value);
$this->assertEquals(
$value,
$this->_grouping->getSort()
);
}
public function testSetAndGetMainResult()
{
$value = true;
$this->_grouping->setMainResult($value);
$this->assertEquals(
$value,
$this->_grouping->getMainResult()
);
}
public function testSetAndGetNumberOfGroups()
{
$value = true;
$this->_grouping->setNumberOfGroups($value);
$this->assertEquals(
$value,
$this->_grouping->getNumberOfGroups()
);
}
public function testSetAndGetCachePercentage()
{
$value = 40;
$this->_grouping->setCachePercentage($value);
$this->assertEquals(
$value,
$this->_grouping->getCachePercentage()
);
}
}
...@@ -477,11 +477,21 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase ...@@ -477,11 +477,21 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
public function testGetHighlighting() public function testGetHighlighting()
{ {
$dismax = $this->_query->getHighlighting(); $hlt = $this->_query->getHighlighting();
$this->assertEquals( $this->assertEquals(
'Solarium_Query_Select_Component_Highlighting', 'Solarium_Query_Select_Component_Highlighting',
get_class($dismax) get_class($hlt)
);
}
public function testGetGrouping()
{
$grouping = $this->_query->getGrouping();
$this->assertEquals(
'Solarium_Query_Select_Component_Grouping',
get_class($grouping)
); );
} }
......
<?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_Grouping_FieldGroupTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Grouping_FieldGroup
*/
protected $_group;
protected $_matches, $_numberOfGroups, $_items;
public function setUp()
{
$this->_matches = 12;
$this->_numberOfGroups = 6;
$this->_items = array(
'key1' => 'content1',
'key2' => 'content2',
);
$this->_group = new Solarium_Result_Select_Grouping_FieldGroup($this->_matches, $this->_numberOfGroups, $this->_items);
}
public function testGetMatches()
{
$this->assertEquals(
$this->_matches,
$this->_group->getMatches()
);
}
public function testGetNumberOfGroups()
{
$this->assertEquals(
$this->_numberOfGroups,
$this->_group->getNumberOfGroups()
);
}
public function testIterator()
{
$items = array();
foreach($this->_group AS $key => $item)
{
$items[$key] = $item;
}
$this->assertEquals($this->_items, $items);
}
public function testCount()
{
$this->assertEquals(count($this->_items), count($this->_group));
}
}
<?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_Grouping_QueryGroupTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Grouping_QueryGroup
*/
protected $_group;
protected $_matches, $_numFound, $_start, $_maximumScore, $_items;
public function setUp()
{
$this->_matches = 12;
$this->_numFound = 6;
$this->_start = 2;
$this->_maximumScore = 0.89;
$this->_items = array(
'key1' => 'content1',
'key2' => 'content2',
);
$this->_group = new Solarium_Result_Select_Grouping_QueryGroup($this->_matches, $this->_numFound, $this->_start, $this->_maximumScore, $this->_items);
}
public function testGetMatches()
{
$this->assertEquals(
$this->_matches,
$this->_group->getMatches()
);
}
public function testGetNumFound()
{
$this->assertEquals(
$this->_numFound,
$this->_group->getNumFound()
);
}
public function testGetStart()
{
$this->assertEquals(
$this->_start,
$this->_group->getStart()
);
}
public function testGetMaximumScore()
{
$this->assertEquals(
$this->_maximumScore,
$this->_group->getMaximumScore()
);
}
public function testGetDocuments()
{
$this->assertEquals(
$this->_items,
$this->_group->getDocuments()
);
}
public function testIterator()
{
$items = array();
foreach($this->_group AS $key => $item)
{
$items[$key] = $item;
}
$this->assertEquals($this->_items, $items);
}
public function testCount()
{
$this->assertEquals(count($this->_items), count($this->_group));
}
}
<?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_Grouping_ValueGroupTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Grouping_ValueGroup
*/
protected $_group;
protected $_value, $_numFound, $_start, $_items;
public function setUp()
{
$this->_value = 'test value';
$this->_numFound = 6;
$this->_start = 2;
$this->_items = array(
'key1' => 'content1',
'key2' => 'content2',
);
$this->_group = new Solarium_Result_Select_Grouping_ValueGroup($this->_value, $this->_numFound, $this->_start, $this->_items);
}
public function testGetValue()
{
$this->assertEquals(
$this->_value,
$this->_group->getValue()
);
}
public function testGetNumFound()
{
$this->assertEquals(
$this->_numFound,
$this->_group->getNumFound()
);
}
public function testGetStart()
{
$this->assertEquals(
$this->_start,
$this->_group->getStart()
);
}
public function testGetDocuments()
{
$this->assertEquals(
$this->_items,
$this->_group->getDocuments()
);
}
public function testIterator()
{
$items = array();
foreach($this->_group AS $key => $item)
{
$items[$key] = $item;
}
$this->assertEquals($this->_items, $items);
}
public function testCount()
{
$this->assertEquals(count($this->_items), count($this->_group));
}
}
<?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_GroupingTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Result_Select_Grouping
*/
protected $_grouping;
protected $_items;
public function setUp()
{
$this->_items = array(
'key1' => 'content1',
'key2' => 'content2',
);
$this->_grouping = new Solarium_Result_Select_Grouping($this->_items);
}
public function testGetGroups()
{
$this->assertEquals(
$this->_items,
$this->_grouping->getGroups()
);
}
public function testGetGroup()
{
$this->assertEquals(
$this->_items['key1'],
$this->_grouping->getGroup('key1')
);
}
public function testGetGroupInvalid()
{
$this->assertEquals(
null,
$this->_grouping->getGroup('invalidkey')
);
}
public function testIterator()
{
$items = array();
foreach($this->_grouping AS $key => $item)
{
$items[$key] = $item;
}
$this->assertEquals($this->_items, $items);
}
public function testCount()
{
$this->assertEquals(count($this->_items), count($this->_grouping));
}
}
...@@ -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; protected $_numFound, $_docs, $_components, $_facetSet, $_moreLikeThis, $_highlighting, $_grouping;
public function setUp() public function setUp()
{ {
...@@ -51,11 +51,13 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase ...@@ -51,11 +51,13 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase
$this->_facetSet = 'dummy-facetset-value'; $this->_facetSet = 'dummy-facetset-value';
$this->_moreLikeThis = 'dummy-facetset-value'; $this->_moreLikeThis = 'dummy-facetset-value';
$this->_highlighting = 'dummy-highlighting-value'; $this->_highlighting = 'dummy-highlighting-value';
$this->_grouping = 'dummy-grouping-value';
$this->_components = array( $this->_components = array(
Solarium_Query_Select::COMPONENT_FACETSET => $this->_facetSet, Solarium_Query_Select::COMPONENT_FACETSET => $this->_facetSet,
Solarium_Query_Select::COMPONENT_MORELIKETHIS => $this->_moreLikeThis, Solarium_Query_Select::COMPONENT_MORELIKETHIS => $this->_moreLikeThis,
Solarium_Query_Select::COMPONENT_HIGHLIGHTING => $this->_highlighting Solarium_Query_Select::COMPONENT_HIGHLIGHTING => $this->_highlighting,
Solarium_Query_Select::COMPONENT_GROUPING => $this->_grouping
); );
$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);
...@@ -118,6 +120,14 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase ...@@ -118,6 +120,14 @@ class Solarium_Result_SelectTest extends PHPUnit_Framework_TestCase
); );
} }
public function testGetGrouping()
{
$this->assertEquals(
$this->_components[Solarium_Query_Select::COMPONENT_GROUPING],
$this->_result->getGrouping()
);
}
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