Permalink
Browse files

add `_parent` in mapping during index reset

  • Loading branch information...
1 parent 10eae2f commit 79eb062cf75e7e9d540014347c43cc3e10dca6b8 Julien Muetton committed Nov 28, 2012
Showing with 56 additions and 2 deletions.
  1. +21 −1 Resetter.php
  2. +35 −1 Tests/ResetterTest.php
View
@@ -58,7 +58,27 @@ public function resetIndexType($indexName, $typeName)
$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;
}
/**
@@ -29,6 +29,17 @@ public function setUp()
),
),
),
+ '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 @@ public function testResetIndexType()
$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 @@ public function testResetIndexTypeShouldThrowExceptionForInvalidType()
$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
*/

0 comments on commit 79eb062

Please sign in to comment.