Commit 63b828b7 authored by Gasol Wu's avatar Gasol Wu

apply patch from Issue #15 Add support for MoreLikeThis handler

parent 67423734
...@@ -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
......
<?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());
// add basic params to request
$request->addParam('q', $query->getQuery());
$request->addParam('mlt.match.offset', $query->getStart());
$request->addParam('rows', $query->getRows());
$request->addParam('mlt.fl', implode(',', $query->getFields()));
$request->addParam('wt', 'json');
$request->addParam('mlt.interestingTerms', $query->getInterestingTerms());
$request->addParam('mlt.match.include', $query->getMatchInclude());
// add sort fields to request
$sort = array();
foreach ($query->getSorts() 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)) {
$componentBuilder = new $componentBuilderClass;
$request = $componentBuilder->build($component, $request);
}
}
return $request;
}
}
\ No newline at end of file
This diff is collapsed.
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