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

Merge pull request #38 from Gasol/mlt_support

Mlt handler support
parents a5b0355f 973a2347
...@@ -71,6 +71,7 @@ class Solarium_Client extends Solarium_Configurable ...@@ -71,6 +71,7 @@ class Solarium_Client extends Solarium_Configurable
* Querytype ping * Querytype ping
*/ */
const QUERYTYPE_PING = 'ping'; const QUERYTYPE_PING = 'ping';
const QUERYTYPE_MORELIKETHIS = 'mlt';
/** /**
* Default options * Default options
...@@ -102,6 +103,11 @@ class Solarium_Client extends Solarium_Configurable ...@@ -102,6 +103,11 @@ class Solarium_Client extends Solarium_Configurable
'requestbuilder' => 'Solarium_Client_RequestBuilder_Ping', 'requestbuilder' => 'Solarium_Client_RequestBuilder_Ping',
'responseparser' => 'Solarium_Client_ResponseParser_Ping' 'responseparser' => 'Solarium_Client_ResponseParser_Ping'
), ),
self::QUERYTYPE_MORELIKETHIS => array(
'query' => 'Solarium_Query_MoreLikeThis',
'requestbuilder' => 'Solarium_Client_RequestBuilder_MoreLikeThis',
'responseparser' => 'Solarium_Client_ResponseParser_MoreLikeThis'
),
); );
/** /**
...@@ -557,6 +563,30 @@ class Solarium_Client extends Solarium_Configurable ...@@ -557,6 +563,30 @@ class Solarium_Client extends Solarium_Configurable
{ {
return $this->execute($query); return $this->execute($query);
} }
/**
* Execute a MoreLikeThis query
*
* Example usage:
* <code>
* $client = new Solarium_Client;
* $query = $client->createMoreLikeThis();
* $result = $client->moreLikeThis($query);
* </code>
*
* @see Solarium_Query_Select
* @see Solarium_Result_Select
*
* @internal This is a convenience method that forwards the query to the
* execute method, thus allowing for an easy to use and clean API.
*
* @param Solarium_Query_Select $query
* @return Solarium_Result_Select
*/
public function moreLikeThis($query)
{
return $this->execute($query);
}
/** /**
* Create a query instance * Create a query instance
...@@ -594,6 +624,17 @@ class Solarium_Client extends Solarium_Configurable ...@@ -594,6 +624,17 @@ class Solarium_Client extends Solarium_Configurable
{ {
return $this->createQuery(self::QUERYTYPE_SELECT, $options); return $this->createQuery(self::QUERYTYPE_SELECT, $options);
} }
/**
* Create a MoreLikeThis query instance
*
* @param mixed $options
* @return Solarium_Query_Select
*/
public function createMoreLikeThis($options = null)
{
return $this->createQuery(self::QUERYTYPE_MORELIKETHIS, $options);
}
/** /**
* Create an update query instance * Create an update query instance
......
...@@ -65,12 +65,12 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter ...@@ -65,12 +65,12 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
{ {
$uri = $this->getBaseUri() . $request->getUri(); $uri = $this->getBaseUri() . $request->getUri();
$method = $request->getMethod(); $method = $request->getMethod();
$options = array( $options = $this->_createOptions($request);
'timeout' => $this->getTimeout()
);
if ($method == Solarium_Client_Request::METHOD_POST) { if ($method == Solarium_Client_Request::METHOD_POST) {
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; if (!isset($options['headers']['Content-Type'])) {
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
}
$httpResponse = http_post_data($uri, $request->getRawData(), $options); $httpResponse = http_post_data($uri, $request->getRawData(), $options);
} else if ($method == Solarium_Client_Request::METHOD_GET) { } else if ($method == Solarium_Client_Request::METHOD_GET) {
$httpResponse = http_get($uri, $options); $httpResponse = http_get($uri, $options);
...@@ -93,6 +93,28 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter ...@@ -93,6 +93,28 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
return array($data, $headers); return array($data, $headers);
} }
/**
* Create http request options from request.
*
* @link http://php.net/manual/en/http.request.options.php
*
* @param Solarium_Client_Request $request
* @return array
*/
protected function _createOptions($request)
{
$options = array(
'timeout' => $this->getTimeout()
);
foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine);
if ($header = trim($header)) {
$options['headers'][$header] = trim($value);
}
}
return $options;
}
/** /**
* Check result of a request * Check result of a request
* *
......
<?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
*/
/**
* Build a MoreLikeThis request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_MoreLikeThis extends Solarium_Client_RequestBuilder
{
/**
* Build request for a MoreLikeThis query
*
* @param Solarium_Query_Select $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());
if ($query->isStream()) {
$request->setRawData($query->getQuery());
$request->setMethod(Solarium_Client_Request::METHOD_POST);
$request->addHeader('Content-Type: text/plain; charset=utf-8');
} else {
$request->addParam('q', $query->getQuery());
}
// add basic params to request
$request->addParam('wt', 'json');
$request->addParam('rows', $query->getRows());
$request->addParam('fl', implode(',', $query->getFields()));
$request->addParam('mlt.interestingTerms', $query->getInterestingTerms());
$request->addParam('mlt.match.include', $query->getMatchInclude());
$request->addParam('mlt.match.offset', $query->getStart());
// 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)) {
$componentBuilder = new $componentBuilderClass;
$request = $componentBuilder->build($component, $request);
}
}
return $request;
}
}
<?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 MoreLikeThis response data
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_ResponseParser_MoreLikeThis extends Solarium_Client_ResponseParser_Select
{
/**
* Get result data for the response
*
* @param Solarium_Result_Select $result
* @return array
*/
public function parse($result)
{
$data = $result->getData();
$query = $result->getQuery();
$postResult = parent::parse($result);
if (isset($data['interestingTerms']) and 'none' != $query->getInterestingTerms()) {
$terms = $data['interestingTerms'];
if ('details' == $query->getInterestingTerms()) {
$tempTerms = array();
for ($i = 0; $i < count($terms); $i += 2) {
$tempTerms[$terms[$i]] = $terms[$i + 1];
}
$terms = $tempTerms;
}
$postResult['interestingTerms'] = $terms;
}
return $postResult;
}
}
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
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Result
*/
/**
* MoreLikeThis query result
*
* This is the standard resulttype for a select query. Example usage:
* <code>
* // total solr results
* $result->getNumFound();
*
* // results fetched
* count($result);
*
* // iterate over fetched docs
* foreach ($result AS $doc) {
* ....
* }
* </code>
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_MoreLikeThis extends Solarium_Result_Select
{
/**
* this will show what "interesting" terms are used for the MoreLikeThis
* query. These are the top tf/idf terms. NOTE: if you select 'details',
* this shows you the term and boost used for each term. Unless
* mlt.boost=true all terms will have boost=1.0
*
* This is NOT the number of document fetched from Solr!
*
* @var array
*/
protected $_interestingTerms;
public function getInterestingTerms()
{
$query = $this->getQuery();
if ('none' == $query->getInterestingTerms()) {
throw new Solarium_Exception('mlt.interestingTerms is none');
}
$this->_parseResponse();
return $this->_interestingTerms;
}
}
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