Skip to content

Commit

Permalink
Merge pull request #190 from floriansemm/nested_documents
Browse files Browse the repository at this point in the history
Nested documents
  • Loading branch information
floriansemm authored Nov 12, 2018
2 parents 28b4342 + 677cea5 commit 49cb335
Show file tree
Hide file tree
Showing 41 changed files with 1,736 additions and 627 deletions.
11 changes: 9 additions & 2 deletions Client/Solarium/Plugin/LoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ protected function initPluginType()
public function preExecuteRequest(PreExecuteRequest $event)
{
$endpoint = $event->getEndpoint();
$uri = $event->getRequest()->getUri();
$request = $event->getRequest();
$uri = $request->getUri();

$path = sprintf('%s://%s:%s%s/%s', $endpoint->getScheme(), $endpoint->getHost(), $endpoint->getPort(), $endpoint->getPath(), urldecode($uri));

$this->logger->startRequest($path);
$requestInformation = [
'uri' => $path,
'method' => $request->getMethod(),
'raw_data' => $request->getRawData()
];

$this->logger->startRequest($requestInformation);
}

/**
Expand Down
33 changes: 31 additions & 2 deletions Client/Solarium/SolariumClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,40 @@ public function addPlugin($pluginName, AbstractPlugin $plugin)
*/
public function build()
{
$solariumClient = new Client(array('endpoint' => $this->settings), $this->eventDispatcher);
$settings = [];
foreach ($this->settings as $name => $options) {
if (isset($options['dsn'])) {
unset(
$options['scheme'],
$options['host'],
$options['port'],
$options['path']
);

$parsedDsn = parse_url($options['dsn']);
unset($options['dsn']);
if ($parsedDsn) {
$options['scheme'] = isset($parsedDsn['scheme']) ? $parsedDsn['scheme'] : 'http';
if (isset($parsedDsn['host'])) {
$options['host'] = $parsedDsn['host'];
}
if (isset($parsedDsn['user'])) {
$auth = $parsedDsn['user'] . (isset($parsedDsn['pass']) ? ':' . $parsedDsn['pass'] : '');
$options['host'] = $auth . '@' . $options['host'];
}
$options['port'] = isset($parsedDsn['port']) ? $parsedDsn['port'] : 80;
$options['path'] = isset($parsedDsn['path']) ? $parsedDsn['path'] : '';
}
}

$settings[$name] = $options;
}

$solariumClient = new Client(array('endpoint' => $settings), $this->eventDispatcher);
foreach ($this->plugins as $pluginName => $plugin) {
$solariumClient->registerPlugin($pluginName, $plugin);
}

return $solariumClient;
}
}
}
103 changes: 92 additions & 11 deletions Command/ShowSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FS\SolrBundle\Command;

use FS\SolrBundle\Doctrine\Mapper\MetaInformationInterface;
use FS\SolrBundle\Doctrine\Mapper\SolrMappingException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -30,31 +31,111 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($namespaces->getEntityClassnames() as $classname) {
try {
$metaInformation = $metaInformationFactory->loadInformation($classname);

if ($metaInformation->isNested()) {
continue;
}
} catch (SolrMappingException $e) {
$output->writeln(sprintf('<info>%s</info>', $e->getMessage()));
continue;
}

$output->writeln(sprintf('<comment>%s</comment>', $classname));
$nested = '';
if ($metaInformation->isNested()) {
$nested = '(nested)';
}
$output->writeln(sprintf('<comment>%s</comment> %s', $classname, $nested));
$output->writeln(sprintf('Documentname: %s', $metaInformation->getDocumentName()));
$output->writeln(sprintf('Document Boost: %s', $metaInformation->getBoost()?$metaInformation->getBoost(): '-'));

$table = new Table($output);
$table->setHeaders(array('Property', 'Document Fieldname', 'Boost'));

foreach ($metaInformation->getFieldMapping() as $documentField => $property) {
$field = $metaInformation->getField($documentField);
$simpleFields = $this->getSimpleFields($metaInformation);

if ($field === null) {
continue;
$rows = [];
foreach ($simpleFields as $documentField => $property) {
if ($field = $metaInformation->getField($documentField)) {
$rows[] = [$property, $documentField, $field->boost];
}
}
$this->renderTable($output, $rows);

$table->addRow(array($property, $documentField, $field->boost));
$nestedFields = $this->getNestedFields($metaInformation);
if (count($nestedFields) == 0) {
return;
}
$table->render();

$output->writeln(sprintf('Fields <comment>(%s)</comment> with nested documents', count($nestedFields)));

foreach ($nestedFields as $idField) {
$propertyName = substr($idField, 0, strpos($idField, '.'));

if ($nestedField = $metaInformation->getField($propertyName)) {
$output->writeln(sprintf('Field <comment>%s</comment> contains nested class <comment>%s</comment>', $propertyName, $nestedField->nestedClass));

$nestedDocument = $metaInformationFactory->loadInformation($nestedField->nestedClass);
$rows = [];
foreach ($nestedDocument->getFieldMapping() as $documentField => $property) {
$field = $nestedDocument->getField($documentField);

if ($field === null) {
continue;
}

$rows[] = [$property, $documentField, $field->boost];
}

$this->renderTable($output, $rows);
}
}

}

}

/**
* @param OutputInterface $output
* @param array $rows
*/
private function renderTable(OutputInterface $output, array $rows)
{
$table = new Table($output);
$table->setHeaders(array('Property', 'Document Fieldname', 'Boost'));
$table->setRows($rows);

$table->render();
}

/**
* @param MetaInformationInterface $metaInformation
*
* @return array
*/
private function getSimpleFields(MetaInformationInterface $metaInformation)
{
$simpleFields = array_filter($metaInformation->getFieldMapping(), function ($field) {
if (strpos($field, '.') === false) {
return true;
}

return false;
});

return $simpleFields;
}

/**
* @param MetaInformationInterface $metaInformation
*
* @return array
*/
protected function getNestedFields(MetaInformationInterface $metaInformation)
{
$complexFields = array_filter($metaInformation->getFieldMapping(), function ($field) {
if (strpos($field, '.id') !== false) {
return true;
}

return false;
});

return $complexFields;
}
}
10 changes: 7 additions & 3 deletions Command/SynchronizeIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$i++;
}

$entities = $repository->findBy(array(), null, $batchSize, $offset);
$entities = $repository->findBy([], null, $batchSize, $offset);

try {
$solr->synchronizeIndex($entities);
Expand Down Expand Up @@ -141,16 +141,20 @@ private function getObjectManager($entityClassname)
private function getIndexableEntities($entity = null)
{
if ($entity) {
return array($entity);
return [$entity];
}

$entities = array();
$entities = [];
$namespaces = $this->getContainer()->get('solr.doctrine.classnameresolver.known_entity_namespaces');
$metaInformationFactory = $this->getContainer()->get('solr.meta.information.factory');

foreach ($namespaces->getEntityClassnames() as $classname) {
try {
$metaInformation = $metaInformationFactory->loadInformation($classname);
if ($metaInformation->isNested()) {
continue;
}

array_push($entities, $metaInformation->getClassName());
} catch (SolrMappingException $e) {
continue;
Expand Down
27 changes: 17 additions & 10 deletions DataCollector/RequestCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace FS\SolrBundle\DataCollector;

use FS\SolrBundle\Logging\DebugLogger;
use FS\SolrBundle\Logging\SolrLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\VarDumper\Cloner\VarCloner;

class RequestCollector extends DataCollector
{
Expand All @@ -25,9 +25,7 @@ public function __construct(DebugLogger $logger)
}

/**
* @param Request $request
* @param Response $response
* @param \Exception|null $exception
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
Expand Down Expand Up @@ -74,12 +72,21 @@ public function getTime()
*/
public function parseQuery($request)
{
list($endpoint, $params) = explode('?', $request['request']);
list($endpoint, $params) = explode('?', $request['request']['uri']);

$request['endpoint'] = $endpoint;
$request['params'] = $params;
$request['method'] = $request['request']['method'];
$request['raw_data'] = $request['request']['raw_data'];

if (class_exists(VarCloner::class)) {
$varCloner = new VarCloner();

parse_str($params, $stub);
$request['stub'] = Kernel::VERSION_ID >= 30200 ? $varCloner->cloneVar($stub) : $stub;
}

return array_merge($request, [
'endpoint' => $endpoint,
'params' => $params
]);
return $request;
}

/**
Expand Down
38 changes: 15 additions & 23 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,30 @@ class Configuration implements ConfigurationInterface
{

/**
* @return TreeBuilder
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('fs_solr');
$rootNode->children()
->arrayNode('endpoints')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('host')->end()
->scalarNode('port')->end()
->scalarNode('path')->end()
->scalarNode('core')->end()
->scalarNode('timeout')->end()
->booleanNode('active')->defaultValue(true)->end()
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('dsn')->end()
->scalarNode('scheme')->end()
->scalarNode('host')->end()
->scalarNode('port')->end()
->scalarNode('path')->end()
->scalarNode('core')->end()
->scalarNode('timeout')->end()
->booleanNode('active')->defaultValue(true)->end()
->end()
->end()
->end()
->end()
->end()
// ->arrayNode('clients')
// ->useAttributeAsKey('name')
// ->prototype('array')
// ->children()
// ->arrayNode('endpoints')
// ->prototype('scalar')->end()
// ->end()
// ->end()
// ->end()
// ->end()
->booleanNode('auto_index')->defaultValue(true)->end()
->end();
->end();

return $treeBuilder;
}
Expand Down
9 changes: 4 additions & 5 deletions DependencyInjection/FSSolrExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace FS\SolrBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
Expand All @@ -14,8 +12,7 @@ class FSSolrExtension extends Extension
{

/**
* @param array $configs
* @param ContainerBuilder $container
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -29,7 +26,9 @@ public function load(array $configs, ContainerBuilder $container)

$this->setupClients($config, $container);

$container->setParameter('solr.auto_index', $config['auto_index']);
if (!$container->hasParameter('solr.auto_index')) {
$container->setParameter('solr.auto_index', $config['auto_index']);
}

$this->setupDoctrineListener($config, $container);
$this->setupDoctrineConfiguration($config, $container);
Expand Down
12 changes: 12 additions & 0 deletions Doctrine/AbstractIndexingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ protected function hasChanged($doctrineChangeSet, $entity)

return count($documentChangeSet) > 0;
}

/**
* @param object $entity
*
* @return bool
*/
protected function isNested($entity)
{
$metaInformation = $this->metaInformationFactory->loadInformation($entity);

return $metaInformation->isNested();
}
}
Loading

0 comments on commit 49cb335

Please sign in to comment.