diff --git a/.circleci/config.yml b/.circleci/config.yml index e4af13e..2e44002 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,9 @@ jobs: build: docker: - image: circleci/php:7.2.9-apache-stretch-node-browsers - - image: docker.elastic.co/elasticsearch/elasticsearch:6.0.0 + - image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1 + environment: + - discovery.type=single-node working_directory: ~/repo steps: - checkout diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fe85fd..3afe9ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Changelog ========= +## [v7.0.0](https://github.com/Nexucis/es-php-index-helper/tree/7.0.0) +* Upgrade elasticsearch-php dependency version from 6.0.X to 7.0.1 +* Requirement of PHP 7.1 instead of 7.0 that is not supported since 1st Jan 2019 +* **[Public]** add a new parameter `$includeTypeName` in the method `updateMappings`, following the [elasticsearch recommendation](https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0) + ## [v6.1.1](https://github.com/Nexucis/es-php-index-helper/tree/6.1.1) ### Feature @@ -16,7 +21,7 @@ Changelog ## [v6.0.0](https://github.com/Nexucis/es-php-index-helper/tree/6.0.0) ### Feature -* Upgrade elasticsearch-php dependencie version from 5.3.1 to 6.0.0 +* Upgrade elasticsearch-php dependency version from 5.3.1 to 6.0.0 * **[Public]** Add new method `deleteIndexByAlias` which cannot be taken an index as parameter ### Breaking Changes diff --git a/README.md b/README.md index 2b0dd16..4a168ba 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # Elasticsearch Index Helper for php -[![CircleCI](https://circleci.com/gh/Nexucis/es-php-index-helper.svg?style=shield)](https://circleci.com/gh/Nexucis/es-php-index-helper) [![codecov](https://codecov.io/gh/Nexucis/es-php-index-helper/branch/master/graph/badge.svg)](https://codecov.io/gh/Nexucis/es-php-index-helper) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) - -[![Latest Stable Version](https://poser.pugx.org/nexucis/es-index-helper/v/stable)](https://packagist.org/packages/nexucis/es-index-helper) [![Latest Unstable Version](https://poser.pugx.org/nexucis/es-index-helper/v/unstable)](https://packagist.org/packages/nexucis/es-index-helper)[![Total Downloads](https://poser.pugx.org/nexucis/es-index-helper/downloads)](https://packagist.org/packages/nexucis/es-index-helper) +[![CircleCI](https://circleci.com/gh/Nexucis/es-php-index-helper.svg?style=shield)](https://circleci.com/gh/Nexucis/es-php-index-helper) [![codecov](https://codecov.io/gh/Nexucis/es-php-index-helper/branch/master/graph/badge.svg)](https://codecov.io/gh/Nexucis/es-php-index-helper) [![Latest Stable Version](https://poser.pugx.org/nexucis/es-index-helper/v/stable)](https://packagist.org/packages/nexucis/es-index-helper) [![Total Downloads](https://poser.pugx.org/nexucis/es-index-helper/downloads)](https://packagist.org/packages/nexucis/es-index-helper) 1. [Overview](#overview) 2. [Installation](#installation) @@ -32,7 +30,8 @@ Where : | Elasticsearch Version | Support Branch | | --------------------- | --------------- | -| >= 6.0 | master | +| >= 7.0 | master | +| >= 6.0, < 7.0 | support/6.0 | | >= 5.0, < 6.0 | support/5.0 | | >= 2.0, < 5.0 | support/2.0 | @@ -132,6 +131,35 @@ So to make this things possible we need to: It takes a lot of steps and verifications to check the update is done successfully. That's why this Helper comes here with the following simplify method : +```php + [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] +]; +$helper->updateMappings($alias, $mappings, false, true, true, false); +``` + +You just need to provide the alias name and the new mapping and that's it. + +:warning: With an index with many documents, the update can take a lot of time. That's why it's better: + +* With ElasticSearch `2.4`, to call this method in an asynchronous process.. +* With ElasticSearch `5` or greater, to set the parameter `$waitForCompletion` to false. It will return taskID, which can then be used with the [_task api](https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html) + +:warning: Elasticsearch has started to remove the document type in the mapping. Following the [recommendation](https://www.elastic.co/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0), +the method `updateMappings` has a new parameter `$includeTypeName` which allows to support the old mapping format + +By default, the method `updateMappings` will consider the old mapping format which is for example : + ```php [ 'type' => 'integer' - ] ] + ] ] ]; $helper->updateMappings($alias, $mappings); ``` -You just need to provide the alias name and the new mapping and that's it. - -:warning: With an index with many documents, the update can take a lot of time. That's why it's better: +If you want to move to the new format, you have to remove the type in the mapping and set to false the parameter `$includeTypeName` -* With ElasticSearch `2.4`, to call this method in an asynchronous process.. -* With ElasticSearch `5` or greater, to set the parameter `$waitForCompletion` to false. It will return taskID, which can then be used with the [_task api](https://www.elastic.co/guide/en/elasticsearch/reference/current/tasks.html) - +Please be aware that the parameter `$includeTypeName` will be removed in the next major version ### Settings Operation Indices settings can be updated the same way as mapping using the `updateSettings` method: @@ -224,10 +248,10 @@ Any contribution or suggestion would be really appreciated. Feel free to use the All following tools are running by [circleci](https://circleci.com/gh/Nexucis/es-php-index-helper), so in order to help you to improve your code and make easier your life, here it is how you can launch the tools with the correct parameter. ### Run unit test -If you want to launch the unit test, you need to have a local elasticsearch instance which must be accessible through the url http://localhost:9200. A simply way to launch it, is to start the [corresponding container](https://hub.docker.com/_/elasticsearch/) : +If you want to launch the unit test, you need to have a local elasticsearch instance which must be accessible through the url http://localhost:9200. A simply way to launch it, is to start the [corresponding container](https://www.docker.elastic.co/) : ```bash -docker run -d -p 9200:9200 docker.elastic.co/elasticsearch/elasticsearch:6.0.0 +docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.1.1 ``` Once ElasticSearch is up, you can run the following command : diff --git a/composer.json b/composer.json index c2d54df..bea9adb 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,8 @@ } ], "require": { - "php": "^7.0", - "elasticsearch/elasticsearch": "^6.0.1" + "php": "^7.1", + "elasticsearch/elasticsearch": "^7.0.1" }, "require-dev": { "phpunit/phpunit": "^6.4", diff --git a/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php b/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php index 3eb291a..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) { @@ -405,7 +408,7 @@ public function getSettings($alias) * @param $type string [REQUIRED] the type of the document * @param $id string|int [REQUIRED] the document ID * @param $refresh bool - * @return array + * @return callable|array * @throws IndexNotFoundException */ public function getDocument($alias, $type, $id, $refresh = false) @@ -428,7 +431,7 @@ 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) - * @return array + * @return callable|array * @throws IndexNotFoundException */ public function getAllDocuments($alias, $from = 0, $size = 10) @@ -439,10 +442,10 @@ public function getAllDocuments($alias, $from = 0, $size = 10) /** * @param string $alias [REQUIRED] * @param array|null $query - * @param string $type + * @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 array + * @return callable|array * @throws IndexNotFoundException */ public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10) @@ -467,10 +470,10 @@ public function searchDocuments($alias, $query = null, $type = null, $from = 0, /** * @param $alias [REQUIRED] - * @param string $type + * @param string $type . This parameter will be removed in the next major release * @param array|null $body * @param SearchParameter $searchParameter - * @return array + * @return callable|array * @throws IndexNotFoundException */ public function advancedSearchDocument($alias, $type = null, $body = null, $searchParameter = null) @@ -734,7 +737,6 @@ protected function copyMappingAndSetting($indexSource, $indexDest) $this->copySettings($params, $settingSource); - $this->client->indices()->create($params); } @@ -827,7 +829,7 @@ protected function copyDocuments($indexSrc, $indexDest, $refresh = false, $waitF /** * @param string $index - * @return array + * @return callable|array */ protected function getSettingsByIndex($index) { @@ -839,7 +841,7 @@ protected function getSettingsByIndex($index) /** * @param string $index - * @return array + * @return callable|array */ protected function getMappingsByIndex($index) { diff --git a/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelperInterface.php b/src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelperInterface.php index 927349e..eaa957a 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 @@ -137,10 +135,10 @@ public function getSettings($alias); /** * @param $alias string [REQUIRED] the name of the index or the name of the alias - * @param $type string [REQUIRED] the type of the document + * @param $type string [REQUIRED] the type of the document. This parameter will be removed in the next major release * @param $id string|int [REQUIRED] the document ID * @param $refresh bool - * @return array + * @return callable|array * @throws IndexNotFoundException */ public function getDocument($alias, $type, $id, $refresh = false); @@ -149,28 +147,28 @@ 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) + * @return callable|array * @throws IndexNotFoundException - * @return array */ public function getAllDocuments($alias, $from = 0, $size = 10); /** * @param string $alias [REQUIRED] * @param array|null $query - * @param string $type + * @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 * @throws IndexNotFoundException - * @return array */ public function searchDocuments($alias, $query = null, $type = null, $from = 0, $size = 10); /** * @param $alias - * @param string $type + * @param string $type. This parameter will be removed in the next major release * @param array|null $body * @param SearchParameter $searchParameter - * @return array + * @return callable|array * @throws IndexNotFoundException */ public function advancedSearchDocument($alias, $type = null, $body = null, $searchParameter = null); @@ -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 string $type [REQUIRED] + * @param string $type [REQUIRED]. This parameter will be removed in the next major release * @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 string $type [REQUIRED] + * @param string $type [REQUIRED]. This parameter will be removed in the next major release * @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 string $type [REQUIRED] + * @param string $type [REQUIRED]. This parameter will be removed in the next major release * @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 ca5db86..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; @@ -71,11 +93,11 @@ protected function createIndex2($alias) $this->client->indices()->create($params); } - protected function loadFinancialIndex($alias, $type = 'complains') + protected function loadFinancialIndex($alias) { $this->helper->createIndexByAlias($alias); - $this->addBulkDocuments($this->jsonArrayToBulkArray(self::$documents, $alias, $type)); + $this->addBulkDocuments($this->jsonArrayToBulkArray(self::$documents, $alias)); } /** @@ -95,14 +117,13 @@ private function addBulkDocuments($params) $this->client->bulk($params); } - private function jsonArrayToBulkArray($documents, $index, $type) + private function jsonArrayToBulkArray($documents, $index) { $params = array(); foreach ($documents as $document) { $params['body'][] = [ 'index' => [ '_index' => $index, - '_type' => $type, ] ]; $params['body'][] = $document; diff --git a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/DocumentActionTest.php b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/DocumentActionTest.php index 1532ead..1ef8bc0 100644 --- a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/DocumentActionTest.php +++ b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/DocumentActionTest.php @@ -202,7 +202,7 @@ public function testGetAllDocumentIndexEmpty(string $alias) $this->helper->createIndexByAlias($alias); $result = $this->helper->getAllDocuments($alias); - $this->assertTrue($result['hits']['total'] === 0); + $this->assertTrue($result['hits']['total']['value'] === 0); $this->assertTrue(count($result['hits']['hits']) === 0); } @@ -216,7 +216,7 @@ public function testGetAllDocument(string $alias) $result = $this->helper->getAllDocuments($alias); - $this->assertTrue($result['hits']['total'] > 10); + $this->assertTrue($result['hits']['total']['value'] > 10); $this->assertTrue(count($result['hits']['hits']) === 10); } @@ -237,7 +237,7 @@ public function testSearchDocumentsIndexEmpty(string $alias) $this->helper->createIndexByAlias($alias); $result = $this->helper->searchDocuments($alias); - $this->assertTrue($result['hits']['total'] === 0); + $this->assertTrue($result['hits']['total']['value'] === 0); $this->assertTrue(count($result['hits']['hits']) === 0); } @@ -246,17 +246,16 @@ public function testSearchDocumentsIndexEmpty(string $alias) */ public function testSearchDocuments(string $alias) { - $type = 'complains'; // create index with some contents - $this->loadFinancialIndex($alias, $type); + $this->loadFinancialIndex($alias); $query = array( 'match_all' => new stdClass() ); - $result = $this->helper->searchDocuments($alias, $query, $type); + $result = $this->helper->searchDocuments($alias, $query); - $this->assertTrue($result['hits']['total'] > 10); + $this->assertTrue($result['hits']['total']['value'] > 10); $this->assertTrue(count($result['hits']['hits']) === 10); } @@ -265,13 +264,12 @@ public function testSearchDocuments(string $alias) */ public function testSearchDocumentsWithSource(string $alias) { - $type = 'complains'; $expectedFields = array( 'name', 'id' ); // create index with some contents - $this->loadFinancialIndex($alias, $type); + $this->loadFinancialIndex($alias); $body = array( 'query' => array( @@ -281,7 +279,7 @@ public function testSearchDocumentsWithSource(string $alias) $result = $this->helper->advancedSearchDocument( $alias, - $type, + null, $body, (new SearchParameter()) ->from(0) @@ -289,7 +287,7 @@ public function testSearchDocumentsWithSource(string $alias) ->includeSource($expectedFields) ); - $this->assertTrue($result['hits']['total'] > 10); + $this->assertTrue($result['hits']['total']['value'] > 10); $this->assertTrue(count($result['hits']['hits']) === 10); foreach ($result['hits']['hits'] as $item) { diff --git a/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/MappingsActionTest.php b/tests/Nexucis/Tests/Elasticsearch/Helper/Nodowntime/MappingsActionTest.php index f4095a8..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,12 +44,26 @@ public function testUpdateMappingsIndexNotFoundException() } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingsBasicData(string $alias) + public function testUpdateMappingsBasicData(string $alias, bool $includeTypeName) { - $mapping = [ - 'my_type' => [ + if ($includeTypeName) { + $mapping = [ + 'my_type' => [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] + ] + ]; + } else { + $mapping = [ 'properties' => [ 'first_name' => [ 'type' => 'text', @@ -59,15 +73,27 @@ public function testUpdateMappingsBasicData(string $alias) 'type' => 'integer' ] ] + ]; + } + + $mappingExpected = [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] ] ]; $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)); } /** @@ -75,33 +101,30 @@ public function testUpdateMappingsBasicData(string $alias) */ public function testUpdateMappingsWithIndexNotEmpty(string $alias) { - $type = 'complains'; // create index with some contents - $this->loadFinancialIndex($alias, $type); + $this->loadFinancialIndex($alias); $mapping = [ - $type => [ - 'properties' => [ - 'viewType' => [ - 'type' => 'text', - 'index' => false - ] + 'properties' => [ + 'viewType' => [ + 'type' => 'text', + 'index' => false ] ] ]; - $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[$type]['properties']['viewType']['index'], $this->helper->getMappings($alias)[$type]['properties']['viewType']['index']); + $this->assertEquals($mapping['properties']['viewType']['index'], $this->helper->getMappings($alias)['properties']['viewType']['index']); $this->assertTrue($this->countDocuments($alias) > 0); } /** - * @dataProvider aliasDataProvider + * @dataProvider aliasDataProviderWithTypeName */ - public function testUpdateMappingWithSettingsNotEmpty(string $alias) + public function testUpdateMappingWithSettingsNotEmpty(string $alias, bool $includeTypeName) { $settings = [ 'number_of_shards' => 1, @@ -134,8 +157,22 @@ public function testUpdateMappingWithSettingsNotEmpty(string $alias) ] ]; - $mapping = [ - 'my_type' => [ + if ($includeTypeName) { + $mapping = [ + 'my_type' => [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] + ] + ] + ]; + } else { + $mapping = [ 'properties' => [ 'first_name' => [ 'type' => 'text', @@ -145,16 +182,29 @@ public function testUpdateMappingWithSettingsNotEmpty(string $alias) 'type' => 'integer' ] ] + ]; + } + + $mappingExpected = [ + 'properties' => [ + 'first_name' => [ + 'type' => 'text', + 'analyzer' => 'standard' + ], + 'age' => [ + 'type' => 'integer' + ] ] ]; + $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)); @@ -164,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)); @@ -179,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));