Skip to content

Commit 99fbfbf

Browse files
committed
Merge pull request #591 from idio/alias-mapping
Fix Type::getMapping from aliased index
2 parents 6262edc + 15b2974 commit 99fbfbf

File tree

4 files changed

+74
-29
lines changed

4 files changed

+74
-29
lines changed

changes.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ CHANGES
22

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

68
2014-04-19
79
- Release v1.1.1.0

lib/Elastica/Type.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,13 @@ public function getMapping()
298298

299299
$response = $this->request($path, Request::GET);
300300
$data = $response->getData();
301-
if (!isset($data[$this->getIndex()->getName()])) {
302-
return array();
301+
302+
$mapping = array_shift($data);
303+
if (isset($mapping['mappings'])) {
304+
return $mapping['mappings'];
303305
}
304-
return $data[$this->getIndex()->getName()]['mappings'];
306+
307+
return array();
305308
}
306309

307310
/**

test/lib/Elastica/Test/IndexTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testMapping()
3030
$type->addDocument($doc);
3131
$index->optimize();
3232

33-
$storedMapping = $type->getMapping();
33+
$storedMapping = $index->getMapping();
3434

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

44+
public function testGetMappingAlias() {
45+
46+
$indexName = 'test-mapping';
47+
$aliasName = 'test-mapping-alias';
48+
49+
$index = $this->_createIndex($indexName);
50+
$indexName = $index->getName();
51+
$index->addAlias($aliasName);
52+
53+
$type = new Type($index, 'test');
54+
$mapping = new Mapping($type, array(
55+
'id' => array('type' => 'integer', 'store' => 'yes'),
56+
));
57+
$type->setMapping($mapping);
58+
59+
$client = $index->getClient();
60+
61+
// Index mapping
62+
$mapping1 = $client->getIndex($indexName)->getMapping();
63+
64+
// Alias mapping
65+
$mapping2 = $client->getIndex($aliasName)->getMapping();
66+
67+
// Make sure, a mapping is set
68+
$this->assertNotEmpty($mapping1);
69+
70+
// Alias and index mapping should be identical
71+
$this->assertEquals($mapping1, $mapping2);
72+
}
73+
4474
public function testParent()
4575
{
4676
$index = $this->_createIndex();

test/lib/Elastica/Test/TypeTest.php

+35-25
Original file line numberDiff line numberDiff line change
@@ -771,36 +771,46 @@ public function testExists()
771771
$index->delete();
772772
$this->assertFalse($index->exists());
773773
}
774-
775-
public function testGetMappingAlias() {
776-
777-
$indexName = 'test-mapping';
778-
$aliasName = 'test-mapping-alias';
779-
774+
775+
public function testGetMapping() {
776+
$indexName = 'test';
777+
$typeName = 'test-type';
778+
780779
$index = $this->_createIndex($indexName);
781780
$indexName = $index->getName();
781+
$type = new Type($index, $typeName);
782+
$mapping = new Mapping($type, $expect = array(
783+
'id' => array('type' => 'integer', 'store' => true)
784+
));
785+
$type->setMapping($mapping);
786+
787+
$client = $index->getClient();
788+
789+
$this->assertEquals(
790+
array('test-type' => array('properties' => $expect)),
791+
$client->getIndex($indexName)->getType($typeName)->getMapping()
792+
);
793+
}
794+
795+
public function testGetMappingAlias() {
796+
$indexName = 'test';
797+
$aliasName = 'test-alias';
798+
$typeName = 'test-alias-type';
799+
800+
$index = $this->_createIndex($indexName);
782801
$index->addAlias($aliasName);
783-
784-
$type = new Type($index, 'test');
785-
$mapping = new Mapping($type, array(
786-
'id' => array('type' => 'integer', 'store' => 'yes'),
787-
));
802+
$type = new Type($index, $typeName);
803+
$mapping = new Mapping($type, $expect = array(
804+
'id' => array('type' => 'integer', 'store' => true)
805+
));
788806
$type->setMapping($mapping);
789-
807+
790808
$client = $index->getClient();
791-
792-
// Index mapping
793-
$mapping1 = $client->getIndex($indexName)->getMapping();
794-
795-
// Alias mapping
796-
$mapping2 = $client->getIndex($aliasName)->getMapping();
797-
798-
// Make sure, a mapping is set
799-
$this->assertNotEmpty($mapping1);
800-
801-
// Alias and index mapping should be identical
802-
$this->assertEquals($mapping1, $mapping2);
803-
809+
810+
$this->assertEquals(
811+
array('test-alias-type' => array('properties' => $expect)),
812+
$client->getIndex($aliasName)->getType($typeName)->getMapping()
813+
);
804814
}
805815
}
806816

0 commit comments

Comments
 (0)