Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options to alias creation #161

Closed
wants to merge 11 commits into from
96 changes: 73 additions & 23 deletions lib/Elastica/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,30 +273,80 @@ public function getClient() {
* @return Elastica_Response Response
* @link http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html
*/
public function addAlias($name, $replace = false) {
$path = '_aliases';

if ($replace) {
$status = new Elastica_Status($this->getClient());

foreach ($status->getIndicesWithAlias($name) as $index) {
$index->removeAlias($name);
}
}

$data = array(
'actions' => array(
array(
'add' => array(
'index' => $this->getName(),
'alias' => $name
)
)
)
);

public function addAlias($name, $options = null, $params = null) {
$path = '_aliases';

$add = array('index' => $this->getName(),
'alias' => $name);

if(is_array($params)){
foreach($params as $key => $value){
switch($key){
case 'recreate':
$status = new Elastica_Status($this->getClient());
foreach ($status->getIndicesWithAlias($name) as $index) {
$index->removeAlias($name);
}
break;

default:
throw new Elastica_Exception_Invalid('Invalid param '.$key);
break;
}
}
}

if(is_array($options)){
foreach ($options as $key => $value){
if (!empty($value)){
switch ($key) {
case 'replace':
$status = new Elastica_Status($this->getClient());
foreach ($status->getIndicesWithAlias($name) as $index) {
$index->removeAlias($name);
}
break;

case 'filter':
if (is_a($value,'Elastica_Query_Abstract')){
$add['filter'] = $value->toArray();
}else{
$add['filter'] = $value;
}
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the easy way. I prefer a "is_a($value, 'Elastica_Query_XXX)" but I don't know how to ask about all the Elastica_Query classes.
By the way, it seems to work :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($value instanceof Elastica_Query_Abstract)

This should work ;-)


case 'index_routing':
$add['index_routing'] = $value;
break;

case 'search_routing':
$add['search_routing'] = $value;
break;

default:
throw new Elastica_Exception_Invalid('Invalid option '.$key);
break;
}
}else{
throw new Elastica_Exception_Invalid('Error setting '.$key.' to null');
}
}
}

return $this->getClient()->request($path, Elastica_Request::POST, $data);
}
$data = array(
'actions' => array(
array(
'add' => $add
)
)
);

return $this->getClient()->request($path, Elastica_Request::POST, $data);
}




/**
* Removes an alias pointing to the current index
Expand Down
37 changes: 34 additions & 3 deletions test/lib/Elastica/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function testExcludeFileSource() {
$this->assertEquals($data['text'], $text);
$this->assertFalse(isset($data['file']));
}

/*
public function testAddRemoveAlias() {
$client = new Elastica_Client();

Expand Down Expand Up @@ -286,7 +286,7 @@ public function testAddRemoveAlias() {
$this->assertTrue(true);
}
}

*/
public function testDeleteIndexDeleteAlias() {
$indexName = 'test';
$aliasName = 'test-aliase';
Expand Down Expand Up @@ -334,6 +334,7 @@ public function testAddAliasTwoIndices() {
$this->assertTrue($index2->getStatus()->hasAlias($aliasName));
}

/*
public function testReplaceAlias() {
$indexName1 = 'test1';
$indexName2 = 'test2';
Expand All @@ -358,6 +359,7 @@ public function testReplaceAlias() {
$this->assertFalse($index1->getStatus()->hasAlias($aliasName));
$this->assertTrue($index2->getStatus()->hasAlias($aliasName));
}
*/

public function testAddDocumentVersion() {
$client = new Elastica_Client();
Expand Down Expand Up @@ -499,7 +501,6 @@ public function testLimitDefaultIndex()
public function testCreateArray(){
$client = new Elastica_Client();
$indexName = 'test';
$aliasName = 'test-aliase';

//Testing recreate (backward compatibility)
$index = $client->getIndex($indexName);
Expand Down Expand Up @@ -535,4 +536,34 @@ public function testCreateArray(){
$this->assertTrue($ex instanceof Elastica_Exception_Invalid);
}
}


public function testCreateAliasArray(){
$options = array();
$client = new Elastica_Client($options);
$indexName = 'test';
$aliasName = 'test-aliase';

//Creating the index
$index = $client->getIndex($indexName);
$index->create(array(), true);
$status = new Elastica_Status($client);
$this->assertTrue($status->indexExists($indexName));

//Creating the alias
try{
$query = new Elastica_Query_Term();
$query->setTerm('user', 'comulinux', 2);
$opts = array('replace'=>true,
'index_routing' => '1',
'search_routing' => '1,2',
'filter' => $query);
$index->addAlias($aliasName,$opts);
$status = new Elastica_Status($client);
$this->assertTrue($status->aliasExists($aliasName));
//$this->fail('Should throw Elastica_Exception_NotImplemented');
}catch(Elastica_Exception_NotImplemented $ex){
$this->assertTrue($ex instanceof Elastica_Exception_NotImplemented);
}
}
}
2 changes: 1 addition & 1 deletion test/lib/Elastica/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function testArrayConfigSearch(){

//Search types
$resultSet = $search->search('test',array('limit'=>5,'search_type'=>'count'));
$this->assertTrue(($resultSet->count()===0) && $resultSet->getTotalHits()===11);
$this->assertTrue(($resultSet->count()===0) && $resultSet->getTotalHits()===$i);

//Invalid option
try{
Expand Down