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

Added support for collections

parent 47b74f62
......@@ -44,6 +44,7 @@ use Solarium\QueryType\Select\RequestBuilder\Component\DistributedSearch as Requ
* Distributed Search (sharding) component
*
* @link http://wiki.apache.org/solr/DistributedSearch
* @link http://wiki.apache.org/solr/SolrCloud/
*/
class DistributedSearch extends Component
{
......@@ -55,6 +56,13 @@ class DistributedSearch extends Component
*/
protected $shards = array();
/**
* Requests will be distributed across collections in this list
*
* @var array
*/
protected $collections = array();
/**
* Get component type
*
......@@ -100,6 +108,9 @@ class DistributedSearch extends Component
case 'shards':
$this->setShards($value);
break;
case 'collections':
$this->setCollections($value);
break;
}
}
}
......@@ -234,4 +245,89 @@ class DistributedSearch extends Component
{
return $this->getOption('shardhandler');
}
/**
* Add a collection
*
* @param string $key unique string
* @param string $collection The syntax is host:port/base_url
* @return self Provides fluent interface
* @link http://wiki.apache.org/solr/SolrCloud/
*/
public function addCollection($key, $collection)
{
$this->collections[$key] = $collection;
return $this;
}
/**
* Add multiple collections
*
* @param array $collections
* @return self Provides fluent interface
*/
public function addCollections(array $collections)
{
foreach ($collections as $key => $collection) {
$this->addCollection($key, $collection);
}
return $this;
}
/**
* Remove a collection
*
* @param string $key
* @return self Provides fluent interface
*/
public function removeCollection($key)
{
if (isset($this->collections[$key])) {
unset($this->collections[$key]);
}
return $this;
}
/**
* Remove all collections
*
* @return self Provides fluent interface
*/
public function clearCollections()
{
$this->collections = array();
return $this;
}
/**
* Set multiple collections
*
* This overwrites any existing collections
*
* @param array $collections Associative array of collections
* @return self Provides fluent interface
*/
public function setCollections(array $collections)
{
$this->clearCollections();
$this->addCollections($collections);
return $this;
}
/**
* Get a list of the collections
*
* @return array
*/
public function getCollections()
{
return $this->collections;
}
}
......@@ -55,13 +55,20 @@ class DistributedSearch implements ComponentRequestBuilderInterface
*/
public function buildComponent($component, $request)
{
// add shard fields to request
// add shards to request
$shards = array_values($component->getShards());
if (count($shards)) {
$request->addParam('shards', implode(',', $shards));
}
$request->addParam('shards.qt', $component->getShardRequestHandler());
// add collections to request
$collections = array_values($component->getCollections());
if (count($collections)) {
$request->addParam('collection', implode(',', $collections));
}
return $request;
}
}
......@@ -46,7 +46,7 @@ class DistributedSearchTest extends \PHPUnit_Framework_TestCase
$this->distributedSearch = new DistributedSearch;
}
public function testConfigMode()
public function testConfigModeForShards()
{
$options = array(
'shardhandler' => 'dummyhandler',
......@@ -62,6 +62,19 @@ class DistributedSearchTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($options['shards'], $this->distributedSearch->getShards());
}
public function testConfigModeForCollections()
{
$options = array(
'collections' => array(
'collection1' => 'localhost:8983/solr/collection1',
'collection2' => 'localhost:8983/solr/collection2',
)
);
$this->distributedSearch->setOptions($options);
$this->assertEquals($options['collections'], $this->distributedSearch->getCollections());
}
public function testGetType()
{
$this->assertEquals(
......@@ -149,4 +162,68 @@ class DistributedSearchTest extends \PHPUnit_Framework_TestCase
);
}
public function testAddCollection()
{
$this->distributedSearch->addCollection('collection1', 'localhost:8983/solr/collection1');
$collections = $this->distributedSearch->getCollections();
$this->assertEquals(
'localhost:8983/solr/collection1',
$collections['collection1']
);
}
public function testRemoveCollection()
{
$this->distributedSearch->addCollection('collection1', 'localhost:8983/solr/collection1');
$this->distributedSearch->removeCollection('collection1');
$collections = $this->distributedSearch->getCollections();
$this->assertFalse(isset($collections['collection1']));
}
public function testClearCollections()
{
$this->distributedSearch->addCollections(array(
'collection1' => 'localhost:8983/solr/collection1',
'collection2' => 'localhost:8983/solr/collection2',
));
$this->distributedSearch->clearCollections();
$collections = $this->distributedSearch->getCollections();
$this->assertTrue(is_array($collections));
$this->assertEquals(0, count($collections));
}
public function testAddCollections()
{
$collections = array(
'collection1' => 'localhost:8983/solr/collection1',
'collection2' => 'localhost:8983/solr/collection2',
);
$this->distributedSearch->addCollections($collections);
$this->assertEquals($collections, $this->distributedSearch->getCollections());
}
public function testSetCollections()
{
$this->distributedSearch->addCollections(array(
'collection1' => 'localhost:8983/solr/collection1',
'collection2' => 'localhost:8983/solr/collection2',
));
$this->distributedSearch->setCollections(array(
'collection3' => 'localhost:8983/solr/collection3',
'collection4' => 'localhost:8983/solr/collection4',
'collection5' => 'localhost:8983/solr/collection5',
));
$collections = $this->distributedSearch->getCollections();
$this->assertEquals(3, count($collections));
$this->assertEquals(array(
'collection3' => 'localhost:8983/solr/collection3',
'collection4' => 'localhost:8983/solr/collection4',
'collection5' => 'localhost:8983/solr/collection5',
), $collections);
}
}
......@@ -37,7 +37,7 @@ use Solarium\Core\Client\Request;
class DistributedSearchTest extends \PHPUnit_Framework_TestCase
{
public function testBuildComponent()
public function testBuildComponentWithShards()
{
$builder = new RequestBuilder;
$request = new Request();
......@@ -61,4 +61,26 @@ class DistributedSearchTest extends \PHPUnit_Framework_TestCase
);
}
public function testBuildComponentWithCollections()
{
$builder = new RequestBuilder;
$request = new Request();
$component = new Component();
$component->addCollection('collection1', 'localhost:8983/solr/collection1');
$component->addCollections(array(
'collection2' => 'localhost:8983/solr/collection2',
'collection3' => 'localhost:8983/solr/collection3'
));
$request = $builder->buildComponent($component, $request);
$this->assertEquals(
array(
'collection' => 'localhost:8983/solr/collection1,localhost:8983/solr/collection2,localhost:8983/solr/collection3',
),
$request->getParams()
);
}
}
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