Skip to content
Browse files

Added test methods and support for search_type in Search->search method

  • Loading branch information...
1 parent b645f38 commit 5439fe7ee828fc0fdbd82596f8956949564c564c @comulinux comulinux committed Apr 23, 2012
Showing with 80 additions and 33 deletions.
  1. +22 −19 lib/Elastica/Index.php
  2. +23 −13 lib/Elastica/Search.php
  3. +15 −0 test/lib/Elastica/IndexTest.php
  4. +20 −1 test/lib/Elastica/SearchTest.php
View
41 lib/Elastica/Index.php
@@ -155,25 +155,28 @@ public function create(array $args = array(), $options = null) {
}
} else if (is_array($options)) {
foreach ($options as $key => $value) {
- switch ($key) {
- case 'recreate' :
- try {
- $this -> delete();
- } catch(Elastica_Exception_Response $e) {
- // Table can't be deleted, because doesn't exist
- }
- break;
-
- case 'routing' :
- if (!empty($value)) {
- $path .= '?_routing=' . $value;
- }
- break;
-
- default:
- throw new Elastica_Exception_Invalid('Invalid option '.$key);
- break;
- }
+ if (empty($value)){
+ throw new Elastica_Exception_Invalid('Invalid value '.$value.' for option '.$key);
+ }else{
+ $path_separator = (strpos($path, '?'))?'&':'?';
+ switch ($key) {
+ case 'recreate' :
+ try {
+ $this -> delete();
+ } catch(Elastica_Exception_Response $e) {
+ // Table can't be deleted, because doesn't exist
+ }
+ break;
+ case 'routing' :
+ if (!empty($value)) {
+ $path .= $path_separator.'routing=' . $value;
+ }
+ break;
+ default:
+ throw new Elastica_Exception_Invalid('Invalid option '.$key);
+ break;
+ }
+ }
}
}
return $this -> request($path, Elastica_Request::PUT, $args);
View
36 lib/Elastica/Search.php
@@ -172,19 +172,29 @@ public function search($query, $options = null) {
} else if (is_array($options)) {
foreach ($options as $key => $value) {
- switch ($key) {
- case 'limit' :
- $query -> setLimit($value);
- break;
- case 'routing' :
- if (!empty($value)) {
- $path .= '?routing=' . $value;
- }
- break;
- default:
- throw new Elastica_Exception_Invalid('Invalid option '.$key);
- break;
- }
+ if (empty($value)){
+ throw new Elastica_Exception_Invalid('Invalid value '.$value.' for option '.$key);
+ }else{
+ $path_separator = (strpos($path, '?'))?'&':'?';
+ switch ($key) {
+ case 'limit' :
+ $query -> setLimit($value);
+ break;
+ case 'routing' :
+ if (!empty($value)) {
+ $path .= $path_separator.'routing=' . $value;
+ }
+ break;
+ case 'search_type':
+ if (!empty($value)) {
+ $path .= $path_separator.'search_type=' . $value;
+ }
+ break;
+ default:
+ throw new Elastica_Exception_Invalid('Invalid option '.$key);
+ break;
+ }
+ }
}
}
View
15 test/lib/Elastica/IndexTest.php
@@ -500,22 +500,37 @@ public function testCreateArray(){
$client = new Elastica_Client();
$indexName = 'test';
$aliasName = 'test-aliase';
+
//Testing recreate (backward compatibility)
$index = $client->getIndex($indexName);
$index->create(array(), true);
$status = new Elastica_Status($client);
$this->assertTrue($status->indexExists($indexName));
+
//Testing create index with array options
$opts = array('recreate'=>true,'routing'=>'r1,r2');
$index->create(array(),$opts);
$status = new Elastica_Status($client);
$this->assertTrue($status->indexExists($indexName));
+
//Testing invalid options
try{
$opts = array('recreate'=>true,'routing'=>'r1,r2','testing_invalid_option'=>true);
$index->create(array(),$opts);
$status = new Elastica_Status($client);
$this->assertTrue($status->indexExists($indexName));
+ $this->fail('Should throw Elastica_Exception_Invalid');
+ }catch(Exception $ex){
+ $this->assertTrue($ex instanceof Elastica_Exception_Invalid);
+ }
+
+ //Invalid value
+ try{
+ $opts = array('recreate'=>true,'routing'=>null);
+ $index->create(array(),$opts);
+ $status = new Elastica_Status($client);
+ $this->assertTrue($status->indexExists($indexName));
+ $this->fail('Should throw Elastica_Exception_Invalid');
}catch(Exception $ex){
$this->assertTrue($ex instanceof Elastica_Exception_Invalid);
}
View
21 test/lib/Elastica/SearchTest.php
@@ -236,6 +236,7 @@ public function testLimitDefaultSearch()
public function testArrayConfigSearch(){
+ ini_set('display_errors',1);
$client = new Elastica_Client();
$search = new Elastica_Search($client);
@@ -263,21 +264,39 @@ public function testArrayConfigSearch(){
// default limit results (default limit is 10)
$resultSet = $search->search('test');
$this->assertEquals(10, $resultSet->count());
+
// limit = 1
$resultSet = $search->search('test', 1);
$this->assertEquals(1, $resultSet->count());
+
//Array with limit
$resultSet = $search->search('test',array('limit'=>2));
$this->assertEquals(2, $resultSet->count());
+
//Array with routing
$resultSet = $search->search('test',array('routing'=>'r1,r2'));
$this->assertEquals(10, $resultSet->count());
+
//Array with limit and routing
$resultSet = $search->search('test',array('limit'=>5,'routing'=>'r1,r2'));
- $this->assertEquals(5,$resultSet->count());// && routing condition
+ $this->assertEquals(5,$resultSet->count());
+
+ //Search types
+ $resultSet = $search->search('test',array('limit'=>5,'search_type'=>'count'));
+ $this->assertTrue(($resultSet->count()===0) && $resultSet->getTotalHits()===11);
+
//Invalid option
try{
$resultSet = $search->search('test',array('invalid_option'=>'invalid_option_value'));
+ $this->fail('Should throw Elastica_Exception_Invalid');
+ }catch(Exception $ex){
+ $this->assertTrue($ex instanceof Elastica_Exception_Invalid);
+ }
+
+ //Invalid value
+ try{
+ $resultSet = $search->search('test',array('routing'=>null));
+ $this->fail('Should throw Elastica_Exception_Invalid');
}catch(Exception $ex){
$this->assertTrue($ex instanceof Elastica_Exception_Invalid);
}

0 comments on commit 5439fe7

Please sign in to comment.
Something went wrong with that request. Please try again.