diff --git a/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php b/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php index 9de9d62..816a3d6 100644 --- a/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php +++ b/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php @@ -109,9 +109,8 @@ public function deleteIndexByAlias($alias) * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. In fact, it would be preferable to create an asynchronous process that executes this task. * If you set it to false, don't forget to put an alias to the new index when the corresponding task is gone. * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException - * @throws IndexNotFoundException * @throws IndexAlreadyExistException + * @throws IndexNotFoundException */ public function copyIndex($aliasSrc, $aliasDest, $refresh = false, $waitForCompletion = true) { @@ -158,7 +157,6 @@ public function copyIndex($aliasSrc, $aliasDest, $refresh = false, $waitForCompl * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. * If you set it to false, don't forget to remove the old index and to switch the alias after the task is gone. * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException * @throws IndexNotFoundException */ public function reindex($alias, $refresh = false, $needToCreateIndexDest = true, $waitForCompletion = true) @@ -251,7 +249,6 @@ public function addSettings($alias, $settings) * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. * If you set it to false, don't forget to remove the old index and to switch the alias after the task is gone. * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException * @throws IndexNotFoundException */ public function updateSettings($alias, $settings, $refresh = false, $needReindexation = true, $waitForCompletion = true) @@ -310,11 +307,13 @@ public function updateSettings($alias, $settings, $refresh = false, $needReindex * @param bool $waitForCompletion : According to the official documentation (https://www.elastic.co/guide/en/elasticsearch/reference/2.4/docs-reindex.html), * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. * If you set it to false, don't forget to remove the old index and to switch the alias after the task is gone. + * @param bool $includeTypeName : Indicate if you still use a type in your index. To be ready for the next release (v8), you should consider to set this parameter to false. + * Which means you have to change your mapping to remove the usage of the type. See more here: https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0 + * This parameter will be removed in the next release * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException * @throws IndexNotFoundException */ - public function updateMappings($alias, $mapping, $refresh = false, $needReindexation = true, $waitForCompletion = true) + public function updateMappings($alias, $mapping, $refresh = false, $needReindexation = true, $waitForCompletion = true, $includeTypeName = true) { if (!$this->existsAlias($alias)) { throw new IndexNotFoundException($alias); @@ -340,6 +339,10 @@ public function updateMappings($alias, $mapping, $refresh = false, $needReindexa $this->copySettings($params, $settings); + if ($includeTypeName) { + $params['include_type_name'] = true; + } + $result = $this->client->indices()->create($params); if ($result['acknowledged'] && $needReindexation) { @@ -402,7 +405,7 @@ public function getSettings($alias) /** * @param $alias string [REQUIRED] the name of the index or the name of the alias - * @param @deprecated $type string [REQUIRED] the type of the document + * @param $type string [REQUIRED] the type of the document * @param $id string|int [REQUIRED] the document ID * @param $refresh bool * @return callable|array @@ -439,7 +442,7 @@ public function getAllDocuments($alias, $from = 0, $size = 10) /** * @param string $alias [REQUIRED] * @param array|null $query - * @param @deprecated string $type. This parameter will be removed in the next major release + * @param string $type . This parameter will be removed in the next major release * @param int $from the offset from the first result you want to fetch (0 by default) * @param int $size allows you to configure the maximum amount of hits to be returned. (10 by default) * @return callable|array @@ -467,7 +470,7 @@ public function searchDocuments($alias, $query = null, $type = null, $from = 0, /** * @param $alias [REQUIRED] - * @param @deprecated string $type. This parameter will be removed in the next major release + * @param string $type . This parameter will be removed in the next major release * @param array|null $body * @param SearchParameter $searchParameter * @return callable|array @@ -501,7 +504,7 @@ public function advancedSearchDocument($alias, $type = null, $body = null, $sear /** * @param string $index [REQUIRED] If the alias is associated to an unique index, you can pass an alias rather than an index * @param $id [REQUIRED] - * @param @deprecated string $type [REQUIRED] + * @param string $type [REQUIRED] * @param array $body [REQUIRED] : actual document to update * @param bool $refresh wait until the result are visible to search * @return boolean : true if the document has been updated. Otherwise, the document has been created. @@ -518,7 +521,7 @@ public function updateDocument($index, $id, $type, $body, $refresh = false) /** * @param string $index [REQUIRED] If the alias is associated to an unique index, you can pass an alias rather than an index * @param $id [REQUIRED] - * @param @deprecated string $type [REQUIRED] + * @param string $type [REQUIRED] * @param array $body [REQUIRED] : actual document to create * @param bool $refresh wait until the result are visible to search * @return boolean : true if the document has been created. @@ -562,7 +565,7 @@ public function deleteAllDocuments($alias) /** * @param $alias [REQUIRED] * @param $id [REQUIRED] - * @param @deprecated string $type [REQUIRED] + * @param string $type [REQUIRED] * @param boolean $refresh , Refresh the index after performing the operation * @return void * @throws IndexNotFoundException @@ -734,7 +737,6 @@ protected function copyMappingAndSetting($indexSource, $indexDest) $this->copySettings($params, $settingSource); - $this->client->indices()->create($params); } diff --git a/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelperInterface.php b/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelperInterface.php index fa32b28..2ae10fe 100644 --- a/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelperInterface.php +++ b/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelperInterface.php @@ -4,7 +4,6 @@ use Elasticsearch\Client; use Elasticsearch\Common\Exceptions\InvalidArgumentException; -use Elasticsearch\Common\Exceptions\RuntimeException; use Nexucis\Elasticsearch\Helper\Nodowntime\Exceptions\IndexAlreadyExistException; use Nexucis\Elasticsearch\Helper\Nodowntime\Exceptions\IndexNotFoundException; use Nexucis\Elasticsearch\Helper\Nodowntime\Parameter\SearchParameter; @@ -50,9 +49,8 @@ public function deleteIndexByAlias($alias); * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. In fact, it would be preferable to create an asynchronous process that executes this task. * If you set it to false, don't forget to put an alias to the new index when the corresponding task is gone. * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException - * @throws IndexNotFoundException * @throws IndexAlreadyExistException + * @throws IndexNotFoundException */ public function copyIndex($aliasSrc, $aliasDest, $refresh = false, $waitForCompletion = true); @@ -64,7 +62,6 @@ public function copyIndex($aliasSrc, $aliasDest, $refresh = false, $waitForCompl * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. * If you set it to false, don't forget to remove the old index and to switch the alias after the task is gone. * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException * @throws IndexNotFoundException */ public function reindex($alias, $refresh = false, $needToCreateIndexDest = true, $waitForCompletion = true); @@ -95,7 +92,6 @@ public function addSettings($alias, $settings); * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. * If you set it to false, don't forget to remove the old index and to switch the alias after the task is gone. * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException * @throws IndexNotFoundException */ public function updateSettings($alias, $settings, $refresh = false, $needReindexation = true, $waitForCompletion = true); @@ -112,11 +108,13 @@ public function updateSettings($alias, $settings, $refresh = false, $needReindex * @param bool $waitForCompletion : According to the official documentation (https://www.elastic.co/guide/en/elasticsearch/reference/2.4/docs-reindex.html), * it is strongly advised to not set this parameter to false with ElasticSearch 2.4. * If you set it to false, don't forget to remove the old index and to switch the alias after the task is gone. + * @param bool $includeTypeName : Indicate if you still use a type in your index. To be ready for the next release (v8), you should consider to set this parameter to false. + * Which means you have to change your mapping to remove the usage of the type. See more here: https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0 + * This parameter will be removed in the next release * @return string : the task ID if the parameter $waitForCompletion is set to false, acknowledge if not - * @throws RuntimeException * @throws IndexNotFoundException */ - public function updateMappings($alias, $mapping, $refresh = false, $needReindexation = true, $waitForCompletion = true); + public function updateMappings($alias, $mapping, $refresh = false, $needReindexation = true, $waitForCompletion = true, $includeTypeName = true); /** * @return array @@ -149,25 +147,25 @@ public function getDocument($alias, $type, $id, $refresh = false); * @param string $alias [REQUIRED] * @param int $from the offset from the first result you want to fetch (0 by default) * @param int $size allows you to configure the maximum amount of hits to be returned. (10 by default) - * @throws IndexNotFoundException * @return callable|array + * @throws IndexNotFoundException */ public function getAllDocuments($alias, $from = 0, $size = 10); /** * @param string $alias [REQUIRED] * @param array|null $query - * @param @deprecated string $type + * @param string $type * @param int $from the offset from the first result you want to fetch (0 by default) * @param int $size allows you to configure the maximum amount of hits to be returned. (10 by default) - * @throws IndexNotFoundException * @return callable|array + * @throws IndexNotFoundException */ public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10); /** * @param $alias - * @param @deprecated string $type + * @param string $type * @param array|null $body * @param SearchParameter $searchParameter * @return callable|array @@ -178,7 +176,7 @@ public function advancedSearchDocument($alias, $type = null, $body = null, $sear /** * @param string $index [REQUIRED] If the alias is associated to an unique index, you can pass an alias rather than an index * @param $id [REQUIRED] - * @param @deprecated string $type [REQUIRED] + * @param string $type [REQUIRED] * @param array $body [REQUIRED] : actual document to update * @param bool $refresh wait until the result are visible to search * @return boolean : true if the document has been updated. Otherwise, the document has been created. @@ -189,7 +187,7 @@ public function updateDocument($index, $id, $type, $body, $refresh = false); /** * @param string $index [REQUIRED] If the alias is associated to an unique index, you can pass an alias rather than an index * @param $id [REQUIRED] - * @param @deprecated string $type [REQUIRED] + * @param string $type [REQUIRED] * @param array $body [REQUIRED] : actual document to create * @param bool $refresh wait until the result are visible to search * @return boolean : true if the document has been created. @@ -202,14 +200,13 @@ public function addDocument($index, $type, $body, $id = null, $refresh = false); * * @param string $alias [REQUIRED] * @return void - * @throws IndexNotFoundException */ public function deleteAllDocuments($alias); /** * @param $alias [REQUIRED] * @param $id [REQUIRED] - * @param @deprecated string $type [REQUIRED] + * @param string $type [REQUIRED] * @param boolean $refresh , Refresh the index after performing the operation * @return void * @throws IndexNotFoundException diff --git a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/AbstractIndexHelperTest.php b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/AbstractIndexHelperTest.php index 99a00d7..f175f82 100644 --- a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/AbstractIndexHelperTest.php +++ b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/AbstractIndexHelperTest.php @@ -61,6 +61,28 @@ public function aliasDataProvider() ]; } + public function aliasDataProviderWithTypeName() + { + return [ + 'latin-char-with-type' => [ + 'myindextest', + true + ], + 'latin-char-without-type' => [ + 'myindextest', + false + ], + 'utf-8-char-with-type' => [ + '⿇⽸⾽', + true + ], + 'utf-8-char-without-type' => [ + '⿇⽸⾽', + false + ], + ]; + } + protected function createIndex2($alias) { $index = $alias . IndexHelper::INDEX_NAME_CONVENTION_2; diff --git a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/MappingsActionTest.php b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/MappingsActionTest.php index cfbc9a9..7872123 100644 --- a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/MappingsActionTest.php +++ b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/MappingsActionTest.php @@ -6,13 +6,13 @@ class MappingsActionTest extends AbstractIndexHelperTest { /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingsEmpty(string $alias) + public function testUpdateMappingsEmpty(string $alias, bool $includeTypeName) { $this->helper->createIndexByAlias($alias); - $this->helper->updateMappings($alias, array()); + $this->helper->updateMappings($alias, array(), false, true, true, $includeTypeName); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_2)); @@ -20,13 +20,13 @@ public function testUpdateMappingsEmpty(string $alias) } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingsNull(string $alias) + public function testUpdateMappingsNull(string $alias, bool $includeTypeName) { $this->helper->createIndexByAlias($alias); - $this->helper->updateMappings($alias, null); + $this->helper->updateMappings($alias, null, false, true, true, $includeTypeName); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_2)); @@ -44,11 +44,39 @@ public function testUpdateMappingsIndexNotFoundException() } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingsBasicData(string $alias) + public function testUpdateMappingsBasicData(string $alias, bool $includeTypeName) { - $mapping = [ + if ($includeTypeName) { + $mapping = [ + 'my_type' => [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] + ] + ]; + } else { + $mapping = [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] + ]; + } + + $mappingExpected = [ 'properties' => [ 'first_name' => [ 'type' => 'text', @@ -62,10 +90,10 @@ public function testUpdateMappingsBasicData(string $alias) $this->helper->createIndexByAlias($alias); - $this->helper->updateMappings($alias, $mapping); + $this->helper->updateMappings($alias, $mapping, true, true, true, $includeTypeName); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_2)); - $this->assertEquals($mapping, $this->helper->getMappings($alias)); + $this->assertEquals($mappingExpected, $this->helper->getMappings($alias)); } /** @@ -85,7 +113,7 @@ public function testUpdateMappingsWithIndexNotEmpty(string $alias) ] ]; - $this->helper->updateMappings($alias, $mapping, true); + $this->helper->updateMappings($alias, $mapping, true, true, true, false); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_2)); $this->assertEquals($mapping['properties']['viewType']['index'], $this->helper->getMappings($alias)['properties']['viewType']['index']); @@ -94,9 +122,9 @@ public function testUpdateMappingsWithIndexNotEmpty(string $alias) } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingWithSettingsNotEmpty(string $alias) + public function testUpdateMappingWithSettingsNotEmpty(string $alias, bool $includeTypeName) { $settings = [ 'number_of_shards' => 1, @@ -129,7 +157,35 @@ public function testUpdateMappingWithSettingsNotEmpty(string $alias) ] ]; - $mapping = [ + if ($includeTypeName) { + $mapping = [ + 'my_type' => [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] + ] + ]; + } else { + $mapping = [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] + ]; + } + + $mappingExpected = [ 'properties' => [ 'first_name' => [ 'type' => 'text', @@ -141,13 +197,14 @@ public function testUpdateMappingWithSettingsNotEmpty(string $alias) ] ]; + $this->helper->createIndexByAlias($alias); - $this->helper->updateSettings($alias, $settings); + $this->helper->updateSettings($alias, $settings, $includeTypeName); - $this->helper->updateMappings($alias, $mapping); + $this->helper->updateMappings($alias, $mapping, true, true, true, $includeTypeName); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_1)); - $this->assertEquals($mapping, $this->helper->getMappings($alias)); + $this->assertEquals($mappingExpected, $this->helper->getMappings($alias)); $resultSettings = $this->helper->getSettings($alias); $this->assertTrue(array_key_exists('analysis', $resultSettings)); @@ -157,14 +214,14 @@ public function testUpdateMappingWithSettingsNotEmpty(string $alias) } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingsIndexAlreadyExists(string $alias) + public function testUpdateMappingsIndexAlreadyExists(string $alias, bool $includeTypeName) { $this->helper->createIndexByAlias($alias); $this->createIndex2($alias); - $this->helper->updateMappings($alias, null); + $this->helper->updateMappings($alias, null, true, true, true, $includeTypeName); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_2)); @@ -172,13 +229,13 @@ public function testUpdateMappingsIndexAlreadyExists(string $alias) } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingsWithoutReindexation(string $alias) + public function testUpdateMappingsWithoutReindexation(string $alias, bool $includeTypeName) { $this->helper->createIndexByAlias($alias); - $this->assertEquals($this->helper::RETURN_ACKNOWLEDGE, $this->helper->updateMappings($alias, null, false, false)); + $this->assertEquals($this->helper::RETURN_ACKNOWLEDGE, $this->helper->updateMappings($alias, null, false, false, true, $includeTypeName)); $this->assertTrue($this->helper->existsIndex($alias)); $this->assertTrue($this->helper->existsIndex($alias . $this->helper::INDEX_NAME_CONVENTION_1));