Complete provider, finder, transformers and configuration refactoring

This commit is contained in:
ornicar 2011-04-27 00:59:04 -07:00
parent 7be25f92bb
commit 0db0490be5
17 changed files with 427 additions and 303 deletions

View file

@ -29,6 +29,7 @@ class SearchCommand extends Command
->addOption('index', null, InputOption::VALUE_NONE, 'The index to search in')
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'The maximum number of documents to return', 20)
->addOption('show-source', null, InputOption::VALUE_NONE, 'Show the documents sources')
->addOption('show-id', null, InputOption::VALUE_NONE, 'Show the documents ids')
->addOption('explain', null, InputOption::VALUE_NONE, 'Enables explanation for each hit on how its score was computed.')
->setName('foq:elastica:search')
->setDescription('Searches documents in a given type and index');
@ -51,17 +52,20 @@ class SearchCommand extends Command
$output->writeLn(sprintf('Found %d results', $type->count($query)));
foreach ($resultSet->getResults() as $result) {
$output->writeLn($this->formatResult($result, $input->getOption('show-source'), $input->getOption('explain')));
$output->writeLn($this->formatResult($result, $input->getOption('show-source'), $input->getOption('show-id'), $input->getOption('explain')));
}
}
protected function formatResult(Elastica_Result $result, $showSource, $explain)
protected function formatResult(Elastica_Result $result, $showSource, $showId, $explain)
{
$source = $result->getSource();
$string = sprintf('[%0.2f] %s', $result->getScore(), var_export(reset($source), true));
if ($showSource) {
$string = sprintf('%s %s', $string, json_encode($source));
}
if ($showId) {
$string = sprintf('{%s} %s', $result->getId(), $string);
}
if ($explain) {
$string = sprintf('%s %s', $string, json_encode($result->getExplanation()));
}

View file

@ -91,19 +91,33 @@ class Configuration
->children()
->scalarNode('driver')->end()
->scalarNode('model')->end()
->scalarNode('identifier')->defaultValue('id')->end()
->scalarNode('identifier')->defaultValue('id')->end()
->arrayNode('provider')
->children()
->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end()
->scalarNode('batch_size')->defaultValue(100)->end()
->scalarNode('clear_object_manager')->defaultTrue()->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('finder')
->children()
->scalarNode('hydrate')->defaultTrue()->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('elastica_to_model_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('hydrate')->defaultTrue()->end()
->scalarNode('service')->end()
->end()
->end()
->arrayNode('model_to_elastica_transformer')
->addDefaultsIfNotSet()
->children()
->scalarNode('service')->end()
->end()
->end()
->end()
->end()
->end()

View file

@ -14,201 +14,250 @@ use InvalidArgumentException;
class FOQElasticaExtension extends Extension
{
protected $supportedProviderDrivers = array('mongodb', 'orm');
protected $typeMappings = array();
protected $loadedDoctrineDrivers = array();
protected $supportedProviderDrivers = array('mongodb', 'orm');
protected $typeMappings = array();
protected $loadedDoctrineDrivers = array();
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$processor = new Processor();
$config = $processor->process($configuration->getConfigTree(), $configs);
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$processor = new Processor();
$config = $processor->process($configuration->getConfigTree(), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('config.xml');
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('config.xml');
if (empty($config['clients']) || empty($config['indexes'])) {
throw new InvalidArgumentException('You must define at least one client and one index');
}
if (empty($config['clients']) || empty($config['indexes'])) {
throw new InvalidArgumentException('You must define at least one client and one index');
}
if (empty($config['default_client'])) {
$keys = array_keys($config['clients']);
$config['default_client'] = reset($keys);
}
if (empty($config['default_client'])) {
$keys = array_keys($config['clients']);
$config['default_client'] = reset($keys);
}
if (empty($config['default_index'])) {
$keys = array_keys($config['indexes']);
$config['default_index'] = reset($keys);
}
if (empty($config['default_index'])) {
$keys = array_keys($config['indexes']);
$config['default_index'] = reset($keys);
}
$clientIdsByName = $this->loadClients($config['clients'], $container);
$indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']);
$indexDefsByName = array_map(function($id) use ($container) {
return $container->getDefinition($id);
}, $indexIdsByName);
$clientIdsByName = $this->loadClients($config['clients'], $container);
$indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']);
$indexDefsByName = array_map(function($id) use ($container) {
return $container->getDefinition($id);
}, $indexIdsByName);
$this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container);
$this->loadMappingSetter($this->typeMappings, $container);
$this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container);
$this->loadMappingSetter($this->typeMappings, $container);
$container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client']));
$container->setAlias('foq_elastica.index', sprintf('foq_elastica.index.%s', $config['default_index']));
}
$container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client']));
$container->setAlias('foq_elastica.index', sprintf('foq_elastica.index.%s', $config['default_index']));
}
/**
* Loads the configured clients.
*
* @param array $config An array of clients configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadClients(array $clients, ContainerBuilder $container)
{
$clientIds = array();
foreach ($clients as $name => $client) {
$clientDefArgs = array(
isset($client['host']) ? $client['host'] : null,
isset($client['port']) ? $client['port'] : array(),
);
$clientDef = new Definition('%foq_elastica.client.class%', $clientDefArgs);
$clientId = sprintf('foq_elastica.client.%s', $name);
$container->setDefinition($clientId, $clientDef);
$clientIds[$name] = $clientId;
}
/**
* Loads the configured clients.
*
* @param array $config An array of clients configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadClients(array $clients, ContainerBuilder $container)
{
$clientIds = array();
foreach ($clients as $name => $client) {
$clientDefArgs = array(
isset($client['host']) ? $client['host'] : null,
isset($client['port']) ? $client['port'] : array(),
);
$clientDef = new Definition('%foq_elastica.client.class%', $clientDefArgs);
$clientId = sprintf('foq_elastica.client.%s', $name);
$container->setDefinition($clientId, $clientDef);
$clientIds[$name] = $clientId;
}
return $clientIds;
}
return $clientIds;
}
/**
* Loads the configured indexes.
*
* @param array $config An array of indexes configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName)
{
$indexIds = array();
foreach ($indexes as $name => $index) {
if (isset($index['client'])) {
$clientName = $index['client'];
if (!isset($clientIdsByName[$clientName])) {
throw new InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName));
}
} else {
$clientName = $defaultClientName;
}
$clientId = $clientIdsByName[$clientName];
$clientDef = $container->getDefinition($clientId);
$indexId = sprintf('foq_elastica.index.%s', $name);
$indexDefArgs = array($name);
$indexDef = new Definition('%foq_elastica.index.class%', $indexDefArgs);
$indexDef->setFactoryService($clientId);
$indexDef->setFactoryMethod('getIndex');
$container->setDefinition($indexId, $indexDef);
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId);
$indexIds[$name] = $indexId;
}
/**
* Loads the configured indexes.
*
* @param array $config An array of indexes configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadIndexes(array $indexes, ContainerBuilder $container, array $clientIdsByName, $defaultClientName)
{
$indexIds = array();
foreach ($indexes as $name => $index) {
if (isset($index['client'])) {
$clientName = $index['client'];
if (!isset($clientIdsByName[$clientName])) {
throw new InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName));
}
} else {
$clientName = $defaultClientName;
}
$clientId = $clientIdsByName[$clientName];
$clientDef = $container->getDefinition($clientId);
$indexId = sprintf('foq_elastica.index.%s', $name);
$indexDefArgs = array($name);
$indexDef = new Definition('%foq_elastica.index.class%', $indexDefArgs);
$indexDef->setFactoryService($clientId);
$indexDef->setFactoryMethod('getIndex');
$container->setDefinition($indexId, $indexDef);
$this->loadTypes(isset($index['types']) ? $index['types'] : array(), $container, $name, $indexId);
$indexIds[$name] = $indexId;
}
return $indexIds;
}
return $indexIds;
}
/**
* Loads the configured types.
*
* @param array $config An array of types configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId)
{
foreach ($types as $name => $type) {
$typeId = sprintf('%s.%s', $indexId, $name);
$typeDefArgs = array($name);
$typeDef = new Definition('%foq_elastica.type.class%', $typeDefArgs);
$typeDef->setFactoryService($indexId);
$typeDef->setFactoryMethod('getType');
$container->setDefinition($typeId, $typeDef);
if (isset($type['mappings'])) {
$this->typeMappings[] = array(
new Reference($typeId),
$type['mappings']
);
}
if (isset($type['doctrine'])) {
$this->loadTypeDoctrineIntegration($type['doctrine'], $container, $typeDef, $indexName, $name);
}
}
}
/**
* Loads the configured types.
*
* @param array $config An array of types configurations
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadTypes(array $types, ContainerBuilder $container, $indexName, $indexId)
{
foreach ($types as $name => $type) {
$typeId = sprintf('%s.%s', $indexId, $name);
$typeDefArgs = array($name);
$typeDef = new Definition('%foq_elastica.type.class%', $typeDefArgs);
$typeDef->setFactoryService($indexId);
$typeDef->setFactoryMethod('getType');
$container->setDefinition($typeId, $typeDef);
if (isset($type['mappings'])) {
$this->typeMappings[] = array(new Reference($typeId), $type['mappings']);
}
if (isset($type['doctrine'])) {
$this->loadTypeDoctrineIntegration($type['doctrine'], $container, $typeDef, $indexName, $name);
}
}
}
/**
* Loads the optional provider and finder for a type
*
* @return null
**/
public function loadTypeDoctrineIntegration(array $config, ContainerBuilder $container, Definition $typeDef, $indexName, $typeName)
{
if (!in_array($config['driver'], $this->supportedProviderDrivers)) {
throw new InvalidArgumentException(sprintf('The provider driver "%s" is not supported'));
}
$this->loadDoctrineDriver($container, $config['driver']);
if (isset($config['provider'])) {
$abstractProviderId = sprintf('foq_elastica.provider.prototype.%s', $config['driver']);
$providerId = sprintf('foq_elastica.provider.%s.%s', $indexName, $typeName);
$providerDef = new DefinitionDecorator($abstractProviderId);
$providerDef->replaceArgument(0, $typeDef);
$providerDef->replaceArgument(3, $config['model']);
$providerDef->replaceArgument(4, array_merge($config['provider'], array(
'identifier' => $config['identifier']
)));
$container->setDefinition($providerId, $providerDef);
$container->getDefinition('foq_elastica.populator')
->addMethodCall('addProvider', array($providerId, new Reference($providerId)));
}
if (isset($config['finder'])) {
$abstractMapperId = sprintf('foq_elastica.mapper.prototype.%s', $config['driver']);
$mapperId = sprintf('foq_elastica.mapper.%s.%s', $indexName, $typeName);
$mapperDef = new DefinitionDecorator($abstractMapperId);
$mapperDef->replaceArgument(1, $config['model']);
$mapperDef->replaceArgument(2, array_merge($config['finder'], array(
'identifier' => $config['identifier']
)));
$container->setDefinition($mapperId, $mapperDef);
$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($mapperId));
$container->setDefinition($finderId, $finderDef);
}
}
/**
* Loads the optional provider and finder for a type
*
* @return null
**/
protected function loadTypeDoctrineIntegration(array $typeConfig, ContainerBuilder $container, Definition $typeDef, $indexName, $typeName)
{
if (!in_array($typeConfig['driver'], $this->supportedProviderDrivers)) {
throw new InvalidArgumentException(sprintf('The provider driver "%s" is not supported'));
}
$this->loadDoctrineDriver($container, $typeConfig['driver']);
/**
* Loads the index manager
*
* @return null
**/
public function loadIndexManager(array $indexDefs, $defaultIndexId, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.index_manager');
$managerDef->replaceArgument(0, $indexDefs);
$managerDef->replaceArgument(1, new Reference('foq_elastica.index'));
}
$elasticaToModelTransformerId = $this->loadElasticaToModelTransformer($typeConfig, $container, $indexName, $typeName);
$modelToElasticaTransformerId = $this->loadModelToElasticaTransformer($typeConfig, $container, $indexName, $typeName);
/**
* Loads the mapping setter
*
* @return null
**/
public function loadMappingSetter(array $mappings, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.mapping_setter');
$managerDef->replaceArgument(0, $mappings);
}
if (isset($typeConfig['provider'])) {
$providerId = $this->loadTypeProvider($typeConfig, $container, $modelToElasticaTransformerId, $typeDef, $indexName, $typeName);
$container->getDefinition('foq_elastica.populator')->addMethodCall('addProvider', array($providerId, new Reference($providerId)));
}
if (isset($typeConfig['finder'])) {
$this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeDef, $indexName, $typeName);
}
}
protected function loadDoctrineDriver(ContainerBuilder $container, $driver)
{
if (in_array($driver, $this->loadedDoctrineDrivers)) {
return;
}
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load($driver.'.xml');
$this->loadedDoctrineDrivers[] = $driver;
}
protected function loadElasticaToModelTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName)
{
if (isset($typeConfig['elastica_to_model_transformer']['service'])) {
return $typeConfig['elastica_to_model_transformer']['service'];
}
$abstractId = sprintf('foq_elastica.elastica_to_model_transformer.prototype.%s', $typeConfig['driver']);
$serviceId = sprintf('foq_elastica.elastica_to_model_transformer.%s.%s', $indexName, $typeName);
$serviceDef = new DefinitionDecorator($abstractId);
$serviceDef->replaceArgument(1, $typeConfig['model']);
$serviceDef->replaceArgument(2, array(
'identifier' => $typeConfig['identifier'],
'hydrate' => $typeConfig['elastica_to_model_transformer']['hydrate']
));
$container->setDefinition($serviceId, $serviceDef);
return $serviceId;
}
protected function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, $indexName, $typeName)
{
if (isset($typeConfig['model_to_elastica_transformer']['service'])) {
return $typeConfig['model_to_elastica_transformer']['service'];
}
$abstractId = sprintf('foq_elastica.model_to_elastica_transformer.prototype.auto');
$serviceId = sprintf('foq_elastica.model_to_elastica_transformer.%s.%s', $indexName, $typeName);
$serviceDef = new DefinitionDecorator($abstractId);
$serviceDef->replaceArgument(0, array(
'identifier' => $typeConfig['identifier']
));
$container->setDefinition($serviceId, $serviceDef);
return $serviceId;
}
protected function loadTypeProvider(array $typeConfig, ContainerBuilder $container, $modelToElasticaTransformerId, $typeDef, $indexName, $typeName)
{
if (isset($typeConfig['provider']['service'])) {
return $typeConfig['provider']['service'];
}
$abstractProviderId = sprintf('foq_elastica.provider.prototype.%s', $typeConfig['driver']);
$providerId = sprintf('foq_elastica.provider.%s.%s', $indexName, $typeName);
$providerDef = new DefinitionDecorator($abstractProviderId);
$providerDef->replaceArgument(0, $typeDef);
$providerDef->replaceArgument(2, new Reference($modelToElasticaTransformerId));
$providerDef->replaceArgument(3, $typeConfig['model']);
$providerDef->replaceArgument(4, array(
'query_builder_method' => $typeConfig['provider']['query_builder_method'],
'batch_size' => $typeConfig['provider']['batch_size'],
'clear_object_manager' => $typeConfig['provider']['clear_object_manager']
));
$container->setDefinition($providerId, $providerDef);
return $providerId;
}
protected function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $elasticaToModelId, $typeDef, $indexName, $typeName)
{
if (isset($typeConfig['finder']['service'])) {
return $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);
return $finderId;
}
/**
* Loads the index manager
*
* @return null
**/
protected function loadIndexManager(array $indexDefs, $defaultIndexId, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.index_manager');
$managerDef->replaceArgument(0, $indexDefs);
$managerDef->replaceArgument(1, new Reference('foq_elastica.index'));
}
/**
* Loads the mapping setter
*
* @return null
**/
protected function loadMappingSetter(array $mappings, ContainerBuilder $container)
{
$managerDef = $container->getDefinition('foq_elastica.mapping_setter');
$managerDef->replaceArgument(0, $mappings);
}
protected function loadDoctrineDriver(ContainerBuilder $container, $driver)
{
if (in_array($driver, $this->loadedDoctrineDrivers)) {
return;
}
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load($driver.'.xml');
$this->loadedDoctrineDrivers[] = $driver;
}
}

View file

@ -2,7 +2,7 @@
namespace FOQ\ElasticaBundle\Finder;
use FOQ\ElasticaBundle\MapperInterface;
use FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use FOQ\ElasticaBundle\Paginator\DoctrinePaginatorAdapter;
use Zend\Paginator\Paginator;
use Elastica_Searchable;
@ -11,15 +11,15 @@ use Elastica_Query;
/**
* Finds elastica documents and map them to persisted objects
*/
class MappedFinder implements FinderInterface, PaginatedFinderInterface
class TransformedFinder implements FinderInterface, PaginatedFinderInterface
{
protected $searchable;
protected $mapper;
protected $transformer;
public function __construct(Elastica_Searchable $searchable, MapperInterface $mapper)
public function __construct(Elastica_Searchable $searchable, ElasticaToModelTransformerInterface $transformer)
{
$this->searchable = $searchable;
$this->mapper = $mapper;
$this->searchable = $searchable;
$this->transformer = $transformer;
}
/**
@ -33,7 +33,7 @@ class MappedFinder implements FinderInterface, PaginatedFinderInterface
$queryObject->setLimit($limit);
$results = $this->searchable->search($queryObject)->getResults();
return $this->mapper->fromElasticaObjects($results);
return $this->transformer->transform($results);
}
/**
@ -44,6 +44,7 @@ class MappedFinder implements FinderInterface, PaginatedFinderInterface
public function findPaginated($query)
{
$queryObject = Elastica_Query::create($query);
$results = $this->searchable->search($queryObject)->getResults();
$paginatorAdapter = $this->createPaginatorAdapter($queryObject);
$paginator = new Paginator($paginatorAdapter);
@ -58,6 +59,6 @@ class MappedFinder implements FinderInterface, PaginatedFinderInterface
*/
protected function createPaginatorAdapter(Elastica_Query $query)
{
return new DoctrinePaginatorAdapter($this->searchable, $query, $this->mapper);
return new DoctrinePaginatorAdapter($this->searchable, $query, $this->transformer);
}
}

View file

@ -1,17 +0,0 @@
<?php
namespace FOQ\ElasticaBundle;
/**
* Maps Elastica documents with persisted objects
*/
interface MapperInterface
{
/**
* Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository
*
* @return array
**/
function fromElasticaObjects(array $elasticaObjects);
}

View file

@ -2,7 +2,7 @@
namespace FOQ\ElasticaBundle\Paginator;
use FOQ\ElasticaBundle\MapperInterface;
use FOQ\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Elastica_Searchable;
use Elastica_Query;
@ -13,17 +13,18 @@ use Elastica_Query;
*/
class DoctrinePaginatorAdapter extends AbstractPaginatorAdapter
{
protected $mapper;
protected $transformer;
/**
* @param Elastica_SearchableInterface the object to search in
* @param Elastica_Query the query to search
* @param MapperInterface the mapper for fetching the results
* @param ElasticaToModelTransformerInterface the transformer for fetching the results
*/
public function __construct(Elastica_Searchable $searchable, Elastica_Query $query, MapperInterface $mapper)
public function __construct(Elastica_Searchable $searchable, Elastica_Query $query, ElasticaToModelTransformerInterface $transformer)
{
parent::__construct($searchable, $query);
$this->mapper = $mapper;
$this->transformer = $transformer;
}
/**
@ -33,6 +34,6 @@ class DoctrinePaginatorAdapter extends AbstractPaginatorAdapter
{
$results = $this->getElasticaResults($offset, $itemCountPerPage);
return $this->mapper->fromElasticaObjects($results);
return $this->transformer->transform($results);
}
}

View file

@ -2,7 +2,7 @@
namespace FOQ\ElasticaBundle\Provider;
use FOQ\ElasticaBundle\Transformer\ObjectToArrayTransformerInterface;
use FOQ\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Elastica_Type;
use Elastica_Document;
@ -18,11 +18,10 @@ class DoctrineProvider implements ProviderInterface
protected $options = array(
'batch_size' => 100,
'clear_object_manager' => true,
'query_builder_method' => 'createQueryBuilder',
'identifier' => 'id'
'query_builder_method' => 'createQueryBuilder'
);
public function __construct(Elastica_Type $type, ObjectManager $objectManager, ObjectToArrayTransformerInterface $transformer, $objectClass, array $options = array())
public function __construct(Elastica_Type $type, ObjectManager $objectManager, ModelToElasticaTransformerInterface $transformer, $objectClass, array $options = array())
{
$this->type = $type;
$this->objectManager = $objectManager;
@ -41,24 +40,30 @@ class DoctrineProvider implements ProviderInterface
$queryBuilder = $this->createQueryBuilder();
$nbObjects = $queryBuilder->getQuery()->count();
$fields = $this->extractTypeFields();
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
for ($offset = 0; $offset < $nbObjects; $offset += $this->options['batch_size']) {
$stepStartTime = microtime(true);
$documents = array();
$objects = $queryBuilder->limit($this->options['batch_size'])->skip($offset)->getQuery()->execute()->toArray();
$stepCount = (count($objects)+$offset);
$loggerClosure(sprintf('%0.1f%% (%d/%d)', 100*$stepCount/$nbObjects, $stepCount, $nbObjects));
foreach ($objects as $object) {
$data = $this->transformer->transform($object, $fields);
$documents[] = new Elastica_Document($object->$identifierGetter(), $data);
try {
$documents[] = $this->transformer->transform($object, $fields);
} catch (NotIndexableException $e) {
// skip document
}
}
$this->type->addDocuments($documents);
if ($this->options['clear_object_manager']) {
$this->objectManager->clear();
}
$stepNbObjects = count($objects);
$stepCount = $stepNbObjects+$offset;
$objectsPerSecond = $stepNbObjects / (microtime(true) - $stepStartTime);
$loggerClosure(sprintf('%0.1f%% (%d/%d), %d objects/s', 100*$stepCount/$nbObjects, $stepCount, $nbObjects, $objectsPerSecond));
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace FOQ\ElasticaBundle\Provider;
use RuntimeException;
/**
* Skip a document during population
*/
class NotIndexableException extends RuntimeException
{
}

View file

@ -30,11 +30,13 @@
<argument /> <!-- mappings -->
</service>
<service id="foq_elastica.transformer.auto" class="FOQ\ElasticaBundle\Transformer\ObjectToArrayAutomaticTransformer" public="false" />
<service id="foq_elastica.finder.prototype" class="FOQ\ElasticaBundle\Finder\MappedFinder" public="true" abstract="true">
<service id="foq_elastica.finder.prototype" class="FOQ\ElasticaBundle\Finder\TransformedFinder" public="true" abstract="true">
<argument /> <!-- index -->
<argument /> <!-- mapper -->
<argument /> <!-- transformer -->
</service>
<service id="foq_elastica.model_to_elastica_transformer.prototype.auto" class="FOQ\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer" public="false" abstract="true">
<argument /> <!-- options -->
</service>
</services>

View file

@ -9,12 +9,12 @@
<service id="foq_elastica.provider.prototype.mongodb" class="FOQ\ElasticaBundle\Provider\DoctrineProvider" public="false" abstract="true">
<argument /> <!-- index -->
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument type="service" id="foq_elastica.transformer.auto" />
<argument /> <!-- transformer -->
<argument /> <!-- model -->
<argument type="collection" /> <!-- options -->
</service>
<service id="foq_elastica.mapper.prototype.mongodb" class="FOQ\ElasticaBundle\Mapper\DoctrineMapper" public="false">
<service id="foq_elastica.elastica_to_model_transformer.prototype.mongodb" class="FOQ\ElasticaBundle\Transformer\ElasticaToModelDoctrineTransformer" public="false">
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument /> <!-- model -->
<argument type="collection" /> <!-- options -->

View file

@ -9,12 +9,12 @@
<service id="foq_elastica.provider.prototype.orm" class="FOQ\ElasticaBundle\Provider\DoctrineProvider" public="false" abstract="true">
<argument /> <!-- index -->
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="foq_elastica.transformer.auto" />
<argument /> <!-- transformer -->
<argument /> <!-- model -->
<argument type="collection" /> <!-- options -->
</service>
<service id="foq_elastica.mapper.prototype.orm" class="FOQ\ElasticaBundle\Mapper\DoctrineMapper" public="false">
<service id="foq_elastica.elastica_to_model_transformer.prototype.orm" class="FOQ\ElasticaBundle\Transformer\ElasticaToModelDoctrineTransformer" public="false">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument /> <!-- model -->
<argument type="collection" /> <!-- options -->

View file

@ -1,16 +1,16 @@
<?php
namespace FOQ\ElasticaBundle\Mapper;
namespace FOQ\ElasticaBundle\Transformer;
use FOQ\ElasticaBundle\MapperInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Elastica_Document;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids
*/
class DoctrineMapper implements MapperInterface
class ElasticaToModelDoctrineTransformer implements ElasticaToModelTransformerInterface
{
/**
* Repository to fetch the objects from
@ -54,9 +54,10 @@ class DoctrineMapper implements MapperInterface
* Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository
*
* @param array of elastica objects
* @return array
**/
public function fromElasticaObjects(array $elasticaObjects)
public function transform(array $elasticaObjects)
{
$ids = array_map(function($elasticaObject) {
return $elasticaObject->getId();
@ -70,11 +71,13 @@ class DoctrineMapper implements MapperInterface
->execute()
->toArray();
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
// sort objects in the order of ids
$idPos = array_flip($ids);
usort($objects, function($a, $b) use ($idPos)
usort($objects, function($a, $b) use ($idPos, $identifierGetter)
{
return $idPos[$a->getId()] > $idPos[$b->getId()];
return $idPos[$a->$identifierGetter()] > $idPos[$b->$identifierGetter()];
});
return $objects;

View file

@ -0,0 +1,18 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
/**
* Maps Elastica documents with model objects
*/
interface ElasticaToModelTransformerInterface
{
/**
* Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository
*
* @param array of elastica objects
* @return array of model objects
**/
function transform(array $elasticaObjects);
}

View file

@ -0,0 +1,78 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
use Doctrine\Common\Persistence\ObjectManager;
use Elastica_Document;
use Traversable;
use ArrayAccess;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids
*/
class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterface
{
/**
* Optional parameters
*
* @var array
*/
protected $options = array(
'identifier' => 'id'
);
/**
* Instanciates a new Mapper
*
* @param array $options
*/
public function __construct(array $options = array())
{
$this->options = array_merge($this->options, $options);
}
/**
* Transforms an object into an elastica object having the required keys
*
* @param object $object the object to convert
* @param array $fields the keys we want to have in the returned array
* @return Elastica_Document
**/
public function transform($object, array $fields)
{
$class = get_class($object);
$array = array();
foreach ($fields as $key) {
$getter = 'get'.ucfirst($key);
if (!method_exists($class, $getter)) {
throw new RuntimeException(sprintf('The getter %s::%s does not exist', $class, $getter));
}
$array[$key] = $this->normalizeValue($object->$getter());
}
$identifierGetter = 'get'.ucfirst($this->options['identifier']);
$identifier = $object->$identifierGetter();
return new Elastica_Document($identifier, array_filter($array));
}
/**
* Attempts to convert any type to a string or an array of strings
*
* @param mixed $value
* @return string|array
*/
protected function normalizeValue($value)
{
if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) {
$value = array_map(function($v) {
return (string) $v;
}, is_array($value) ? $value : iterator_to_array($value));
} else {
$value = (string) $value;
}
return $value;
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
/**
* Maps Elastica documents with model objects
*/
interface ModelToElasticaTransformerInterface
{
/**
* Transforms an object into an elastica object having the required keys
*
* @param object $object the object to convert
* @param array $fields the keys we want to have in the returned array
* @return Elastica_Document
**/
function transform($object, array $fields);
}

View file

@ -1,50 +0,0 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
use RuntimeException;
use Traversable;
use ArrayAccess;
/**
* AutomaticObjectToArrayTransformer
* Tries to convert objects by generating getters
* based on the required keys
*/
class ObjectToArrayAutomaticTransformer implements ObjectToArrayTransformerInterface
{
/**
* Transforms an object into an array having the required keys
*
* @param object $object the object to convert
* @param array $requiredKeys the keys we want to have in the returned array
* @return array
**/
public function transform($object, array $requiredKeys)
{
$class = get_class($object);
$array = array();
foreach ($requiredKeys as $key) {
$getter = 'get'.ucfirst($key);
if (!method_exists($class, $getter)) {
throw new RuntimeException(sprintf('The getter %s::%s does not exist', $class, $getter));
}
$array[$key] = $this->normalizeValue($object->$getter());
}
return array_filter($array);
}
public function normalizeValue($value)
{
if (is_array($value) || $value instanceof Traversable || $value instanceof ArrayAccess) {
$value = array_map(function($v) {
return (string) $v;
}, is_array($value) ? $value : iterator_to_array($value));
} else {
$value = (string) $value;
}
return $value;
}
}

View file

@ -1,15 +0,0 @@
<?php
namespace FOQ\ElasticaBundle\Transformer;
interface ObjectToArrayTransformerInterface
{
/**
* Transforms an object into an array having the required keys
*
* @param object $object the object to convert
* @param array $requiredKeys the keys we want to have in the returned array
* @return array
**/
public function transform($object, array $requiredKeys);
}