Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
cfac1abd
Commit
cfac1abd
authored
Sep 26, 2011
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #42 from Gasol/improvement/shards
Add support for Distributed Search (shards)
parents
2384976e
fd6fe00b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
242 additions
and
6 deletions
+242
-6
.gitignore
.gitignore
+1
-0
library/Solarium/Client/RequestBuilder/Select.php
library/Solarium/Client/RequestBuilder/Select.php
+9
-2
library/Solarium/Query/Select.php
library/Solarium/Query/Select.php
+137
-1
tests/Solarium/Client/RequestBuilder/SelectTest.php
tests/Solarium/Client/RequestBuilder/SelectTest.php
+24
-1
tests/Solarium/Query/SelectTest.php
tests/Solarium/Query/SelectTest.php
+71
-2
No files found.
.gitignore
View file @
cfac1abd
.idea
build
phpunit.xml
library/Solarium/Client/RequestBuilder/Select.php
View file @
cfac1abd
...
...
@@ -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
}
library/Solarium/Query/Select.php
View file @
cfac1abd
...
...
@@ -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
}
tests/Solarium/Client/RequestBuilder/SelectTest.php
View file @
cfac1abd
...
...
@@ -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
}
tests/Solarium/Query/SelectTest.php
View file @
cfac1abd
...
...
@@ -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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment