Skip to content

Commit

Permalink
Merge pull request #17 from Nexucis/feature/elastic-v7
Browse files Browse the repository at this point in the history
moving to elasticsearch v7
  • Loading branch information
Nexucis committed Jun 23, 2019
2 parents b9cf6a8 + c6d7b6e commit add8501
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 100 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 |

Expand Down Expand Up @@ -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
<?php
$alias = "myindex";
$mappings = [
'properties' => [
'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
<?php
$alias = "myindex";
Expand All @@ -144,20 +172,16 @@ $mappings = [
],
'age' => [
'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:

Expand Down Expand Up @@ -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 :
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 17 additions & 15 deletions src/Nexucis/Elasticsearch/Helper/Nodowntime/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -734,7 +737,6 @@ protected function copyMappingAndSetting($indexSource, $indexDest)

$this->copySettings($params, $settingSource);


$this->client->indices()->create($params);
}

Expand Down Expand Up @@ -827,7 +829,7 @@ protected function copyDocuments($indexSrc, $indexDest, $refresh = false, $waitF

/**
* @param string $index
* @return array
* @return callable|array
*/
protected function getSettingsByIndex($index)
{
Expand All @@ -839,7 +841,7 @@ protected function getSettingsByIndex($index)

/**
* @param string $index
* @return array
* @return callable|array
*/
protected function getMappingsByIndex($index)
{
Expand Down
Loading

0 comments on commit add8501

Please sign in to comment.