Commit 2f1edbf2 authored by Bas de Nooijer's avatar Bas de Nooijer

- lots of refactoring for the new solarium 2.0 structure

- still very much a work in progress, largely untested!
parent a41cc287
...@@ -55,6 +55,13 @@ ...@@ -55,6 +55,13 @@
class Solarium_Client extends Solarium_Configurable class Solarium_Client extends Solarium_Configurable
{ {
/**
* Querytype definitions
*/
const QUERYTYPE_SELECT = 'Select';
const QUERYTYPE_UPDATE = 'Update';
const QUERYTYPE_PING = 'Ping';
/** /**
* Default options * Default options
* *
...@@ -64,12 +71,25 @@ class Solarium_Client extends Solarium_Configurable ...@@ -64,12 +71,25 @@ class Solarium_Client extends Solarium_Configurable
* @var array * @var array
*/ */
protected $_options = array( protected $_options = array(
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/solr',
'core' => null,
'adapter' => 'Solarium_Client_Adapter_Http', 'adapter' => 'Solarium_Client_Adapter_Http',
'timeout' => 5, );
/**
* Querytype mappings
*/
protected $_queryTypes = array(
self::QUERYTYPE_SELECT => array(
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select',
'responseparser' => 'Solarium_Client_ResponseParser_Select'
),
self::QUERYTYPE_UPDATE => array(
'requestbuilder' => 'Solarium_Client_RequestBuilder_Update',
'responseparser' => 'Solarium_Client_ResponseParser_Update'
),
self::QUERYTYPE_PING => array(
'requestbuilder' => 'Solarium_Client_RequestBuilder_Ping',
'responseparser' => 'Solarium_Client_ResponseParser_Ping'
),
); );
/** /**
...@@ -87,131 +107,11 @@ class Solarium_Client extends Solarium_Configurable ...@@ -87,131 +107,11 @@ class Solarium_Client extends Solarium_Configurable
protected $_adapter; protected $_adapter;
/** /**
* Initialization hook * Request builder instances
*
* In this case the path needs to be cleaned of trailing slashes.
* @see setPath()
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'path':
$this->setPath($value);
break;
}
}
}
/**
* Set an option
*
* If any option of this client is changed after the adapter has been
* instantiated the change is propagated to the adapter. This allows for
* switching the client to another core for a second query, for instance.
*
* @param string $name
* @param mixed $value
* @return Solarium_Configurable
*/
protected function _setOption($name, $value)
{
parent::_setOption($name, $value);
if (null !== $this->_adapter) {
$this->_adapter->setOptions($this->_options);
}
return $this;
}
/**
* Set host option
*
* @param string $host This can be a hostname or an IP address
* @return Solarium_Client Provides fluent interface
*/
public function setHost($host)
{
return $this->_setOption('host', $host);
}
/**
* Get host option
*
* @return string
*/
public function getHost()
{
return $this->getOption('host');
}
/**
* Set port option
* *
* @param int $port Common values are 80, 8080 and 8983 * @var array
* @return Solarium_Client Provides fluent interface
*/
public function setPort($port)
{
return $this->_setOption('port', $port);
}
/**
* Get port option
*
* @return int
*/
public function getPort()
{
return $this->getOption('port');
}
/**
* Set path option
*
* If the path has a trailing slash it will be removed.
*
* @param string $path
* @return Solarium_Client Provides fluent interface
*/
public function setPath($path)
{
if (substr($path, -1) == '/') $path = substr($path, 0, -1);
return $this->_setOption('path', $path);
}
/**
* Get path option
*
* @return void
*/
public function getPath()
{
return $this->getOption('path');
}
/**
* Set core option
*
* @param string $core
* @return Solarium_Client Provides fluent interface
*/ */
public function setCore($core) protected $_requestBuilders;
{
return $this->_setOption('core', $core);
}
/**
* Get core option
*
* @return string
*/
public function getCore()
{
return $this->getOption('core');
}
/** /**
* Set the adapter * Set the adapter
...@@ -284,29 +184,92 @@ class Solarium_Client extends Solarium_Configurable ...@@ -284,29 +184,92 @@ class Solarium_Client extends Solarium_Configurable
} }
/** /**
* Set adapter options * Register a querytype
*
* You can also use this method to override any existing querytype with a new mapping
* *
* @param array|object $options * @param string $type
* @param string $requestBuilder
* @param string $responseParser
* @return Solarium_Client Provides fluent interface * @return Solarium_Client Provides fluent interface
*/ */
public function setAdapterOptions($options) public function registerQueryType($type, $requestBuilder, $responseParser)
{ {
// covert config object into an array if needed $this->_queryTypes[$type] = array(
if (is_object($options)) { 'requestbuilder' => $requestBuilder,
$options = $options->toArray(); 'responseparser' => $responseParser,
);
return $this;
}
/**
* Get all registered querytypes
*
* @return array
*/
public function getQueryTypes()
{
return $this->_queryTypes;
}
/**
* Creates a request based on a query instance
*
* @todo add caching of request builder?
*
* @param Solarium_Query $query
* @return Solarium_Client_Request
*/
public function createRequest($query)
{
$queryType = $query->getType();
if (!isset($this->_queryTypes[$queryType])) {
throw new Solarium_Exception('No requestbuilder registered for querytype: '. $queryType);
} }
return $this->_setOption('adapteroptions', $options); $requestBuilderClass = $this->_queryTypes[$queryType]['requestbuilder'];
$requestBuilder = new $requestBuilderClass;
return $requestBuilder->build($query);
} }
/** /**
* Get adapteroptions * Creates a result object
* *
* @return array * @param Solarium_Query $query
* @param array Solarium_Client_Response $response
* @return Solarium_Result
*/
public function createResult($query, $response)
{
$resultClass = $query->getResultClass();
return new $resultClass($this, $query, $response);
}
/**
* Execute a query
*
* @param Solarium_Query
* @return Solarium_Result
*/
public function execute($query)
{
$request = $this->createRequest($query);
$response = $this->executeRequest($request);
return $this->createResult($query, $response);
}
/**
* Execute a request and return the response
*
* @param Solarium_Client_Request
* @return Solarium_Client_Response
*/ */
public function getAdapterOptions() public function executeRequest($request)
{ {
return $this->getOption('adapteroptions'); return $this->getAdapter()->execute($request);
} }
/** /**
...@@ -330,7 +293,7 @@ class Solarium_Client extends Solarium_Configurable ...@@ -330,7 +293,7 @@ class Solarium_Client extends Solarium_Configurable
*/ */
public function ping($query) public function ping($query)
{ {
return $this->getAdapter()->ping($query); return $this->execute($query);
} }
/** /**
...@@ -356,7 +319,7 @@ class Solarium_Client extends Solarium_Configurable ...@@ -356,7 +319,7 @@ class Solarium_Client extends Solarium_Configurable
*/ */
public function update($query) public function update($query)
{ {
return $this->getAdapter()->update($query); return $this->execute($query);
} }
/** /**
...@@ -381,6 +344,6 @@ class Solarium_Client extends Solarium_Configurable ...@@ -381,6 +344,6 @@ class Solarium_Client extends Solarium_Configurable
*/ */
public function select($query) public function select($query)
{ {
return $this->getAdapter()->select($query); return $this->execute($query);
} }
} }
\ No newline at end of file
...@@ -56,58 +56,175 @@ ...@@ -56,58 +56,175 @@
*/ */
abstract class Solarium_Client_Adapter extends Solarium_Configurable abstract class Solarium_Client_Adapter extends Solarium_Configurable
{ {
/**
* Default options
*
* The defaults match a standard Solr example instance as distributed by
* the Apache Lucene Solr project.
*
* @var array
*/
protected $_options = array(
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/solr',
'core' => null,
'timeout' => 5,
);
/**
* Initialization hook
*
* In this case the path needs to be cleaned of trailing slashes.
* @see setPath()
*/
protected function _init()
{
foreach ($this->_options AS $name => $value) {
switch ($name) {
case 'path':
$this->setPath($value);
break;
}
}
}
/**
* Set host option
*
* @param string $host This can be a hostname or an IP address
* @return Solarium_Client Provides fluent interface
*/
public function setHost($host)
{
return $this->_setOption('host', $host);
}
/**
* Get host option
*
* @return string
*/
public function getHost()
{
return $this->getOption('host');
}
/**
* Set port option
*
* @param int $port Common values are 80, 8080 and 8983
* @return Solarium_Client Provides fluent interface
*/
public function setPort($port)
{
return $this->_setOption('port', $port);
}
/**
* Get port option
*
* @return int
*/
public function getPort()
{
return $this->getOption('port');
}
/** /**
* Set options * Set path option
* *
* Overrides any existing values * If the path has a trailing slash it will be removed.
*
* @param string $path
* @return Solarium_Client Provides fluent interface
*/
public function setPath($path)
{
if (substr($path, -1) == '/') $path = substr($path, 0, -1);
return $this->_setOption('path', $path);
}
/**
* Get path option
* *
* @param array $options
* @return void * @return void
*/ */
public function setOptions($options) public function getPath()
{ {
$this->_setOptions($options, true); return $this->getOption('path');
} }
/** /**
* Execute a select query * Set core option
* *
* Abstract method to require an implementation inside all adapters. * @param string $core
* If the adapter cannot support this method it should implement a method * @return Solarium_Client Provides fluent interface
* that throws an exception. */
public function setCore($core)
{
return $this->_setOption('core', $core);
}
/**
* Get core option
* *
* @abstract * @return string
* @param Solarium_Query_Select $query
* @return Solarium_Result_Select
*/ */
abstract public function select($query); public function getCore()
{
return $this->getOption('core');
}
/** /**
* Execute a ping query * Set timeout option
* *
* Abstract method to require an implementation inside all adapters. * @param int $timeout
* If the adapter cannot support this method it should implement a method * @return Solarium_Client Provides fluent interface
* that throws an exception. */
public function setTimeout($timeout)
{
return $this->_setOption('timeout', $timeout);
}
/**
* Get timeout option
* *
* @abstract * @return string
* @param Solarium_Query_Ping $query
* @return boolean
*/ */
abstract public function ping($query); public function getTimeout()
{
return $this->getOption('timeout');
}
/** /**
* Execute an update query * Execute a request
* *
* Abstract method to require an implementation inside all adapters. * Abstract method to require an implementation inside all adapters.
* If the adapter cannot support this method it should implement a method
* that throws an exception.
* *
* @abstract * @abstract
* @param Solarium_Query_Update $query * @param Solarium_Client_Request $request
* @return Solarium_Result_Update * @return Solarium_Client_Response
*/
abstract public function execute($request);
/**
* Get the base url for all requests
*
* Based on host, path, port and core options.
*
* @return void
*/ */
abstract public function update($query); public function getBaseUri()
{
$uri = 'http://' . $this->getHost() . ':' . $this->getPort() . $this->getPath() . '/';
$core = $this->getCore();
if (!empty($core)) {
$uri .= $core.'/';
}
return $uri;
}
} }
\ No newline at end of file
...@@ -44,67 +44,24 @@ ...@@ -44,67 +44,24 @@
class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
{ {
/**
* Executes a select query
*
* @param Solarium_Query_Select $query
* @return Solarium_Result_Select
*/
public function select($query)
{
$request = new Solarium_Client_Request_Select($this->_options, $query);
$data = $this->_handleRequest($request);
$response = new Solarium_Client_Response_Select($query, $data);
return $response->getResult();
}
/**
* Executes a ping query
*
* @param Solarium_Query_Ping $query
* @return boolean
*/
public function ping($query)
{
$request = new Solarium_Client_Request_Ping($this->_options, $query);
return (boolean)$this->_handleRequest($request);
}
/**
* Executes an update query
*
* @param Solarium_Query_Update $query
* @return Solarium_Result_Update
*/
public function update($query)
{
$request = new Solarium_Client_Request_Update($this->_options, $query);
$data = $this->_handleRequest($request);
$response = new Solarium_Client_Response_Update($query, $data);
return $response->getResult();
}
/** /**
* Handle Solr communication * Handle Solr communication
* *
* @throws Solarium_Exception * @throws Solarium_Exception
* @param Solarium_Client_Request * @param Solarium_Client_Request
* @return array * @return Solarium_Client_Response
*/ */
protected function _handleRequest($request) public function execute($request)
{ {
$method = $request->getMethod(); $method = $request->getMethod();
$context = stream_context_create( $context = stream_context_create(
array('http' => array( array('http' => array(
'method' => $method, 'method' => $method,
'timeout' => $this->getOption('timeout') 'timeout' => $this->getTimeout()
)) ))
); );
if ($method == Solarium_Client_Request::POST) { if ($method == Solarium_Client_Request::METHOD_POST) {
$data = $request->getRawData(); $data = $request->getRawData();
if (null !== $data) { if (null !== $data) {
stream_context_set_option( stream_context_set_option(
...@@ -113,6 +70,7 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter ...@@ -113,6 +70,7 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
'content', 'content',
$data $data
); );
//TODO header via request->setRawData!!
stream_context_set_option( stream_context_set_option(
$context, $context,
'http', 'http',
...@@ -122,85 +80,16 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter ...@@ -122,85 +80,16 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
} }
} }
$data = @file_get_contents($request->getUri(), false, $context); $uri = $this->getBaseUri() . $request->getUri();
$data = @file_get_contents($uri, false, $context);
// if there is no data and there are no headers it's a total failure, // if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible. // a connection to the host was impossible.
if (false === $data && !isset($http_response_header)) { if (false === $data && !isset($http_response_header)) {
throw new Solarium_Client_HttpException("HTTP request failed"); throw new Solarium_Client_HttpException("HTTP request failed");
} }
$this->_checkHeaders($http_response_header); return new Solarium_Client_Response($data, $http_response_header);
if ($method == Solarium_Client_Request::HEAD) {
// HEAD request has no result data
return true;
} else {
if (false === $data) {
$error = error_get_last();
throw new Solarium_Exception($error['message']);
}
return $this->_jsonDecode($data);
}
} }
/**
* Check HTTP headers
*
* The status header is parsed, an exception will be thrown for an error
* code.
*
* @throws Solarium_Client_HttpException
* @param array $headers
* @return void
*/
protected function _checkHeaders($headers)
{
// get the status header
$statusHeader = null;
foreach ($headers AS $header) {
if (substr($header, 0, 4) == 'HTTP') {
$statusHeader = $header;
break;
}
}
if (null == $statusHeader) {
throw new Solarium_Client_HttpException("No HTTP status found");
}
// parse header like "$statusInfo[1]" into code and message
// $statusInfo[1] = the HTTP response code
// $statusInfo[2] = the response message
$statusInfo = explode(' ', $statusHeader, 3);
// check status for error (range of 400 and 500)
$statusNum = floor($statusInfo[1] / 100);
if ($statusNum == 4 || $statusNum == 5) {
throw new Solarium_Client_HttpException(
$statusInfo[2],
$statusInfo[1]
);
}
}
/**
* Decode json response data
*
* @throws Solarium_Exception
* @param string $data
* @return string
*/
protected function _jsonDecode($data)
{
$data = json_decode($data, true);
if (null === $data) {
throw new Solarium_Exception(
'Solr JSON response could not be decoded'
);
}
return $data;
}
} }
\ No newline at end of file
This diff is collapsed.
<?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 Client
*/
/**
* Class for building Solarium client requests
*
* @package Solarium
* @subpackage Client
*/
abstract class Solarium_Client_RequestBuilder
{
/**
* Render a param with localParams
*
* LocalParams can be use in various Solr GET params.
* @link http://wiki.apache.org/solr/LocalParams
*
* @param string $value
* @param array $localParams in key => value format
* @return string with Solr localparams syntax
*/
public function renderLocalParams($value, $localParams = array())
{
$params = '';
foreach ($localParams AS $paramName => $paramValue) {
if (empty($paramValue)) continue;
if (is_array($paramValue)) {
$paramValue = implode($paramValue, ',');
}
$params .= $paramName . '=' . $paramValue . ' ';
}
if ($params !== '') {
$value = '{!' . trim($params) . '}' . $value;
}
return $value;
}
}
\ No newline at end of file
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
* @package Solarium * @package Solarium
* @subpackage Client * @subpackage Client
*/ */
class Solarium_Client_Request_Ping extends Solarium_Client_Request class Solarium_Client_RequestBuilder_Ping extends Solarium_Client_RequestBuilder
{ {
/** /**
......
<?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 Client
*/
/**
* Build a select request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select extends Solarium_Client_RequestBuilder
{
/**
* Build request for a select query
*
* @param Solarium_Query_Select $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());
// add basic params to request
$request->addParam('q', $query->getQuery());
$request->addParam('start', $query->getStart());
$request->addParam('rows', $query->getRows());
$request->addParam('fl', implode(',', $query->getFields()));
$request->addParam('wt', 'json');
// add sort fields to request
$sort = array();
foreach ($query->getSortFields() AS $field => $order) {
$sort[] = $field . ' ' . $order;
}
if (count($sort) !== 0) {
$request->addParam('sort', implode(',', $sort));
}
// add filterqueries to request
$filterQueries = $query->getFilterQueries();
if (count($filterQueries) !== 0) {
foreach ($filterQueries AS $filterQuery) {
$fq = $this->renderLocalParams(
$filterQuery->getQuery(),
array('tag' => $filterQuery->getTags())
);
$request->addParam('fq', $fq);
}
}
// add components to request
$types = $query->getComponentTypes();
foreach ($query->getComponents() as $component) {
$componentBuilderClass = $types[$component->getType()]['requestbuilder'];
if (!empty($componentBuilderClass)) {
// todo add caching?
$componentBuilder = new $componentBuilderClass;
$request = $componentBuilder->build($component, $request);
}
}
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
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component dismax to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_DisMax
{
/**
* Add request settings for Dismax
*
* @param Solarium_Query_Select_Component_Dismax $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable dismax
$request->addParam('defType', 'dismax');
$request->addParam('q.alt', $component->getQueryAlternative());
$request->addParam('qf', $component->getQueryFields());
$request->addParam('mm', $component->getMinimumMatch());
$request->addParam('pf', $component->getPhraseFields());
$request->addParam('ps', $component->getPhraseSlop());
$request->addParam('qs', $component->getQueryPhraseSlop());
$request->addParam('tie', $component->getTie());
$request->addParam('bq', $component->getBoostQuery());
$request->addParam('bf', $component->getBoostFunctions());
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
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component Highlighting to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_Highlighting
{
/**
* Add request settings for Highlighting
*
* @param Solarium_Query_Select_Component_Highlighting $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable highlighting
$request->addParam('hl', 'true');
$request->addParam('hl.fl', $component->getFields());
$request->addParam('hl.snippets', $component->getSnippets());
$request->addParam('hl.fragsize', $component->getFragSize());
$request->addParam('hl.mergeContiguous', $component->getMergeContiguous());
$request->addParam('hl.requireFieldMatch', $component->getRequireFieldMatch());
$request->addParam('hl.maxAnalyzedChars', $component->getMaxAnalyzedChars());
$request->addParam('hl.alternateField', $component->getAlternateField());
$request->addParam('hl.maxAlternateFieldLength', $component->getMaxAlternateFieldLength());
$request->addParam('hl.formatter', $component->getFormatter());
$request->addParam('hl.simple.pre', $component->getSimplePrefix());
$request->addParam('hl.simple.post', $component->getSimplePostfix());
$request->addParam('hl.fragmenter', $component->getFragmenter());
$request->addParam('hl.fragListBuilder', $component->getFragListBuilder());
$request->addParam('hl.fragmentsBuilder', $component->getFragmentsBuilder());
$request->addParam('hl.useFastVectorHighlighter', $component->getUseFastVectorHighlighter());
$request->addParam('hl.usePhraseHighlighter', $component->getUsePhraseHighlighter());
$request->addParam('hl.highlightMultiTerm', $component->getHighlightMultiTerm());
$request->addParam('hl.regex.slop', $component->getRegexSlop());
$request->addParam('hl.regex.pattern', $component->getRegexPattern());
$request->addParam('hl.regex.maxAnalyzedChars', $component->getRegexMaxAnalyzedChars());
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
*
* @package Solarium
* @subpackage Client
*/
/**
* Add select component morelikethis to the request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis
{
/**
* Add request settings for morelikethis
*
* @param Solarium_Query_Select_Component_MoreLikeThis $component
* @param Solarium_Client_Request $request
* @return Solarium_Client_Request
*/
public function build($component, $request)
{
// enable morelikethis
$request->addParam('mlt','true');
$request->addParam('mlt.fl', $component->getFields());
$request->addParam('mlt.mintf', $component->getMinimumTermFrequency());
$request->addParam('mlt.mindf', $component->getMinimumDocumentFrequency());
$request->addParam('mlt.minwl', $component->getMinimumWordLength());
$request->addParam('mlt.maxwl', $component->getMaximumWordLength());
$request->addParam('mlt.maxqt', $component->getMaximumQueryTerms());
$request->addParam('mlt.maxntp', $component->getMaximumNumberOfTokens());
$request->addParam('mlt.boost', $component->getBoost());
$request->addParam('mlt.qf', $component->getQueryFields());
$request->addParam('mlt.count', $component->getCount());
return $request;
}
}
\ No newline at end of file
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
* @package Solarium * @package Solarium
* @subpackage Client * @subpackage Client
*/ */
class Solarium_Client_Request_Update extends Solarium_Client_Request class Solarium_Client_RequestBuilder_Update extends Solarium_Client_RequestBuilder
{ {
/** /**
......
...@@ -36,63 +36,124 @@ ...@@ -36,63 +36,124 @@
*/ */
/** /**
* Base class for handling Solr HTTP responses * Class for describing a response
*
* Most {@link Solarium_Client_Adapter} implementations will use HTTP for
* communicating with Solr. While the HTTP part is adapter-specific, the parsing
* of the response into Solarium_Result classes is not. This abstract class is
* the base for several response handlers that do just that for the various
* querytypes.
* *
* @package Solarium * @package Solarium
* @subpackage Client * @subpackage Client
*/ */
abstract class Solarium_Client_Response class Solarium_Client_Response
{ {
/** /**
* Query instance * Headers
* *
* The query that was used for executing a request that led to this * @var array
* response. The query holds important settings for generating the right */
* result, like the resultclass and documentclass settings. protected $_headers;
/**
* Body
* *
* @var Solarium_Query * @var string
*/ */
protected $_query; protected $_body;
/** /**
* Response data * HTTP response code
* *
* A (json)decoded HTTP response body data array. * @var int
*/
protected $_statusCode;
/**
* HTTP response message
* *
* @var array * @var string
*/ */
protected $_data; protected $_statusMessage;
/** /**
* Constructor * Constructor
* *
* @param Solarium_Query $query Query instance that was used for the request * @param string $body
* @param array $data Decoded data array of the HTTP response * @param array $headers
*/ */
public function __construct($query, $data = null) public function __construct($body, $headers = array())
{ {
$this->_query = $query; $this->_body = $body;
$this->_data = $data; $this->_headers = $headers;
$this->_setHeaders($headers);
} }
/** /**
* Get a Solarium_Result instance for the response * Get body data
* *
* When this method is called the actual response parsing is started. * @return string
*/
public function getBody()
{
return $this->_body;
}
/**
* Get response headers
* *
* @internal Must be implemented in descendents because this parsing is * @return array
* query specific. */
public function getHeaders()
{
return $this->_headers;
}
/**
* Get status code
* *
* @abstract * @return int
* @return mixed
*/ */
abstract function getResult(); public function getStatusCode()
{
return $this->_statusCode;
}
/**
* Get status message
*
* @return string
*/
public function getStatusMessage()
{
return $this->_statusMessage;
}
/**
* Set headers
*
* @param array $headers
* @return void
*/
public function _setHeaders($headers)
{
$this->_headers = $headers;
// get the status header
$statusHeader = null;
foreach ($headers AS $header) {
if (substr($header, 0, 4) == 'HTTP') {
$statusHeader = $header;
break;
}
}
if (null == $statusHeader) {
throw new Solarium_Client_HttpException("No HTTP status found");
}
// parse header like "$statusInfo[1]" into code and message
// $statusInfo[1] = the HTTP response code
// $statusInfo[2] = the response message
$statusInfo = explode(' ', $statusHeader, 3);
$this->_statusCode = $statusInfo[1];
$this->_statusMessage = $statusInfo[2];
}
} }
\ 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 Client
*/
/**
* Base class for handling Solr response data
*
* Most {@link Solarium_Client_Adapter} implementations will use HTTP for
* communicating with Solr. While the HTTP part is adapter-specific, the parsing
* of the response into Solarium_Result classes is not. This abstract class is
* the base for several response handlers that do just that for the various
* querytypes.
*
* @package Solarium
* @subpackage Client
*/
abstract class Solarium_Client_ResponseParser
{
/**
* Get a Solarium_Result instance for the given data
*
* When this method is called the actual response parsing is started.
*
* @internal Must be implemented in descendents because this parsing is
* query specific.
*
* @abstract
*
* @param Solarium_Result $result
* @return mixed
*/
abstract function parse($result);
}
\ 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 Client
*/
/**
* Parse select response data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select extends Solarium_Client_ResponseParser
{
/**
* Get result data for the response
*
* @param Solarium_Result_Select $result
* @return array
*/
public function parse($result)
{
$data = $result->getData();
$query = $result->getQuery();
// reset arrays
$this->_components = array();
// create document instances
$documentClass = $query->getOption('documentclass');
$documents = array();
if (isset($data['response']['docs'])) {
foreach ($data['response']['docs'] AS $doc) {
$fields = (array)$doc;
$documents[] = new $documentClass($fields);
}
}
// component results
$components = array();
$types = $query->getComponentTypes();
foreach ($query->getComponents() as $component) {
$componentParserClass = $types[$component->getType()]['responseparser'];
if (!empty($componentParserClass)) {
// todo add caching?
$componentParser = new $componentParserClass;
$components[$component->getType()] = $componentParser->parse($query, $component, $data);
}
}
return array(
'status' => $data['responseHeader']['status'],
'querytime' => $data['responseHeader']['QTime'],
'numfound' => $data['response']['numFound'],
'documents' => $documents,
'components' => $components,
);
}
}
\ 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 Client
*/
/**
* Parse select component Highlighting result from the data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select_Component_Highlighting
{
/**
* Parse result data into result objects
*
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_Highlighting $highlighting
* @param array $data
* @return Solarium_Result_Select_Highlighting
*/
public function parse($query, $highlighting, $data)
{
$results = array();
if (isset($data['highlighting'])) {
$highlightResults = $data['highlighting'];
foreach ($highlightResults AS $key => $result) {
$results[$key] = new Solarium_Result_Select_Highlighting_Result(
$result
);
}
}
return new Solarium_Result_Select_Highlighting($results);
}
}
\ 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 Client
*/
/**
* Parse select component MoreLikeThis result from the data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_Select_Component_MoreLikeThis
{
/**
* Parse result data into result objects
*
* @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_MoreLikeThis $moreLikeThis
* @param array $data
* @return Solarium_Result_Select_MoreLikeThis
*/
public function parse($query, $moreLikeThis, $data)
{
$results = array();
if (isset($data['moreLikeThis'])) {
$documentClass = $query->getOption('documentclass');
$searchResults = $data['moreLikeThis'];
foreach ($searchResults AS $key => $result) {
// create document instances
$docs = array();
foreach ($result['docs'] AS $fields) {
$docs[] = new $documentClass($fields);
}
$results[$key] = new Solarium_Result_Select_MoreLikeThis_Result(
$result['numFound'],
$result['maxScore'],
$docs
);
}
}
return new Solarium_Result_Select_MoreLikeThis($results);
}
}
\ No newline at end of file
...@@ -38,29 +38,27 @@ ...@@ -38,29 +38,27 @@
/** /**
* Parse update response data * Parse update response data
* *
* Will create a result object based on response data of the type
* {@Solarium_Result_Update} (or your own resultclass setting)
*
* @package Solarium * @package Solarium
* @subpackage Client * @subpackage Client
*/ */
class Solarium_Client_Response_Update extends Solarium_Client_Response class Solarium_Client_ResponseParser_Update extends Solarium_Client_ResponseParser
{ {
/** /**
* Get a result instance for the response * Parse response data
*
* When this method is called the actual response parsing is done.
* *
* @return mixed * @param Solarium_Result_Select $result
* @return array
*/ */
public function getResult() public function parse($result)
{ {
$resultClass = $this->_query->getOption('resultclass'); $data = $result->getData();
$query = $result->getQuery();
$resultClass = $query->getResultClass();
return new $resultClass( return array(
$this->_data['responseHeader']['status'], 'status' => $data['responseHeader']['status'],
$this->_data['responseHeader']['QTime'] 'querytime' => $data['responseHeader']['QTime'],
); );
} }
......
...@@ -58,7 +58,7 @@ class Solarium_Configurable ...@@ -58,7 +58,7 @@ class Solarium_Configurable
* Constructor * Constructor
* *
* If options are passed they will be merged with {@link $_options} using * If options are passed they will be merged with {@link $_options} using
* the {@link _setOptions()} method. * the {@link setOptions()} method.
* *
* After handling the options the {@link _init()} method is called. * After handling the options the {@link _init()} method is called.
* *
...@@ -68,7 +68,7 @@ class Solarium_Configurable ...@@ -68,7 +68,7 @@ class Solarium_Configurable
*/ */
public function __construct($options = null) public function __construct($options = null)
{ {
$this->_setOptions($options); $this->setOptions($options);
$this->_init(); $this->_init();
} }
...@@ -87,7 +87,7 @@ class Solarium_Configurable ...@@ -87,7 +87,7 @@ class Solarium_Configurable
* *
* @return void * @return void
*/ */
protected function _setOptions($options, $overwrite = false) public function setOptions($options, $overwrite = false)
{ {
if (null !== $options) { if (null !== $options) {
// first convert to array if needed // first convert to array if needed
...@@ -105,6 +105,9 @@ class Solarium_Configurable ...@@ -105,6 +105,9 @@ class Solarium_Configurable
} else { } else {
$this->_options = array_merge($this->_options, $options); $this->_options = array_merge($this->_options, $options);
} }
// re-init for new options
$this->_init();
} }
} }
......
...@@ -41,8 +41,15 @@ ...@@ -41,8 +41,15 @@
* @package Solarium * @package Solarium
* @subpackage Query * @subpackage Query
*/ */
class Solarium_Query extends Solarium_Configurable abstract class Solarium_Query extends Solarium_Configurable
{ {
/**
* Get type for this query
*
* @return string
*/
abstract public function getType();
/** /**
* Set handler option * Set handler option
......
...@@ -48,6 +48,16 @@ ...@@ -48,6 +48,16 @@
class Solarium_Query_Ping extends Solarium_Query class Solarium_Query_Ping extends Solarium_Query
{ {
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_PING;
}
/** /**
* Default options * Default options
* *
......
...@@ -54,6 +54,24 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -54,6 +54,24 @@ class Solarium_Query_Select extends Solarium_Query
const SORT_DESC = 'desc'; const SORT_DESC = 'desc';
const SORT_ASC = 'asc'; const SORT_ASC = 'asc';
/**
* Query components
*/
const COMPONENT_FACETSET = 'facetset';
const COMPONENT_DISMAX = 'dismax';
const COMPONENT_MORELIKETHIS = 'morelikethis';
const COMPONENT_HIGHLIGHTING = 'highlighting';
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_SELECT;
}
/** /**
* Default options * Default options
* *
...@@ -69,6 +87,34 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -69,6 +87,34 @@ class Solarium_Query_Select extends Solarium_Query
'fields' => '*,score', 'fields' => '*,score',
); );
/**
* Default select query component types
*
* @var array
*/
protected $_componentTypes = array(
self::COMPONENT_FACETSET => array(
'component' => 'Solarium_Query_Select_Component_FacetSet',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_FacetSet',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_FacetSet',
),
self::COMPONENT_DISMAX => array(
'component' => 'Solarium_Query_Select_Component_DisMax',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_DisMax',
'responseparser' => null,
),
self::COMPONENT_MORELIKETHIS => array(
'component' => 'Solarium_Query_Select_Component_MoreLikeThis',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_MoreLikeThis',
),
self::COMPONENT_HIGHLIGHTING => array(
'component' => 'Solarium_Query_Select_Component_Highlighting',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Select_Component_Highlighting',
'responseparser' => 'Solarium_Client_ResponseParser_Select_Component_Highlighting',
),
);
/** /**
* Fields to fetch * Fields to fetch
* *
...@@ -537,6 +583,36 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -537,6 +583,36 @@ class Solarium_Query_Select extends Solarium_Query
$this->addFilterQueries($filterQueries); $this->addFilterQueries($filterQueries);
} }
/**
* Get all registered component types
*
* @return array
*/
public function getComponentTypes()
{
return $this->_componentTypes;
}
/**
* Register a component type
*
* @param string $key
* @param string $component
* @param string $requestBuilder
* @param string $responseParser
* @return Solarium_Query Provides fluent interface
*/
public function registerComponentType($key, $component, $requestBuilder=null, $responseParser=null)
{
$this->_componentTypes[$key] = array(
'component' => $component,
'requestbuilder' => $requestBuilder,
'responseparser' => $responseParser,
);
return $this;
}
/** /**
* Get all registered components * Get all registered components
* *
...@@ -566,16 +642,16 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -566,16 +642,16 @@ class Solarium_Query_Select extends Solarium_Query
if ($autoload == true) { if ($autoload == true) {
switch ($key) { switch ($key) {
case Solarium_Query_Select_Component::MORELIKETHIS: case Solarium_Query_Select::COMPONENT_MORELIKETHIS:
$className = 'Solarium_Query_Select_Component_MoreLikeThis'; $className = 'Solarium_Query_Select_Component_MoreLikeThis';
break; break;
case Solarium_Query_Select_Component::FACETSET: case Solarium_Query_Select::COMPONENT_FACETSET:
$className = 'Solarium_Query_Select_Component_FacetSet'; $className = 'Solarium_Query_Select_Component_FacetSet';
break; break;
case Solarium_Query_Select_Component::DISMAX: case Solarium_Query_Select::COMPONENT_DISMAX:
$className = 'Solarium_Query_Select_Component_DisMax'; $className = 'Solarium_Query_Select_Component_DisMax';
break; break;
case Solarium_Query_Select_Component::HIGHLIGHTING: case Solarium_Query_Select::COMPONENT_HIGHLIGHTING:
$className = 'Solarium_Query_Select_Component_Highlighting'; $className = 'Solarium_Query_Select_Component_Highlighting';
break; break;
default: default:
...@@ -642,7 +718,7 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -642,7 +718,7 @@ class Solarium_Query_Select extends Solarium_Query
*/ */
public function getMoreLikeThis() public function getMoreLikeThis()
{ {
return $this->getComponent(Solarium_Query_Select_Component::MORELIKETHIS, true); return $this->getComponent(Solarium_Query_Select::COMPONENT_MORELIKETHIS, true);
} }
/** /**
...@@ -654,7 +730,7 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -654,7 +730,7 @@ class Solarium_Query_Select extends Solarium_Query
*/ */
public function getFacetSet() public function getFacetSet()
{ {
return $this->getComponent(Solarium_Query_Select_Component::FACETSET, true); return $this->getComponent(Solarium_Query_Select::COMPONENT_FACETSET, true);
} }
/** /**
...@@ -666,7 +742,7 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -666,7 +742,7 @@ class Solarium_Query_Select extends Solarium_Query
*/ */
public function getDisMax() public function getDisMax()
{ {
return $this->getComponent(Solarium_Query_Select_Component::DISMAX, true); return $this->getComponent(Solarium_Query_Select::COMPONENT_DISMAX, true);
} }
/** /**
...@@ -678,7 +754,7 @@ class Solarium_Query_Select extends Solarium_Query ...@@ -678,7 +754,7 @@ class Solarium_Query_Select extends Solarium_Query
*/ */
public function getHighlighting() public function getHighlighting()
{ {
return $this->getComponent(Solarium_Query_Select_Component::HIGHLIGHTING, true); return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING, true);
} }
/** /**
......
...@@ -44,14 +44,6 @@ ...@@ -44,14 +44,6 @@
class Solarium_Query_Select_Component extends Solarium_Configurable class Solarium_Query_Select_Component extends Solarium_Configurable
{ {
/**
* Component types
*/
const MORELIKETHIS = 'morelikethis';
const FACETSET = 'facetset';
const DISMAX = 'dismax';
const HIGHLIGHTING = 'highlighting';
/** /**
* Component type * Component type
* *
......
...@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_DisMax extends Solarium_Query_Select_Compo ...@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_DisMax extends Solarium_Query_Select_Compo
* *
* @var string * @var string
*/ */
protected $_type = self::DISMAX; protected $_type = Solarium_Query_Select::COMPONENT_DISMAX;
/** /**
* Set QueryAlternative option * Set QueryAlternative option
......
...@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com ...@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_FacetSet extends Solarium_Query_Select_Com
* *
* @var string * @var string
*/ */
protected $_type = self::FACETSET; protected $_type = Solarium_Query_Select::COMPONENT_FACETSET;
/** /**
* Default options * Default options
......
...@@ -56,7 +56,7 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select ...@@ -56,7 +56,7 @@ class Solarium_Query_Select_Component_Highlighting extends Solarium_Query_Select
* *
* @var string * @var string
*/ */
protected $_type = self::HIGHLIGHTING; protected $_type = Solarium_Query_Select::COMPONENT_HIGHLIGHTING;
/** /**
* Set fields option * Set fields option
......
...@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_MoreLikeThis extends Solarium_Query_Select ...@@ -51,7 +51,7 @@ class Solarium_Query_Select_Component_MoreLikeThis extends Solarium_Query_Select
* *
* @var string * @var string
*/ */
protected $_type = self::MORELIKETHIS; protected $_type = Solarium_Query_Select::COMPONENT_MORELIKETHIS;
/** /**
* Set fields option * Set fields option
......
...@@ -48,6 +48,16 @@ ...@@ -48,6 +48,16 @@
class Solarium_Query_Update extends Solarium_Query class Solarium_Query_Update extends Solarium_Query
{ {
/**
* Get type for this query
*
* @return string
*/
public function getType()
{
return Solarium_Client::QUERYTYPE_UPDATE;
}
/** /**
* Default options * Default options
* *
......
<?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
*/
/**
* Query result
*
* This base class provides access to the response and decoded data. If you need more functionality
* like resultset parsing use one of the subclasses
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result
{
/**
* Response object
*
* @var Solarium_Client_Response
*/
protected $_response;
/**
* Decode response data
*
* This is lazy loaded, see getData()
*
* @var array
*/
protected $_data;
/**
* Query used for this request
*
* @var Solarium_Query
*/
protected $_query;
/**
* @var Solarium_Client
*/
protected $_client;
/**
* Constructor
*
* @param Solarium_Client $client
* @param Solarium_Query $query
* @param Solarium_Client_Response $response
* @return void
*/
public function __construct($client, $query, $response)
{
$this->_client = $client;
$this->_query = $query;
$this->_response = $response;
// check status for error (range of 400 and 500)
$statusNum = floor($response->getStatusCode() / 100);
if ($statusNum == 4 || $statusNum == 5) {
throw new Solarium_Client_HttpException(
$response->getStatusMessage(),
$response->getStatusCode()
);
}
}
/**
* Get response object
*
* This is the raw HTTP response object, not the parsed data!
*
* @return Solarium_Client_Response
*/
public function getResponse()
{
return $this->_response;
}
/**
* Get query instance
*
* @return Solarium_Query
*/
public function getQuery()
{
return $this->_query;
}
/**
* Get Solr response data
*
* Included a lazy loading mechanism: JSON body data is decoded on first use and then saved for reuse.
*
* @return array
*/
public function getData()
{
if (null == $this->_data) {
$this->_data = json_decode($this->_response->getBody(), true);
if (null === $this->_data) {
throw new Solarium_Exception(
'Solr JSON response could not be decoded'
);
}
}
return $this->_data;
}
}
\ 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
*/
/**
* QueryType result
*
* TODO intro
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_QueryType extends Solarium_Result
{
/**
* Lazy load parsing indicator
*
* @var bool
*/
protected $_parsed = false;
/**
* Parse response into result objects
*
* Only runs once
*
* @return void
*/
protected function _parseResponse()
{
if (!$this->_parsed) {
$queryType = $this->_query->getType();
$queryTypes = $this->_client->getQueryTypes();
if (!isset($queryTypes[$queryType])) {
throw new Solarium_Exception('No responseparser registered for querytype: '. $queryType);
}
$responseParserClass = $queryTypes[$queryType]['responseparser'];
$responseParser = new $responseParserClass;
$this->_mapData($responseParser->parse($this));
}
}
/**
* Map parser data into properties
*
* @param array $mapData
* @return void
*/
protected function _mapData($mapData)
{
foreach($mapData AS $key => $data) {
$this->{'_'.$key} = $data;
}
}
}
\ No newline at end of file
...@@ -58,7 +58,7 @@ ...@@ -58,7 +58,7 @@
* @package Solarium * @package Solarium
* @subpackage Result * @subpackage Result
*/ */
class Solarium_Result_Select extends Solarium_Result_Query class Solarium_Result_Select extends Solarium_Result_QueryType
implements IteratorAggregate, Countable implements IteratorAggregate, Countable
{ {
...@@ -69,7 +69,7 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -69,7 +69,7 @@ class Solarium_Result_Select extends Solarium_Result_Query
* *
* @var int * @var int
*/ */
protected $_numFound; protected $_numfound;
/** /**
* Document instances array * Document instances array
...@@ -78,43 +78,11 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -78,43 +78,11 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
protected $_documents; protected $_documents;
/**
* Facet result instances
*
* @var array
*/
protected $_facets;
/** /**
* Component results * Component results
*/ */
protected $_components; protected $_components;
/**
* Constructor
*
* This is the only point where data can be set in this immutable value
* object.
*
* @param int $status
* @param int $queryTime
* @param int $numFound
* @param array $documents
* @param array $facets
* @param array $components
* @return void
*/
public function __construct($status, $queryTime, $numFound, $documents,
$facets, $components)
{
$this->_status = $status;
$this->_queryTime = $queryTime;
$this->_numFound = $numFound;
$this->_documents = $documents;
$this->_facets = $facets;
$this->_components = $components;
}
/** /**
* get Solr numFound * get Solr numFound
* *
...@@ -125,7 +93,9 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -125,7 +93,9 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getNumFound() public function getNumFound()
{ {
return $this->_numFound; $this->_parseResponse();
return $this->_numfound;
} }
/** /**
...@@ -135,32 +105,9 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -135,32 +105,9 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getDocuments() public function getDocuments()
{ {
return $this->_documents; $this->_parseResponse();
}
/**
* Get all facet results
*
* @return array
*/
public function getFacets()
{
return $this->_facets;
}
/** return $this->_documents;
* Get a facet result by key
*
* @param string $key
* @return Solarium_Result_Select_Facet
*/
public function getFacet($key)
{
if (isset($this->_facets[$key])) {
return $this->_facets[$key];
} else {
return null;
}
} }
/** /**
...@@ -170,6 +117,8 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -170,6 +117,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getIterator() public function getIterator()
{ {
$this->_parseResponse();
return new ArrayIterator($this->_documents); return new ArrayIterator($this->_documents);
} }
...@@ -180,6 +129,8 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -180,6 +129,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function count() public function count()
{ {
$this->_parseResponse();
return count($this->_documents); return count($this->_documents);
} }
...@@ -190,6 +141,8 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -190,6 +141,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getComponents() public function getComponents()
{ {
$this->_parseResponse();
return $this->_components; return $this->_components;
} }
...@@ -201,6 +154,8 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -201,6 +154,8 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getComponent($key) public function getComponent($key)
{ {
$this->_parseResponse();
if (isset($this->_components[$key])) { if (isset($this->_components[$key])) {
return $this->_components[$key]; return $this->_components[$key];
} else { } else {
...@@ -217,7 +172,7 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -217,7 +172,7 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getMoreLikeThis() public function getMoreLikeThis()
{ {
return $this->getComponent(Solarium_Query_Select_Component::MORELIKETHIS); return $this->getComponent(Solarium_Query_Select::COMPONENT_MORELIKETHIS);
} }
/** /**
...@@ -229,6 +184,18 @@ class Solarium_Result_Select extends Solarium_Result_Query ...@@ -229,6 +184,18 @@ class Solarium_Result_Select extends Solarium_Result_Query
*/ */
public function getHighlighting() public function getHighlighting()
{ {
return $this->getComponent(Solarium_Query_Select_Component::HIGHLIGHTING); return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING);
}
/**
* Get facetset component result
*
* This is a convenience method that maps presets to getComponent
*
* @return Solarium_Result_Select_Component_FacetSet
*/
public function getFacetSet()
{
return $this->getComponent(Solarium_Query_Select::COMPONENT_HIGHLIGHTING);
} }
} }
\ No newline at end of file
...@@ -36,69 +36,74 @@ ...@@ -36,69 +36,74 @@
*/ */
/** /**
* Query result * Select component facetset result
*
* This base class provides methods for two common result values: status and
* querytime.
* *
* @package Solarium * @package Solarium
* @subpackage Result * @subpackage Result
*/ */
class Solarium_Result_Query class Solarium_Result_Select_FacetSet implements IteratorAggregate, Countable
{ {
/** /**
* Status code returned by Solr * Facet array
* *
* @var int * @var array
*/ */
protected $_status; protected $_facets;
/** /**
* Solr index queryTime * Constructor
*
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
* *
* @var int * @param array $facets
* @return void
*/ */
protected $_queryTime; public function __construct($facets)
{
$this->_facets = $facets;
}
/** /**
* Constructor * Get a facet by key
* *
* @param int $status * @param mixed $key
* @param int $queryTime * @return mixed
* @return void
*/ */
public function __construct($status, $queryTime) public function getFacet($key)
{ {
$this->_status = $status; if (isset($this->_facets[$key])) {
$this->_queryTime = $queryTime; return $this->_facets[$key];
} else {
return null;
}
} }
/** /**
* Get Solr status code * Get all results
* *
* This is not the HTTP status code! The normal value for success is 0. * @return array
*/
public function getFacets()
{
return $this->_facets;
}
/**
* IteratorAggregate implementation
* *
* @return int * @return ArrayIterator
*/ */
public function getStatus() public function getIterator()
{ {
return $this->_status; return new ArrayIterator($this->_facets);
} }
/** /**
* Get Solr query time * Countable implementation
* *
* This doesn't include things like the HTTP responsetime. Purely the Solr
* query execution time.
*
* @return int * @return int
*/ */
public function getQueryTime() public function count()
{ {
return $this->_queryTime; return count($this->_facets);
} }
} }
\ No newline at end of file
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
* @package Solarium * @package Solarium
* @subpackage Result * @subpackage Result
*/ */
class Solarium_Result_Update extends Solarium_Result_Query class Solarium_Result_Update extends Solarium_Result
{ {
} }
\ 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