Skip to content

Commit

Permalink
Merge pull request #591 from idio/alias-mapping
Browse files Browse the repository at this point in the history
Fix Type::getMapping from aliased index
  • Loading branch information
ruflin committed Apr 21, 2014
2 parents 6262edc + 15b2974 commit 99fbfbf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 29 deletions.
2 changes: 2 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ CHANGES

2014-04-20
- Handling of HasChild type parsing bug #585
- Consolidate Index getMapping tests
- Fix Type::getMapping when using an aliased index #588

2014-04-19
- Release v1.1.1.0
Expand Down
9 changes: 6 additions & 3 deletions lib/Elastica/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,13 @@ public function getMapping()

$response = $this->request($path, Request::GET);
$data = $response->getData();
if (!isset($data[$this->getIndex()->getName()])) {
return array();

$mapping = array_shift($data);
if (isset($mapping['mappings'])) {
return $mapping['mappings'];
}
return $data[$this->getIndex()->getName()]['mappings'];

return array();
}

/**
Expand Down
32 changes: 31 additions & 1 deletion test/lib/Elastica/Test/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testMapping()
$type->addDocument($doc);
$index->optimize();

$storedMapping = $type->getMapping();
$storedMapping = $index->getMapping();

$this->assertEquals($storedMapping['test']['properties']['id']['type'], 'integer');
$this->assertEquals($storedMapping['test']['properties']['id']['store'], true);
Expand All @@ -41,6 +41,36 @@ public function testMapping()
$result = $type->search('hanswurst');
}

public function testGetMappingAlias() {

$indexName = 'test-mapping';
$aliasName = 'test-mapping-alias';

$index = $this->_createIndex($indexName);
$indexName = $index->getName();
$index->addAlias($aliasName);

$type = new Type($index, 'test');
$mapping = new Mapping($type, array(
'id' => array('type' => 'integer', 'store' => 'yes'),
));
$type->setMapping($mapping);

$client = $index->getClient();

// Index mapping
$mapping1 = $client->getIndex($indexName)->getMapping();

// Alias mapping
$mapping2 = $client->getIndex($aliasName)->getMapping();

// Make sure, a mapping is set
$this->assertNotEmpty($mapping1);

// Alias and index mapping should be identical
$this->assertEquals($mapping1, $mapping2);
}

public function testParent()
{
$index = $this->_createIndex();
Expand Down
60 changes: 35 additions & 25 deletions test/lib/Elastica/Test/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,36 +771,46 @@ public function testExists()
$index->delete();
$this->assertFalse($index->exists());
}

public function testGetMappingAlias() {

$indexName = 'test-mapping';
$aliasName = 'test-mapping-alias';


public function testGetMapping() {
$indexName = 'test';
$typeName = 'test-type';

$index = $this->_createIndex($indexName);
$indexName = $index->getName();
$type = new Type($index, $typeName);
$mapping = new Mapping($type, $expect = array(
'id' => array('type' => 'integer', 'store' => true)
));
$type->setMapping($mapping);

$client = $index->getClient();

$this->assertEquals(
array('test-type' => array('properties' => $expect)),
$client->getIndex($indexName)->getType($typeName)->getMapping()
);
}

public function testGetMappingAlias() {
$indexName = 'test';
$aliasName = 'test-alias';
$typeName = 'test-alias-type';

$index = $this->_createIndex($indexName);
$index->addAlias($aliasName);

$type = new Type($index, 'test');
$mapping = new Mapping($type, array(
'id' => array('type' => 'integer', 'store' => 'yes'),
));
$type = new Type($index, $typeName);
$mapping = new Mapping($type, $expect = array(
'id' => array('type' => 'integer', 'store' => true)
));
$type->setMapping($mapping);

$client = $index->getClient();

// Index mapping
$mapping1 = $client->getIndex($indexName)->getMapping();

// Alias mapping
$mapping2 = $client->getIndex($aliasName)->getMapping();

// Make sure, a mapping is set
$this->assertNotEmpty($mapping1);

// Alias and index mapping should be identical
$this->assertEquals($mapping1, $mapping2);


$this->assertEquals(
array('test-alias-type' => array('properties' => $expect)),
$client->getIndex($aliasName)->getType($typeName)->getMapping()
);
}
}

Expand Down

0 comments on commit 99fbfbf

Please sign in to comment.