2011-04-17 19:56:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOQ\ElasticaBundle\Provider;
|
|
|
|
|
2011-04-27 09:59:04 +02:00
|
|
|
use FOQ\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
|
2011-04-17 19:56:32 +02:00
|
|
|
use Elastica_Type;
|
|
|
|
use Elastica_Document;
|
|
|
|
use Closure;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
|
|
|
class DoctrineProvider implements ProviderInterface
|
|
|
|
{
|
|
|
|
protected $type;
|
|
|
|
protected $objectManager;
|
|
|
|
protected $objectClass;
|
2011-04-20 05:03:54 +02:00
|
|
|
protected $transformer;
|
2011-04-17 19:56:32 +02:00
|
|
|
protected $options = array(
|
2011-04-20 22:53:59 +02:00
|
|
|
'batch_size' => 100,
|
|
|
|
'clear_object_manager' => true,
|
2011-04-27 09:59:04 +02:00
|
|
|
'query_builder_method' => 'createQueryBuilder'
|
2011-04-17 19:56:32 +02:00
|
|
|
);
|
|
|
|
|
2011-05-09 18:23:35 +02:00
|
|
|
public function __construct(Elastica_Type $type, $objectManager, ModelToElasticaTransformerInterface $transformer, $objectClass, array $options = array())
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
|
|
|
$this->type = $type;
|
|
|
|
$this->objectManager = $objectManager;
|
|
|
|
$this->objectClass = $objectClass;
|
2011-04-20 05:03:54 +02:00
|
|
|
$this->transformer = $transformer;
|
2011-04-17 19:56:32 +02:00
|
|
|
$this->options = array_merge($this->options, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the repository objects in the type index
|
|
|
|
*
|
|
|
|
* @param Closure $loggerClosure
|
|
|
|
*/
|
|
|
|
public function populate(Closure $loggerClosure)
|
|
|
|
{
|
|
|
|
$queryBuilder = $this->createQueryBuilder();
|
|
|
|
$nbObjects = $queryBuilder->getQuery()->count();
|
2011-04-20 05:03:54 +02:00
|
|
|
$fields = $this->extractTypeFields();
|
2011-04-17 19:56:32 +02:00
|
|
|
|
|
|
|
for ($offset = 0; $offset < $nbObjects; $offset += $this->options['batch_size']) {
|
|
|
|
|
2011-04-27 09:59:04 +02:00
|
|
|
$stepStartTime = microtime(true);
|
2011-04-20 05:03:54 +02:00
|
|
|
$documents = array();
|
2011-04-20 22:53:59 +02:00
|
|
|
$objects = $queryBuilder->limit($this->options['batch_size'])->skip($offset)->getQuery()->execute()->toArray();
|
|
|
|
|
2011-04-20 05:03:54 +02:00
|
|
|
foreach ($objects as $object) {
|
2011-04-27 09:59:04 +02:00
|
|
|
try {
|
|
|
|
$documents[] = $this->transformer->transform($object, $fields);
|
|
|
|
} catch (NotIndexableException $e) {
|
|
|
|
// skip document
|
|
|
|
}
|
2011-04-20 05:03:54 +02:00
|
|
|
}
|
|
|
|
$this->type->addDocuments($documents);
|
2011-04-17 19:56:32 +02:00
|
|
|
|
|
|
|
if ($this->options['clear_object_manager']) {
|
|
|
|
$this->objectManager->clear();
|
|
|
|
}
|
2011-04-27 09:59:04 +02:00
|
|
|
|
|
|
|
$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));
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-20 05:03:54 +02:00
|
|
|
* Creates the query buider used to fetch the documents to index
|
2011-04-17 19:56:32 +02:00
|
|
|
*
|
2011-04-20 05:03:54 +02:00
|
|
|
* @return Query
|
2011-04-17 19:56:32 +02:00
|
|
|
**/
|
2011-04-20 05:03:54 +02:00
|
|
|
protected function createQueryBuilder()
|
|
|
|
{
|
2011-04-20 22:53:59 +02:00
|
|
|
return $this->objectManager->getRepository($this->objectClass)->{$this->options['query_builder_method']}();
|
2011-04-20 05:03:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function extractTypeFields()
|
2011-04-17 19:56:32 +02:00
|
|
|
{
|
|
|
|
$mappings = $this->type->getMapping();
|
|
|
|
// skip index name
|
|
|
|
$mappings = reset($mappings);
|
|
|
|
// skip type name
|
|
|
|
$mappings = reset($mappings);
|
|
|
|
$mappings = $mappings['properties'];
|
2011-04-20 22:53:59 +02:00
|
|
|
if (array_key_exists('__isInitialized__', $mappings)) {
|
|
|
|
unset($mappings['__isInitialized__']);
|
|
|
|
}
|
2011-04-17 19:56:32 +02:00
|
|
|
|
2011-04-20 05:03:54 +02:00
|
|
|
return array_keys($mappings);
|
2011-04-17 19:56:32 +02:00
|
|
|
}
|
|
|
|
}
|