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

Fix Type::getMapping from aliased index #591

Merged
merged 2 commits into from
Apr 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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