From e361b7c53b4f1381c0551b1c20f12b0b915b5145 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Sun, 4 Jan 2015 21:58:53 +1100 Subject: [PATCH 1/2] Implement additional configuration options for types. --- CHANGELOG-3.1.md | 4 +++- Configuration/TypeConfig.php | 24 ++++++++++++++++++++ DependencyInjection/Configuration.php | 3 +++ DependencyInjection/FOSElasticaExtension.php | 3 +++ Index/MappingBuilder.php | 20 ++++++++++------ Index/Resetter.php | 3 +++ Tests/Functional/MappingToElasticaTest.php | 4 ++++ Tests/Functional/app/Basic/config.yml | 3 +++ Tests/Functional/app/Serializer/config.yml | 2 +- 9 files changed, 57 insertions(+), 9 deletions(-) diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 0ce44ad..a03de37 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -14,6 +14,8 @@ https://github.com/FriendsOfSymfony/FOSElasticaBundle/compare/v3.0.4...v3.1.0 * BC BREAK: `Doctrine\Listener#scheduleForDeletion` access changed to private. * BC BREAK: `ObjectPersisterInterface` gains the method `handlesObject` that returns a boolean value if it will handle a given object or not. - * Removed `Doctrine\Listener#getSubscribedEvents`. The container + * BC BREAK: Removed `Doctrine\Listener#getSubscribedEvents`. The container configuration now configures tags with the methods to call to avoid loading this class on every request where doctrine is active. + * Added ability to configure `date_detection`, `numeric_detection` and + `dynamic_date_formats` for types. diff --git a/Configuration/TypeConfig.php b/Configuration/TypeConfig.php index fc9041d..a46cd34 100644 --- a/Configuration/TypeConfig.php +++ b/Configuration/TypeConfig.php @@ -35,6 +35,22 @@ class TypeConfig $this->name = $name; } + /** + * @return bool|null + */ + public function getDateDetection() + { + return $this->getConfig('date_detection'); + } + + /** + * @return array + */ + public function getDynamicDateFormats() + { + return $this->getConfig('dynamic_date_formats'); + } + /** * @return string|null */ @@ -61,6 +77,14 @@ class TypeConfig null; } + /** + * @return bool|null + */ + public function getNumericDetection() + { + return $this->getConfig('numeric_detection'); + } + /** * @return string */ diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index b204f0c..760966e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -225,7 +225,10 @@ class Configuration implements ConfigurationInterface }) ->end() ->children() + ->booleanNode('date_detection')->end() + ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() ->scalarNode('index_analyzer')->end() + ->booleanNode('numeric_detection')->end() ->scalarNode('search_analyzer')->end() ->variableNode('indexable_callback')->end() ->append($this->getPersistenceNode()) diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 565ba33..529bd29 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -241,6 +241,9 @@ class FOSElasticaExtension extends Extension 'serializer', 'index_analyzer', 'search_analyzer', + 'date_detection', + 'dynamic_date_formats', + 'numeric_detection', ) as $field) { $typeConfig['config'][$field] = array_key_exists($field, $type) ? $type[$field] : diff --git a/Index/MappingBuilder.php b/Index/MappingBuilder.php index 21ae871..996db5f 100644 --- a/Index/MappingBuilder.php +++ b/Index/MappingBuilder.php @@ -58,13 +58,19 @@ class MappingBuilder */ public function buildTypeMapping(TypeConfig $typeConfig) { - $mapping = array_merge($typeConfig->getMapping(), array( - // 'date_detection' => true, - // 'dynamic_date_formats' => array() - // 'dynamic_templates' => $typeConfig->getDynamicTemplates(), - // 'numeric_detection' => false, - // 'properties' => array(), - )); + $mapping = $typeConfig->getMapping(); + + if (null !== $typeConfig->getDynamicDateFormats()) { + $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); + } + + if (null !== $typeConfig->getDateDetection()) { + $mapping['date_detection'] = $typeConfig->getDateDetection(); + } + + if (null !== $typeConfig->getNumericDetection()) { + $mapping['numeric_detection'] = $typeConfig->getNumericDetection(); + } if ($typeConfig->getIndexAnalyzer()) { $mapping['index_analyzer'] = $typeConfig->getIndexAnalyzer(); diff --git a/Index/Resetter.php b/Index/Resetter.php index 9b65a8f..68a43dd 100644 --- a/Index/Resetter.php +++ b/Index/Resetter.php @@ -42,6 +42,9 @@ class Resetter /** * Deletes and recreates all indexes + * + * @param bool $populating + * @param bool $force */ public function resetAllIndexes($populating = false, $force = false) { diff --git a/Tests/Functional/MappingToElasticaTest.php b/Tests/Functional/MappingToElasticaTest.php index f42df61..229a1a2 100644 --- a/Tests/Functional/MappingToElasticaTest.php +++ b/Tests/Functional/MappingToElasticaTest.php @@ -49,6 +49,9 @@ class MappingToElasticaTest extends WebTestCase $mapping = $type->getMapping(); $this->assertNotEmpty($mapping, 'Mapping was populated'); + $this->assertFalse($mapping['type']['date_detection']); + $this->assertTrue($mapping['type']['numeric_detection']); + $this->assertEquals(array('yyyy-MM-dd'), $mapping['type']['dynamic_date_formats']); $this->assertArrayHasKey('store', $mapping['type']['properties']['field1']); $this->assertTrue($mapping['type']['properties']['field1']['store']); $this->assertArrayNotHasKey('store', $mapping['type']['properties']['field2']); @@ -105,6 +108,7 @@ class MappingToElasticaTest extends WebTestCase /** * @param Client $client + * @param string $type * @return \Elastica\Type */ private function getType(Client $client, $type = 'type') diff --git a/Tests/Functional/app/Basic/config.yml b/Tests/Functional/app/Basic/config.yml index 3c3d369..690bf5e 100644 --- a/Tests/Functional/app/Basic/config.yml +++ b/Tests/Functional/app/Basic/config.yml @@ -46,6 +46,8 @@ fos_elastica: index_analyzer: my_analyzer type: search_analyzer: my_analyzer + date_detection: false + dynamic_date_formats: [ 'yyyy-MM-dd' ] dynamic_templates: - dates: match: "date_*" @@ -56,6 +58,7 @@ fos_elastica: mapping: analyzer: english type: string + numeric_detection: true properties: field1: ~ field2: diff --git a/Tests/Functional/app/Serializer/config.yml b/Tests/Functional/app/Serializer/config.yml index 9bea4ba..de7caec 100644 --- a/Tests/Functional/app/Serializer/config.yml +++ b/Tests/Functional/app/Serializer/config.yml @@ -28,7 +28,7 @@ fos_elastica: serializer: ~ indexes: index: - index_name: foselastica_test_%kernel.environment% + index_name: foselastica_ser_test_%kernel.environment% types: type: properties: From 92aab4bcf66ccb992195ad574457d706fcad63e6 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Sun, 4 Jan 2015 22:02:54 +1100 Subject: [PATCH 2/2] Add PR# to changelog --- CHANGELOG-3.1.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index a03de37..8880675 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -16,6 +16,6 @@ https://github.com/FriendsOfSymfony/FOSElasticaBundle/compare/v3.0.4...v3.1.0 returns a boolean value if it will handle a given object or not. * BC BREAK: Removed `Doctrine\Listener#getSubscribedEvents`. The container configuration now configures tags with the methods to call to avoid loading - this class on every request where doctrine is active. + this class on every request where doctrine is active. #729 * Added ability to configure `date_detection`, `numeric_detection` and - `dynamic_date_formats` for types. + `dynamic_date_formats` for types. #753