Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions lib/Doctrine/Template/Solr.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function isSearchAvailableTableProxy()
**/
public function getUniqueId()
{
return sprintf('%s_%s', get_class($this->getInvoker()), $this->getInvoker()->getId());
return sprintf('%s_%s', get_class($this->getInvoker()), $this->getInvoker()->getPrimaryKey());
}

/**
Expand Down Expand Up @@ -100,14 +100,22 @@ public function getFieldsArray()

// set meta data
$document['sf_meta_class']['value'] = get_class($invoker);
$document['sf_meta_id']['value'] = $invoker->getId();
$document['sf_meta_id']['value'] = $invoker->getPrimaryKey();

// Should we perform specific i18n operations?
$isI18N = $invoker->getTable()->hasTemplate('Doctrine_Template_I18n');
if ($isI18N)
{
$langs = $invoker->Translation->getKeys();
$translatedFields = $invoker->Translation->getFirst()->getTable()->getFieldNames();
// Make sure we retrieve lang codes
$langs = array();
foreach($invoker->Translation->getKeys() as $key)
{
if (is_string($key))
{
$langs[] = $key;
}
}
$translatedFields = $invoker->Translation->getFirst()->getTable()->getFieldNames();
}

// Set others fields
Expand All @@ -121,13 +129,13 @@ public function getFieldsArray()
// If the current field is part of the i18n table
if ($isI18N && in_array($field, $translatedFields))
{
foreach ($langs as $lang)
{
$fieldName = $field . '_' . $lang;
$value = $invoker->Translation[$lang]->get($field);
$document[$fieldName]['value'] = $value;
$document[$fieldName]['boost'] = $fieldBoost;
}
foreach ($langs as $lang)
{
$fieldName = $field . '_' . $lang;
$value = $invoker->Translation[$lang]->get($field);
$document[$fieldName]['value'] = $value;
$document[$fieldName]['boost'] = $fieldBoost;
}
}
else
{
Expand Down Expand Up @@ -167,8 +175,9 @@ public function searchTableProxy($search, $offset = 0, $limit = 30, $params = ar
*
* @return Doctrine_Query
**/
public function createSearchQueryTableProxy($search, $offset=0, $limit = 30, array $params = array())
public function createSearchQueryTableProxy($search, $offset = 0, $limit = 30, array $params = array())
{
$primaryKey = $this->getTable()->getIdentifier();
$response = $this->getTable()->search($search, $offset, $limit, $params);

$pks = array();
Expand All @@ -183,15 +192,18 @@ public function createSearchQueryTableProxy($search, $offset=0, $limit = 30, arr

if($pks)
{
$q->whereIn($alias.'.id', $pks);
$q->whereIn($alias.'.'.$primaryKey, $pks);
// preserve score order
$q->addSelect(sprintf('FIELD(%s.id,%s) as field', $alias, implode(',', $pks)));
$q->addSelect(sprintf('FIELD(%s.%s,%s) as field', $alias, $primaryKey, implode(',', $pks)));
$q->orderBy('field');
}
else
{
$q->whereIn($alias.'.id', -1);
$q->whereIn($alias.'.'.$primaryKey, -1);
}

var_dump($response)."\n";
echo $q->getSqlQuery()."\n";

return $q;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/task/searchTask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function configure()
$this->addOptions(array(
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', null),
new sfCommandOption('start', null, sfCommandOption::PARAMETER_REQUIRED, 'The search offset', 0),
new sfCommandOption('offset', null, sfCommandOption::PARAMETER_REQUIRED, 'The search offset', 0),
new sfCommandOption('limit', null, sfCommandOption::PARAMETER_REQUIRED, 'The search limit', 10),
// add your own options here
));
Expand All @@ -40,7 +40,7 @@ protected function configure()

You can also add additional parameters:

[php symfony solr:search ModelClass query --offset=5 --limit=5|INFO]
[php symfony solr:search ModelClass "query" --offset=0 --limit=10|INFO]
EOF;
}

Expand All @@ -53,6 +53,7 @@ protected function execute($arguments = array(), $options = array())

$model = $arguments['model'];
$query = $arguments['query'];
$offset = $options['offset'];
$limit = $options['limit'];

if(!Doctrine_Core::getTable($model)->isSearchAvailable())
Expand All @@ -62,11 +63,11 @@ protected function execute($arguments = array(), $options = array())

$this->logSection('solr', 'Running search');

$q = Doctrine_Core::getTable($model)->createSearchQuery($query, $limit);
$q = Doctrine_Core::getTable($model)->createSearchQuery($query, $offset, $limit);
$results = $q->fetchArray();

$this->log(array(
sprintf('found %s results', count($results)),
sprintf('found %s result(s)', count($results)),
sfYaml::dump($results, 4)
));
}
Expand Down