Permalink
Browse files

Merge remote-tracking branch 'origin/master' into recursive_field_config

Conflicts:
	DependencyInjection/Configuration.php
  • Loading branch information...
leahaense committed Feb 22, 2013
2 parents 71ccacc + 737b73b commit 7e85f79f8ed0300648a93a77c07fced719682ba0
@@ -40,6 +40,16 @@ public function getQueries()
return $this->data['queries'];
}
+ public function getTime()
+ {
+ $time = 0;
+ foreach ($this->data['queries'] as $query) {
+ $time += $query['executionMS'];
+ }
+
+ return $time;
+ }
+
/**
* {@inheritdoc}
*/
@@ -108,6 +108,7 @@ private function addIndexesSection(ArrayNodeDefinition $rootNode)
->prototype('array')
->performNoDeepMerging()
->children()
+ ->scalarNode('index_name')->end()
->scalarNode('client')->end()
->scalarNode('finder')
->treatNullLike(true)
@@ -253,6 +254,7 @@ protected function getTypesNode()
->append($this->getMappingsNode())
->append($this->getSourceNode())
->append($this->getBoostNode())
+ ->append($this->getRoutingNode())
->end()
;
@@ -403,6 +405,9 @@ protected function getSourceNode()
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
+ ->scalarNode('compress')->end()
+ ->scalarNode('compress_threshold')->end()
+ ->scalarNode('enabled')->end()
->end()
;
@@ -426,4 +431,22 @@ protected function getBoostNode()
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;
+ }
}
@@ -105,7 +105,8 @@ protected function loadIndexes(array $indexes, ContainerBuilder $container, arra
$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');
@@ -178,6 +179,9 @@ protected function loadTypes(array $types, ContainerBuilder $container, $indexNa
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);
@@ -368,15 +372,16 @@ private function getDoctrineEvents(array $typeConfig)
protected function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, $typeDef, $indexName, $typeName)
{
if (isset($typeConfig['finder']['service'])) {
- return $typeConfig['finder']['service'];
+ $finderId = $typeConfig['finder']['service'];
+ } 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);
}
- $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));
View
@@ -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.
@@ -395,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:
@@ -416,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:
@@ -478,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:
@@ -10,6 +10,10 @@
<b>Queries</b>
<span>{{ collector.querycount }}</span>
</div>
+ <div class="sf-toolbar-info-piece">
+ <b>Query Time</b>
+ <span>{{ '%0.2f'|format(collector.time * 1000) }} ms</span>
+ </div>
{% endset %}
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
{% endblock %}
@@ -60,4 +60,29 @@ public function testCorrectQueriesReturned()
$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());
+ }
+
}
@@ -73,17 +73,26 @@ public function transform($object, array $fields)
* @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();
}
/**
View
@@ -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.*"
},

0 comments on commit 7e85f79

Please sign in to comment.