From 4d896d972d94530e40b0ca89112e64b518f528db Mon Sep 17 00:00:00 2001 From: stevenp Date: Tue, 20 Mar 2018 17:32:14 +0100 Subject: [PATCH 1/2] [NodeSearchBundle] Elastic search version 6 support --- composer.json | 2 +- .../DependencyInjection/Configuration.php | 32 +++- .../KunstmaanNodeSearchExtension.php | 164 +++++++++++------- .../Helper/ElasticSearchUtil.php | 17 ++ .../Resources/doc/ElasticSearch.md | 50 ++++++ 5 files changed, 196 insertions(+), 69 deletions(-) create mode 100644 src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php create mode 100644 src/Kunstmaan/NodeSearchBundle/Resources/doc/ElasticSearch.md diff --git a/composer.json b/composer.json index 07ac62b027..2fb64b8242 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "twig/extensions": "~1.0", "egulias/email-validator": "~1.2", "box/spout": "^2.5", - "ruflin/elastica": "^5.1", + "ruflin/elastica": "^5.1|^6.0", "behat/transliterator": "~1.2", "defuse/php-encryption": "v2.1.0" }, diff --git a/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php b/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php index 8ee0186454..7db61a4534 100644 --- a/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php +++ b/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php @@ -5,6 +5,7 @@ use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; +use Kunstmaan\NodeSearchBundle\Helper\ElasticSearchUtil; /** * This is the class that validates and merges configuration from your app/config files @@ -28,16 +29,35 @@ public function getConfigTreeBuilder() /** @var ArrayNodeDefinition $properties */ $properties = $rootNode->children()->arrayNode('mapping')->useAttributeAsKey('name')->prototype('array'); - $properties->children()->scalarNode('type')->beforeNormalization()->ifNotInArray($types = [ - 'string', 'token_count', 'text', 'keyword', + $types = [ + 'token_count', 'text', 'keyword', 'float', 'double', 'byte', 'short', 'integer', 'long', 'date', 'boolean', 'binary', - ])->thenInvalid('type must be one of: ' . implode(', ', $types)); - $properties->children()->scalarNode('index')->beforeNormalization()->ifNotInArray(['analyzed', 'not_analyzed', 'no']) - ->thenInvalid("index must be one of: analyzed, not_analyzed, no"); - $properties->children()->booleanNode('include_in_all'); + ]; + if (!ElasticSearchUtil::useVersion6()) { + $types[] = 'string'; + } + + $properties->children()->scalarNode('type')->beforeNormalization()->ifNotInArray($types)->thenInvalid('type must be one of: ' . implode(', ', $types)); + + if (ElasticSearchUtil::useVersion6()) { + $properties->children()->booleanNode('fielddata'); + $properties->children() + ->scalarNode('index') + ->beforeNormalization() + ->ifNotInArray(['true', 'false', true, false]) + ->thenInvalid("index must be one of: true, false"); + } else { + $properties->children() + ->scalarNode('index') + ->beforeNormalization() + ->ifNotInArray(['analyzed', 'not_analyzed', 'no']) + ->thenInvalid("index must be one of: analyzed, not_analyzed, no"); + $properties->children()->booleanNode('include_in_all'); + } + $properties->children()->booleanNode('store'); $properties->children()->floatNode('boost'); $properties->children()->scalarNode('null_value'); diff --git a/src/Kunstmaan/NodeSearchBundle/DependencyInjection/KunstmaanNodeSearchExtension.php b/src/Kunstmaan/NodeSearchBundle/DependencyInjection/KunstmaanNodeSearchExtension.php index 124eb7cb3b..821fbcaea2 100644 --- a/src/Kunstmaan/NodeSearchBundle/DependencyInjection/KunstmaanNodeSearchExtension.php +++ b/src/Kunstmaan/NodeSearchBundle/DependencyInjection/KunstmaanNodeSearchExtension.php @@ -7,6 +7,7 @@ use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Kunstmaan\NodeSearchBundle\Helper\ElasticSearchUtil; /** * This is the class that loads and manages your bundle configuration @@ -48,67 +49,106 @@ public function load(array $configs, ContainerBuilder $container) */ public function prepend(ContainerBuilder $container) { - $container->prependExtensionConfig('kunstmaan_node_search', [ - 'mapping' => [ - 'root_id' => [ - 'type' => 'integer', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'node_id' => [ - 'type' => 'integer', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'nodetranslation_id' => [ - 'type' => 'integer', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'nodeversion_id' => [ - 'type' => 'integer', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'title' => [ - 'type' => 'string', - 'include_in_all' => true - ], - 'slug' => [ - 'type' => 'string', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'type' => [ - 'type' => 'string', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'page_class' => [ - 'type' => 'string', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'content' => [ - 'type' => 'string', - 'include_in_all' => true - ], - 'created' => [ - 'type' => 'date', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'updated' => [ - 'type' => 'date', - 'include_in_all' => false, - 'index' => 'not_analyzed' - ], - 'view_roles' => [ - 'type' => 'string', - 'include_in_all' => true, - 'index' => 'not_analyzed', - ], - ] - ]); + if (ElasticSearchUtil::useVersion6()) { + $mapping = [ + 'mapping' => [ + 'root_id' => [ + 'type' => 'integer', + ], + 'node_id' => [ + 'type' => 'integer', + ], + 'nodetranslation_id' => [ + 'type' => 'integer', + ], + 'nodeversion_id' => [ + 'type' => 'integer', + ], + 'title' => [ + 'type' => 'text', + ], + 'slug' => [ + 'type' => 'text', + ], + 'type' => [ + 'type' => 'keyword', + ], + 'page_class' => [ + 'type' => 'keyword', + ], + 'content' => [ + 'type' => 'text', + ], + 'view_roles' => [ + 'type' => 'keyword', + ], + ] + ]; + } else { + $mapping = [ + 'mapping' => [ + 'root_id' => [ + 'type' => 'integer', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'node_id' => [ + 'type' => 'integer', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'nodetranslation_id' => [ + 'type' => 'integer', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'nodeversion_id' => [ + 'type' => 'integer', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'title' => [ + 'type' => 'string', + 'include_in_all' => true + ], + 'slug' => [ + 'type' => 'string', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'type' => [ + 'type' => 'string', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'page_class' => [ + 'type' => 'string', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'content' => [ + 'type' => 'string', + 'include_in_all' => true + ], + 'created' => [ + 'type' => 'date', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'updated' => [ + 'type' => 'date', + 'include_in_all' => false, + 'index' => 'not_analyzed' + ], + 'view_roles' => [ + 'type' => 'string', + 'include_in_all' => true, + 'index' => 'not_analyzed', + ], + ] + ]; + } + + $container->prependExtensionConfig('kunstmaan_node_search', $mapping); } } diff --git a/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php b/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php new file mode 100644 index 0000000000..74c2f6c1df --- /dev/null +++ b/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php @@ -0,0 +1,17 @@ += 6.0. + +## Dynamic configuration +We have added dynamic support to detect which version your project is using. +Dependancies for using the configuration of version 6 is that your project uses PHP 7, +that ruflin/elastica >= 6.0 is used and that it uses the package ocramius/package-versions. +This last package will generate a list of package versions when composer is used. + + +## Difference between versions + +The biggest change in version 6, is that Elastica has dropped support for the "_all" meta field. +(see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html) + +When using version 6.x, then you will no longer be able to use the "include_in_all" and "index => 'not_analysed". +use 'index => false' instead. Also some of the index types have been changed and some fields are no longer indexed (like the 'created' and 'updated' fields). + +### Example + +Some examples of the mapping that have changed (for the kunstmaan_node_search extension): + +``` +'node_id' => [ + 'type' => 'integer', + 'include_in_all' => false, + 'index' => 'not_analyzed' +], +... +'view_roles' => [ + 'type' => 'string', + 'include_in_all' => true, + 'index' => 'not_analyzed', +], +``` + +in + + +``` +'node_id' => [ + 'type' => 'integer', +], +... +'view_roles' => [ + 'type' => 'keyword', +], +``` From 888c7e6c91273a47e83ea866b552904479e22aa3 Mon Sep 17 00:00:00 2001 From: stevenp Date: Mon, 26 Mar 2018 17:32:32 +0200 Subject: [PATCH 2/2] [NodeSearchBundle] Elastic search version 6 support --- .../DependencyInjection/Configuration.php | 1 + .../NodeSearchBundle/Helper/ElasticSearchUtil.php | 2 +- .../NodeSearchBundle/Resources/doc/ElasticSearch.md | 12 ++++++------ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php b/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php index 7db61a4534..6a6a49feca 100644 --- a/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php +++ b/src/Kunstmaan/NodeSearchBundle/DependencyInjection/Configuration.php @@ -44,6 +44,7 @@ public function getConfigTreeBuilder() if (ElasticSearchUtil::useVersion6()) { $properties->children()->booleanNode('fielddata'); + $properties->children()->booleanNode('doc_values'); $properties->children() ->scalarNode('index') ->beforeNormalization() diff --git a/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php b/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php index 74c2f6c1df..da7dcb1674 100644 --- a/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php +++ b/src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php @@ -12,6 +12,6 @@ final class ElasticSearchUtil */ public static function useVersion6() { - return (PHP_VERSION[0] == 7 && !class_exists('\Elastica\Tool\CrossIndex')); + return (PHP_MAJOR_VERSION == 7 && !class_exists('\Elastica\Tool\CrossIndex')); } } \ No newline at end of file diff --git a/src/Kunstmaan/NodeSearchBundle/Resources/doc/ElasticSearch.md b/src/Kunstmaan/NodeSearchBundle/Resources/doc/ElasticSearch.md index ceacc8156d..26ef160c67 100644 --- a/src/Kunstmaan/NodeSearchBundle/Resources/doc/ElasticSearch.md +++ b/src/Kunstmaan/NodeSearchBundle/Resources/doc/ElasticSearch.md @@ -1,14 +1,10 @@ # Elastic search support We have added support for Elastic search up to version 6.x. -When you have a project that is using PHP 7 then you can use version 6.x when the project is using ruflin/elastica >= 6.0. ## Dynamic configuration -We have added dynamic support to detect which version your project is using. -Dependancies for using the configuration of version 6 is that your project uses PHP 7, -that ruflin/elastica >= 6.0 is used and that it uses the package ocramius/package-versions. -This last package will generate a list of package versions when composer is used. - +For the mapping and configuration we detect which version of the library your project is using. +Dependencies for using the configuration of version 6 is that your project uses PHP 7 and ruflin/elastica >= 6.0. ## Difference between versions @@ -48,3 +44,7 @@ in 'type' => 'keyword', ], ``` + +### Link to elastic search documentation + +https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html \ No newline at end of file