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

Merge pull request #42 from Gasol/improvement/shards

Add support for Distributed Search (shards)
parents 2384976e fd6fe00b
.idea
build
phpunit.xml
......@@ -63,6 +63,13 @@ class Solarium_Client_RequestBuilder_Select extends Solarium_Client_RequestBuild
$request->addParam('fl', implode(',', $query->getFields()));
$request->addParam('wt', 'json');
// add shard fields to request
$shards = array_values($query->getShards());
if (count($shards)) {
$request->addParam('shards', implode(',', $shards));
}
$request->addParam('shards.qt', $query->getShardRequestHandler());
// add sort fields to request
$sort = array();
foreach ($query->getSorts() AS $field => $order) {
......@@ -93,8 +100,8 @@ class Solarium_Client_RequestBuilder_Select extends Solarium_Client_RequestBuild
$request = $componentBuilder->build($component, $request);
}
}
return $request;
}
}
\ No newline at end of file
}
......@@ -166,6 +166,13 @@ class Solarium_Query_Select extends Solarium_Query
*/
protected $_sorts = array();
/**
* Request to be distributed across all shards in the list
*
* @var array
*/
protected $_shards = array();
/**
* Filterqueries
*
......@@ -210,6 +217,9 @@ class Solarium_Query_Select extends Solarium_Query
case 'start':
$this->setStart((int)$value);
break;
case 'shards':
$this->setShards($value);
break;
case 'component':
$this->_createComponents($value);
break;
......@@ -503,6 +513,132 @@ class Solarium_Query_Select extends Solarium_Query
return $this;
}
/**
* Add a shard
*
* @param string $key unique string
* @param string $shard The syntax is host:port/base_url
* @return Solarium_Query_Select Provides fluent interface
* @link http://wiki.apache.org/solr/DistributedSearch
*/
public function addShard($key, $shard)
{
$this->_shards[$key] = $shard;
return $this;
}
/**
* Add multiple shards
*
* Example usage:
* <code>
* $client = new Solarium_Client;
* $query = $client->createSelect();
* $query->addShards(array(
* 'core0' => 'localhost:8983/solr/core0',
* 'core1' => 'localhost:8983/solr/core1'
* ));
* $result = $client->select($query);
* </code>
* @param array $shards
* @return Solarium_Query_Select Provides fluent interface
*/
public function addShards(array $shards)
{
foreach ($shards as $key => $shard) {
$this->addShard($key, $shard);
}
return $this;
}
/**
* Remove a shard
*
* @param string $key
* @return Solarium_Query_Select Provides fluent interface
*/
public function removeShard($key)
{
if (isset($this->_shards[$key])) {
unset($this->_shards[$key]);
}
return $this;
}
/**
* Remove all shards
*
* @return Solarium_Query_Select Provides fluent interface
*/
public function clearShards()
{
$this->_shards = array();
return $this;
}
/**
* Set multiple shards
*
* This overwrites any existing shards
*
* Example usage:
* <code>
* $client = new Solarium_Client;
* $query = $client->createSelect();
* $query->setShards(array(
* 'core0' => 'localhost:8983/solr/core0',
* 'core1' => 'localhost:8983/solr/core1'
* ));
* $result = $client->select($query);
* </code>
*
* @param array $shards Associative array of shards
* @return Solarium_Query_Select Provides fluent interface
*/
public function setShards(array $shards)
{
$this->clearShards();
$this->addShards($shards);
return $this;
}
/**
* Get a list of the shards
*
* @return array
*/
public function getShards()
{
return $this->_shards;
}
/**
* A sharded request will go to the standard request handler
* (not necessarily the original); this can be overridden via shards.qt
*
* @param string
* @return Solarium_Query_Select Provides fluent interface
*/
public function setShardRequestHandler($handler)
{
$this->_setOption('shardhandler', $handler);
return $this;
}
/**
* Get a shard request handler (shards.qt)
*
* @param string
* @return Solarium_Query_Select Provides fluent interface
*/
public function getShardRequestHandler()
{
return $this->getOption('shardhandler');
}
/**
* Create a filterquery instance
*
......@@ -856,4 +992,4 @@ class Solarium_Query_Select extends Solarium_Query
return $this->getComponent(Solarium_Query_Select::COMPONENT_SPELLCHECK, true);
}
}
\ No newline at end of file
}
......@@ -89,6 +89,29 @@ class Solarium_Client_RequestBuilder_SelectTest extends PHPUnit_Framework_TestCa
);
}
public function testSelectUrlWithShard()
{
$this->_query->addShard('shard1', 'localhost:8983/solr/shard1');
$this->_query->addShards(array(
'shard2' => 'localhost:8983/solr/shard2',
'shard3' => 'localhost:8983/solr/shard3'
));
$this->_query->setShardRequestHandler('dummy');
$request = $this->_builder->build($this->_query);
$this->assertEquals(
null,
$request->getRawData()
);
$this->assertEquals(
'select?q=*:*&start=0&rows=10&fl=*,score&wt=json' .
'&shards=localhost:8983/solr/shard1,localhost:8983/solr/shard2,localhost:8983/solr/shard3' .
'&shards.qt=dummy',
urldecode($request->getUri())
);
}
public function testSelectUrlWithSortAndFilters()
{
$this->_query->addSort('id', Solarium_Query_Select::SORT_ASC);
......@@ -142,4 +165,4 @@ class TestDummyComponent extends Solarium_Query_Select_Component{
{
return 'testcomponent';
}
}
\ No newline at end of file
}
......@@ -209,7 +209,76 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
$this->_query->getSorts()
);
}
public function testAddShard()
{
$this->_query->addShard('shard1', 'localhost:8983/solr/shard1');
$shards = $this->_query->getShards();
$this->assertEquals(
'localhost:8983/solr/shard1',
$shards['shard1']
);
}
public function testRemoveShard()
{
$this->_query->addShard('shard1', 'localhost:8983/solr/shard1');
$this->_query->removeShard('shard1');
$shards = $this->_query->getShards();
$this->assertFalse(isset($shards['shard1']));
}
public function testClearShards()
{
$this->_query->addShards(array(
'shard1' => 'localhost:8983/solr/shard1',
'shard2' => 'localhost:8983/solr/shard2',
));
$this->_query->clearShards();
$shards = $this->_query->getShards();
$this->assertTrue(is_array($shards));
$this->assertEquals(0, count($shards));
}
public function testAddShards()
{
$shards = array(
'shard1' => 'localhost:8983/solr/shard1',
'shard2' => 'localhost:8983/solr/shard2',
);
$this->_query->addShards($shards);
$this->assertEquals($shards, $this->_query->getShards());
}
public function testSetShards()
{
$this->_query->addShards(array(
'shard1' => 'localhost:8983/solr/shard1',
'shard2' => 'localhost:8983/solr/shard2',
));
$this->_query->setShards(array(
'shard3' => 'localhost:8983/solr/shard3',
'shard4' => 'localhost:8983/solr/shard4',
'shard5' => 'localhost:8983/solr/shard5',
));
$shards = $this->_query->getShards();
$this->assertEquals(3, count($shards));
$this->assertEquals(array(
'shard3' => 'localhost:8983/solr/shard3',
'shard4' => 'localhost:8983/solr/shard4',
'shard5' => 'localhost:8983/solr/shard5',
), $shards);
}
public function testSetShardRequestHandler()
{
$this->_query->setShardRequestHandler('dummy');
$this->assertEquals(
'dummy',
$this->_query->getShardRequestHandler()
);
}
public function testAddAndGetFilterQuery()
{
$fq = new Solarium_Query_Select_FilterQuery;
......@@ -581,4 +650,4 @@ class Solarium_Query_SelectTest extends PHPUnit_Framework_TestCase
$fqOptions['optionB']
);
}
}
\ 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