From e2cc4f965b370f1c153e07f36e47ef1a1bf2663c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Piechota?= Date: Thu, 22 Nov 2012 11:30:13 +0100 Subject: [PATCH 01/13] Fix register custom finder for entity --- DependencyInjection/FOQElasticaExtension.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 92310cc..434339b 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -363,15 +363,17 @@ class FOQElasticaExtension extends Extension protected function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, $typeDef, $indexName, $typeName) { if (isset($typeConfig['finder']['service'])) { - return $typeConfig['finder']['service']; + $finderId = $typeConfig['finder']['service']; } - $abstractFinderId = 'foq_elastica.finder.prototype'; - $finderId = sprintf('foq_elastica.finder.%s.%s', $indexName, $typeName); - $finderDef = new DefinitionDecorator($abstractFinderId); - $finderDef->replaceArgument(0, $typeDef); - $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); - $container->setDefinition($finderId, $finderDef); - + else{ + $abstractFinderId = 'foq_elastica.finder.prototype'; + $finderId = sprintf('foq_elastica.finder.%s.%s', $indexName, $typeName); + $finderDef = new DefinitionDecorator($abstractFinderId); + $finderDef->replaceArgument(0, $typeDef); + $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); + $container->setDefinition($finderId, $finderDef); + } + $managerId = sprintf('foq_elastica.manager.%s', $typeConfig['driver']); $managerDef = $container->getDefinition($managerId); $arguments = array( $typeConfig['model'], new Reference($finderId)); From ea921c8abe24644f71894ab80d35f0c8050a8b6a Mon Sep 17 00:00:00 2001 From: micjohnson Date: Wed, 12 Dec 2012 06:23:54 -0800 Subject: [PATCH 02/13] Update DependencyInjection/Configuration.php Adding _routing for types --- DependencyInjection/Configuration.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6aa179d..83656a9 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -247,6 +247,7 @@ class Configuration ->append($this->getMappingsNode()) ->append($this->getSourceNode()) ->append($this->getBoostNode()) + ->append($this->getRoutingNode()) ->end() ; @@ -373,4 +374,22 @@ class Configuration return $node; } + + /** + * Returns the array node used for "_routing". + */ + protected function getRoutingNode() + { + $builder = new TreeBuilder(); + $node = $builder->root('_routing'); + + $node + ->children() + ->scalarNode('required')->end() + ->scalarNode('path')->end() + ->end() + ; + + return $node; + } } From d9367c7ed888e2987b0c940d5e2aa509dd257275 Mon Sep 17 00:00:00 2001 From: micjohnson Date: Wed, 12 Dec 2012 06:25:55 -0800 Subject: [PATCH 03/13] Update DependencyInjection/FOQElasticaExtension.php --- DependencyInjection/FOQElasticaExtension.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 92310cc..9023e0e 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -173,6 +173,9 @@ class FOQElasticaExtension extends Extension if (isset($type['_boost'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['_boost'] = $type['_boost']; } + if (isset($type['_routing'])) { + $this->indexConfigs[$indexName]['config']['mappings'][$name]['_routing'] = $type['_routing']; + } if (isset($type['mappings'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['properties'] = $type['mappings']; $typeName = sprintf('%s/%s', $indexName, $name); From b2314f21c67b156b8bf9dbe6a041b1160c40f687 Mon Sep 17 00:00:00 2001 From: micjohnson Date: Wed, 12 Dec 2012 07:49:37 -0800 Subject: [PATCH 04/13] Update DependencyInjection/Configuration.php add access to compression and disabling of _source --- DependencyInjection/Configuration.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6aa179d..1750b42 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -350,6 +350,9 @@ class Configuration ->useAttributeAsKey('name') ->prototype('scalar')->end() ->end() + ->scalarNode('compress')->end() + ->scalarNode('compress_threshold')->end() + ->scalarNode('enabled')->end() ->end() ; From 93236752c4f9081b0e3b1e97996c07ef0296b528 Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Fri, 4 Jan 2013 14:02:50 +0000 Subject: [PATCH 05/13] Adding configuring index name separately from service name --- DependencyInjection/Configuration.php | 1 + DependencyInjection/FOQElasticaExtension.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6aa179d..1b4749a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -102,6 +102,7 @@ class Configuration ->prototype('array') ->performNoDeepMerging() ->children() + ->scalarNode('index_name')->end() ->scalarNode('client')->end() ->scalarNode('finder') ->treatNullLike(true) diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 92310cc..01290c5 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -100,7 +100,8 @@ class FOQElasticaExtension extends Extension $clientId = $clientIdsByName[$clientName]; $indexId = sprintf('foq_elastica.index.%s', $name); - $indexDefArgs = array($name); + $indexName = isset($index['index_name']) ? $index['index_name'] : $name; + $indexDefArgs = array($indexName); $indexDef = new Definition('%foq_elastica.index.class%', $indexDefArgs); $indexDef->setFactoryService($clientId); $indexDef->setFactoryMethod('getIndex'); From 4055ff305f7652f51ab5d7827282929be20e77c9 Mon Sep 17 00:00:00 2001 From: Lea Haensenberger Date: Thu, 10 Jan 2013 15:00:21 +0100 Subject: [PATCH 06/13] Adding the fields configuration to the properties so multi_field fields can also be defined in properties --- DependencyInjection/Configuration.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6aa179d..01a9748 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -322,6 +322,26 @@ class Configuration ->scalarNode('null_value')->end() ->booleanNode('include_in_all')->defaultValue('true')->end() ->scalarNode('lat_lon')->end() + ->arrayNode('fields') + ->useAttributeAsKey('name') + ->prototype('array') + ->treatNullLike(array()) + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('type')->defaultValue('string')->end() + ->scalarNode('boost')->end() + ->scalarNode('store')->end() + ->scalarNode('index')->end() + ->scalarNode('index_analyzer')->end() + ->scalarNode('search_analyzer')->end() + ->scalarNode('analyzer')->end() + ->scalarNode('term_vector')->end() + ->scalarNode('null_value')->end() + ->booleanNode('include_in_all')->defaultValue('true')->end() + ->scalarNode('lat_lon')->end() + ->end() + ->end() + ->end() ->end() ->end() ->end() From 82ec81cb08c880d39a0a01c255cf7da4f2c0574d Mon Sep 17 00:00:00 2001 From: daFish Date: Thu, 24 Jan 2013 16:43:10 +0100 Subject: [PATCH 07/13] Fixes problem with potential null values coming from nested properties. --- .../ModelToElasticaAutoTransformer.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index aa649e2..e65d59d 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -73,17 +73,26 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf * @param array $objects the object to convert * @param array $fields the keys we want to have in the returned array * @param Elastica_Document $parent the parent document + * * @return array */ protected function transformNested($objects, array $fields, $parent) { - $documents = array(); - foreach($objects as $object) { - $document = $this->transform($object, $fields); - $documents[] = $document->getData(); + if (is_array($objects) || $objects instanceof \Traversable || $objects instanceof \ArrayAccess) { + $documents = array(); + foreach ($objects as $object) { + $document = $this->transform($object, $fields); + $documents[] = $document->getData(); + } + + return $documents; + } elseif (null !== $objects) { + $document = $this->transform($objects, $fields); + + return $document->getData(); } - return $documents; + return array(); } /** From e9be67d59bd1001818bdcfe898700000b24f8735 Mon Sep 17 00:00:00 2001 From: Vyacheslav Salakhutdinov Date: Fri, 25 Jan 2013 14:59:24 +0400 Subject: [PATCH 08/13] Updated dev deps --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index ad0b1fe..77106c9 100644 --- a/composer.json +++ b/composer.json @@ -18,12 +18,12 @@ "ruflin/elastica": "0.19.8" }, "require-dev":{ - "doctrine/orm":"2.2.*", + "doctrine/orm":">=2.2,<2.5-dev", "doctrine/mongodb-odm":"dev-master", "propel/propel1":"1.6.*" }, "suggest": { - "doctrine/orm": "2.2.*", + "doctrine/orm": ">=2.2,<2.5-dev", "doctrine/mongodb-odm": ">=3.0.0-beta2,<3.1-dev", "propel/propel1": "1.6.*" }, From 10ce9a6d0a43b915178f6bcc0f8eb0c3c406f08d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Piechota?= Date: Wed, 30 Jan 2013 22:24:19 +0100 Subject: [PATCH 09/13] CS --- DependencyInjection/FOQElasticaExtension.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 434339b..4eba776 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -364,8 +364,7 @@ class FOQElasticaExtension extends Extension { if (isset($typeConfig['finder']['service'])) { $finderId = $typeConfig['finder']['service']; - } - else{ + } else { $abstractFinderId = 'foq_elastica.finder.prototype'; $finderId = sprintf('foq_elastica.finder.%s.%s', $indexName, $typeName); $finderDef = new DefinitionDecorator($abstractFinderId); From ef869be10b1f666bef9d81af2a53d0869dbb7502 Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Sun, 3 Feb 2013 19:38:53 +0000 Subject: [PATCH 10/13] Update README.md Adding details of the ```index_name``` config --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 5afd398..b2cc942 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,21 @@ Here we created a "website" index, that uses our "default" client. Our index is now available as a service: `foq_elastica.index.website`. It is an instance of `Elastica_Index`. +If you need to have different index name from the service name, for example, +in order to have different indexes for different environments then you can +use the ```index_name``` key to change the index name. The service name will +remain the same across the environments: + + foq_elastica: + clients: + default: { host: localhost, port: 9200 } + indexes: + website: + client: default + index_name: website_qa + +The service id will be `foq_elastica.index.website` but the underlying index name is website_qa. + #### Declare a type Elasticsearch type is comparable to Doctrine entity repository. From 6995d75dc890d6256914b955d0165d92ff8a6f61 Mon Sep 17 00:00:00 2001 From: Marco Leong Date: Thu, 7 Feb 2013 17:12:38 +0800 Subject: [PATCH 11/13] The finder was not created in that section The finder was not created in that section, use repository instead, make more sense for user --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b2cc942..131b1b4 100644 --- a/README.md +++ b/README.md @@ -410,7 +410,7 @@ example: $repository = $repositoryManager->getRepository('UserBundle:User'); /** var array of Acme\UserBundle\Entity\User */ - $users = $finder->find('bob'); + $users = $repository->find('bob'); You can also specify the full name of the entity instead of the shortcut syntax: @@ -431,7 +431,7 @@ to `foq_elastica.manager`. So the above example could be simplified to: $repository = $repositoryManager->getRepository('UserBundle:User'); /** var array of Acme\UserBundle\Entity\User */ - $users = $finder->find('bob'); + $users = $repository->find('bob'); If you use multiple drivers then you can choose which one is aliased to `foq_elastica.manager` using the `default_manager` parameter: @@ -493,7 +493,7 @@ Then the custom queries will be available when using the repository returned fro $repository = $repositoryManager->getRepository('UserBundle:User'); /** var array of Acme\UserBundle\Entity\User */ - $users = $finder->findWithCustomQuery('bob'); + $users = $repository->findWithCustomQuery('bob'); Alternatively you can specify the custom repository using an annotation in the entity: From 9c60a5ccaca0a2520bb98330cf86e6adff6665a8 Mon Sep 17 00:00:00 2001 From: tyx Date: Thu, 21 Feb 2013 10:30:05 +0100 Subject: [PATCH 12/13] Add totalquery time in debugbar (like Doctrine does) --- DataCollector/ElasticaDataCollector.php | 10 ++++++++++ Resources/views/Collector/elastica.html.twig | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/DataCollector/ElasticaDataCollector.php b/DataCollector/ElasticaDataCollector.php index 27736af..2eb3146 100644 --- a/DataCollector/ElasticaDataCollector.php +++ b/DataCollector/ElasticaDataCollector.php @@ -40,6 +40,16 @@ class ElasticaDataCollector extends DataCollector return $this->data['queries']; } + public function getTime() + { + $time = 0; + foreach ($this->data['queries'] as $query) { + $time += $query['executionMS']; + } + + return $time; + } + /** * {@inheritdoc} */ diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig index 972f581..f63e8cf 100644 --- a/Resources/views/Collector/elastica.html.twig +++ b/Resources/views/Collector/elastica.html.twig @@ -10,6 +10,10 @@ Queries {{ collector.querycount }} +
+ Query Time + {{ '%0.2f'|format(collector.time * 1000) }} ms +
{% endset %} {% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %} {% endblock %} From f66858ff3ed2d94ec317725bafa94abc0e83312a Mon Sep 17 00:00:00 2001 From: tyx Date: Thu, 21 Feb 2013 10:52:49 +0100 Subject: [PATCH 13/13] Add test for the query time in datacollector --- .../ElasticaDataCollectorTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tests/DataCollector/ElasticaDataCollectorTest.php b/Tests/DataCollector/ElasticaDataCollectorTest.php index b83a81c..7ec9800 100644 --- a/Tests/DataCollector/ElasticaDataCollectorTest.php +++ b/Tests/DataCollector/ElasticaDataCollectorTest.php @@ -60,4 +60,29 @@ class ElasticaDataCollectorTest extends \PHPUnit_Framework_TestCase $this->assertEquals($queries, $elasticaDataCollector->getQueries()); } + public function testCorrectQueriesTime() + { + $requestMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') + ->disableOriginalConstructor() + ->getMock(); + + $responseMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response') + ->disableOriginalConstructor() + ->getMock(); + + $loggerMock = $this->getMockBuilder('FOQ\ElasticaBundle\Logger\ElasticaLogger') + ->disableOriginalConstructor() + ->getMock(); + + $queries = array(array('executionMS' => 10), array('executionMS' => 20)); + + $loggerMock->expects($this->once()) + ->method('getQueries') + ->will($this->returnValue($queries)); + + $elasticaDataCollector = new ElasticaDataCollector($loggerMock); + $elasticaDataCollector->collect($requestMock, $responseMock); + $this->assertEquals(30, $elasticaDataCollector->getTime()); + } + }