diff --git a/Resetter.php b/Resetter.php index f8d94ed..2773140 100644 --- a/Resetter.php +++ b/Resetter.php @@ -58,7 +58,27 @@ class Resetter $type = $indexConfig['index']->getType($typeName); $type->delete(); - $type->setMapping($indexConfig['config']['mappings'][$typeName]['properties']); + $mapping = $this->createMapping($indexConfig['config']['mappings'][$typeName]); + $type->setMapping($mapping); + } + + /** + * create type mapping object + * + * @param array $indexConfig + * @return Elastica_Type_Mapping + */ + protected function createMapping($indexConfig) + { + $mapping = \Elastica_Type_Mapping::create($indexConfig['properties']); + + foreach($indexConfig['properties'] as $field => $type) { + if (!empty($type['_parent']) && $type['_parent'] !== '~') { + $mapping->setParam('_parent', array('type' => $type['_parent']['type'])); + } + } + + return $mapping; } /** diff --git a/Tests/ResetterTest.php b/Tests/ResetterTest.php index d59da18..754310c 100644 --- a/Tests/ResetterTest.php +++ b/Tests/ResetterTest.php @@ -29,6 +29,17 @@ class ResetterTest extends \PHPUnit_Framework_TestCase ), ), ), + 'parent' => array( + 'index' => $this->getMockElasticaIndex(), + 'config' => array( + 'mappings' => array( + 'a' => array('properties' => array( + 'field_1' => array('_parent' => array('type' => 'b', 'identifier' => 'id')), + 'field_2' => array())), + 'b' => array('properties' => array()), + ), + ), + ), ); } @@ -80,9 +91,10 @@ class ResetterTest extends \PHPUnit_Framework_TestCase $type->expects($this->once()) ->method('delete'); + $mapping = \Elastica_Type_Mapping::create($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']); $type->expects($this->once()) ->method('setMapping') - ->with($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']); + ->with($mapping); $resetter = new Resetter($this->indexConfigsByName); $resetter->resetIndexType('foo', 'a'); @@ -106,6 +118,28 @@ class ResetterTest extends \PHPUnit_Framework_TestCase $resetter->resetIndexType('foo', 'c'); } + public function testIndexMappingForParent() + { + $type = $this->getMockElasticaType(); + + $this->indexConfigsByName['parent']['index']->expects($this->once()) + ->method('getType') + ->with('a') + ->will($this->returnValue($type)); + + $type->expects($this->once()) + ->method('delete'); + + $mapping = \Elastica_Type_Mapping::create($this->indexConfigsByName['parent']['config']['mappings']['a']['properties']); + $mapping->setParam('_parent', array('type' => 'b')); + $type->expects($this->once()) + ->method('setMapping') + ->with($mapping); + + $resetter = new Resetter($this->indexConfigsByName); + $resetter->resetIndexType('parent', 'a'); + } + /** * @return Elastica_Index */