Commit 89a7376c authored by Bas de Nooijer's avatar Bas de Nooijer

- added multiquery facet

- modified several classes to add multiquery facet support
- small code and phpdoc fixes
parent a23a3644
......@@ -96,6 +96,9 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
case Solarium_Query_Select_Facet::QUERY:
$this->addFacetQuery($facet);
break;
case Solarium_Query_Select_Facet::MULTIQUERY:
$this->addFacetMultiQuery($facet);
break;
default:
throw new Solarium_Exception('Unknown facet type');
}
......@@ -108,7 +111,7 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
/**
* Add params for a field facet to request
*
* @param mixed $facet
* @param Solarium_Query_Select_Facet_Field $facet
* @return void
*/
public function addFacetField($facet)
......@@ -133,9 +136,9 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
}
/**
* Add params for a field query to request
* Add params for a facet query to request
*
* @param mixed $facet
* @param Solarium_Query_Select_Facet_Query $facet
* @return void
*/
public function addFacetQuery($facet)
......@@ -149,4 +152,17 @@ class Solarium_Client_Request_Select extends Solarium_Client_Request
);
}
/**
* Add params for a multiquery facet to request
*
* @param Solarium_Query_Select_Facet_MultiQuery $facet
* @return void
*/
public function addFacetMultiQuery($facet)
{
foreach($facet->getQueries() AS $facetQuery) {
$this->addFacetQuery($facetQuery);
}
}
}
\ No newline at end of file
......@@ -84,6 +84,9 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
case Solarium_Query_Select_Facet::QUERY:
$this->_addFacetQuery($facet);
break;
case Solarium_Query_Select_Facet::MULTIQUERY:
$this->_addFacetMultiQuery($facet);
break;
default:
throw new Solarium_Exception('Unknown facet type');
}
......@@ -104,7 +107,7 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
/**
* Add a facet result for a field facet
*
* @param mixed $facet
* @param Solarium_Query_Select_Facet_Field $facet
* @return void
*/
protected function _addFacetField($facet)
......@@ -128,9 +131,9 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
}
/**
* Add a facet result for a field query
* Add a facet result for a facet query
*
* @param mixed $facet
* @param Solarium_Query_Select_Facet_Query $facet
* @return void
*/
protected function _addFacetQuery($facet)
......@@ -143,4 +146,25 @@ class Solarium_Client_Response_Select extends Solarium_Client_Response
new Solarium_Result_Select_Facet_Query($value);
}
}
/**
* Add a facet result for a multiquery facet
*
* @param Solarium_Query_Select_Facet_MultiQuery $facet
* @return void
*/
protected function _addFacetMultiQuery($facet)
{
$values = array();
foreach ($facet->getQueries() AS $query) {
$key = $query->getKey();
if (isset($this->_data['facet_counts']['facet_queries'][$key])) {
$count = $this->_data['facet_counts']['facet_queries'][$key];
$values[$key] = $count;
}
}
$this->_facets[$facet->getKey()] =
new Solarium_Result_Select_Facet_MultiQuery($values);
}
}
\ No newline at end of file
......@@ -51,6 +51,7 @@ abstract class Solarium_Query_Select_Facet extends Solarium_Configurable
*/
const QUERY = 'query';
const FIELD = 'field';
const MULTIQUERY = 'multiquery';
/**
* Exclude tags for this facet
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Query
*/
/**
* Facet MultiQuery
*
* This is a 'virtual' querytype that combines multiple facet queries into a
* single resultset
*
* @package Solarium
* @subpackage Query
*/
class Solarium_Query_Select_Facet_MultiQuery extends Solarium_Query_Select_Facet
{
/**
* Facet query objects
*
* @var array
*/
protected $_facetQueries = 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 'query':
if(!is_array($value)) $value = array($value);
$this->addQueries($value);
break;
}
}
}
/**
* Get the facet type
*
* @return string
*/
public function getType()
{
return self::MULTIQUERY;
}
/**
* Create a new facetQuery
*
* Convenience method so you don't need to manually create facetquery
* objects.
*
* @param string $key
* @param string $query
* @param array $excludes
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function createQuery($key, $query, $excludes = array())
{
// merge excludes with shared excludes
$excludes = array_merge($this->_excludes, $excludes);
$facetQuery = new Solarium_Query_Select_Facet_Query;
$facetQuery->setKey($key);
$facetQuery->setQuery($query);
$facetQuery->setExcludes($excludes);
return $this->addQuery($facetQuery);
}
/**
* Add a facetquery
*
* Supports a facetquery instance or a config array, in that case a new
* facetquery instance wil be created based on the options.
*
* @param Solarium_Query_Select_Facet_Query|array $facetQuery
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function addQuery($facetQuery)
{
if (is_array($facetQuery)) {
$facetQuery = new Solarium_Query_Select_Facet_Query($facetQuery);
}
$key = $facetQuery->getKey();
if (0 === strlen($key)) {
throw new Solarium_Exception('A facetquery must have a key value');
}
if (array_key_exists($key, $this->_facetQueries)) {
throw new Solarium_Exception('A query must have a unique key'
. ' value within a multiquery facet');
}
// forward shared excludes
$facetQuery->addExcludes($this->_excludes);
$this->_facetQueries[$key] = $facetQuery;
return $this;
}
/**
* Add multiple facetqueries
*
* @param array $facetQueries Instances or config array
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function addQueries(array $facetQueries)
{
foreach ($facetQueries AS $key => $facetQuery) {
// in case of a config array: add key to config
if (is_array($facetQuery) && !isset($facetQuery['key'])) {
$facetQuery['key'] = $key;
}
$this->addQuery($facetQuery);
}
return $this;
}
/**
* Get a facetquery
*
* @param string $key
* @return string
*/
public function getQuery($key)
{
if (isset($this->_facetQueries[$key])) {
return $this->_facetQueries[$key];
} else {
return null;
}
}
/**
* Get all facetqueries
*
* @return array
*/
public function getQueries()
{
return $this->_facetQueries;
}
/**
* Remove a single facetquery by key
*
* @param string $key
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function removeQuery($key)
{
if (isset($this->_facetQueries[$key])) {
unset($this->_facetQueries[$key]);
}
return $this;
}
/**
* Remove all facetqueries
*
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function clearQueries()
{
$this->_facetQueries = array();
return $this;
}
/**
* Set multiple facetqueries
*
* This overwrites any existing facetqueries
*
* @param array $facetQueries
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function setFacetQueries($facetQueries)
{
$this->clearQueries();
return $this->addQueries($facetQueries);
}
/**
* Add an exclude tag
*
* Excludes added to the MultiQuery facet a shared by all underlying
* FacetQueries, so they must be forwarded to any existing instances.
*
* If you don't want to share an exclude use the addExclude method of a
* specific FacetQuery instance instead.
*
* @param string $tag
* @return Solarium_Query_Select_Facet Provides fluent interface
*/
public function addExclude($exclude)
{
foreach ($this->_facetQueries AS $facetQuery) {
$facetQuery->addExclude($exclude);
}
return parent::addExclude($exclude);
}
/**
* Remove a single exclude tag
*
* Excludes added to the MultiQuery facet a shared by all underlying
* FacetQueries, so changes must be forwarded to any existing instances.
*
* If you don't want this use the removeExclude method of a
* specific FacetQuery instance instead.
*
* @param string $exclude
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function removeExclude($exclude)
{
foreach ($this->_facetQueries AS $facetQuery) {
$facetQuery->removeExclude($exclude);
}
return parent::removeExclude($exclude);
}
/**
* Remove all excludes
*
* Excludes added to the MultiQuery facet a shared by all underlying
* FacetQueries, so changes must be forwarded to any existing instances.
*
* If you don't want this use the clearExcludes method of a
* specific FacetQuery instance instead.
*
* @return Solarium_Query_Select_Facet_MultiQuery Provides fluent interface
*/
public function clearExcludes()
{
foreach ($this->_facetQueries AS $facetQuery) {
$facetQuery->clearExcludes();
}
return parent::clearExcludes();
}
}
\ 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
*
* @package Solarium
* @subpackage Result
*/
/**
* Select multiquery facet result
*
* A multiquery facet will usually return a dataset of multiple rows, in each
* row a query key and it's count. You can access the values as an array using
* {@link getValues()} or iterate this object.
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Facet_MultiQuery extends Solarium_Result_Select_Facet_Field
{
}
\ No newline at end of file
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