public function deleteModel(sfLucene $lucene, $model) { $query = 'sfl_model:' . $model; $lucene->getLucene()->deleteByQuery($query); $lucene->getLucene()->commit(); $lucene->getLucene()->optimize(); }
/** * Singleton constructor * @param sfLucene $search The search instance to tie to * @param sfLuceneStorage $writer The writer (optional) */ public function __construct($search, $writer = null) { if (!$search instanceof sfLucene) { throw new sfLuceneException('Search must be an instance of sfLucene'); } if ($writer == null) { $writer = new sfLuceneStorageFilesystem(sfConfig::get('sf_cache_dir') . DIRECTORY_SEPARATOR . $search->getParameter('index_location') . DIRECTORY_SEPARATOR . 'categories.php'); } if (!$writer instanceof sfLuceneStorage) { throw new sfLuceneException('Writer must be an instance of sfLuceneStorage'); } $this->writer = $writer; $this->load(); }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); $this->createConfigFiles(sfLucene::getConfig($this->configuration)); }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $this->notifyListRow('Plugin Version', 'sfLucene ' . sfLucene::VERSION); if ($app) { $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); foreach (sfLucene::getAllInstances() as $search) { $this->dispatcher->notify(new sfEvent($this, 'command.log', array($this->formatter->format(sprintf('For %s/%s:', $search->getParameter('name'), $search->getParameter('culture')), array('fg' => 'red', 'bold' => true))))); $segments = $search->segmentCount(); $this->notifyListRow('Document Count', $search->numDocs(), 3); $this->notifyListRow('Segment Count', $segments, 3); $rawSize = $search->byteSize(); $size = $rawSize / 1024 > 1024 ? number_format($rawSize / pow(1024, 2), 3) . 'MB' : number_format($rawSize / 1024, 3) . ' KB'; $this->notifyListRow('Index Size', $size, 3); if ($segments == 0) { $condition = 'Empty: Perhaps you should rebuild the index?'; } elseif ($segments == 1) { $condition = 'Great: No optimization neccessary'; } elseif ($segments <= 10) { $condition = 'Good: Consider optimizing for full performance'; } elseif ($segments <= 20) { $condition = 'Bad: Optimization is recommended'; } else { $condition = 'Terrible: Immediate optimization neccessary!'; } $this->notifyListRow('Index Condition', $condition, 3); } } }
public function __construct() { $this->lucene = sfLucene::getInstance('index', 'en'); if (!$this->lucene->getSearchService()->ping()) { throw new Exception('Solr index server not loaded - please use >symfony lucene:service client start'); } $this->service = $this->lucene->getSearchService(); }
protected function execute($arguments = array(), $options = array()) { $this->standardBootstrap($arguments['application'], $options['env']); if ($arguments['confirmation'] == 'delete') { $this->dispatcher->notify(new sfEvent($this, 'command.log', array($this->formatter->format('Delete confirmation provided. Deleting index now...', array('fg' => 'red', 'bold' => true))))); $instances = sfLucene::getAllInstances(); foreach ($instances as $instance) { $instance->getLucene()->deleteByQuery('*:*'); $instance->getLucene()->commit(); $this->dispatcher->notify(new sfEvent($this, 'command.log', array($this->formatter->format('Delete ' . $instance->getPublicName(), array('fg' => 'red', 'bold' => true))))); } } }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); $start = microtime(true); $instances = sfLucene::getAllInstances(); foreach ($instances as $instance) { $this->optimize($instance); } $time = microtime(true) - $start; $final = $this->formatter->format('All done!', array('fg' => 'red', 'bold' => true)) . ' Optimized for ' . $this->formatter->format(count($instances), array('fg' => 'cyan')); $final .= count($instances) == 1 ? ' index in ' : ' indexes in '; $final .= $this->formatter->format(number_format($time, 5), array('fg' => 'cyan')) . ' seconds.'; $this->dispatcher->notify(new sfEvent($this, 'command.log', array('', $final))); }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $index = $arguments['index']; $culture = $arguments['culture']; $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); $start = microtime(true); $instance = sfLucene::getInstance($index, $culture, true); $this->setupEventDispatcher($instance); $this->rebuild($instance); $time = microtime(true) - $start; $final = $this->formatter->format('All done!', array('fg' => 'red', 'bold' => true)) . ' Rebuilt for ' . $this->formatter->format(count($instances), array('fg' => 'cyan')); $final .= count($instances) == 1 ? ' index in ' : ' indexes in '; $final .= $this->formatter->format(number_format($time, 5), array('fg' => 'cyan')) . ' seconds.'; $this->dispatcher->notify(new sfEvent($this, 'command.log', array('', $final))); }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $index = $arguments['index']; $culture = $arguments['culture']; $model = $arguments['model']; $offset = $options['offset']; $limit = $options['limit']; $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); if (sfConfig::get('sf_orm') != 'doctrine') { throw new LogicException('This feature is only implemented for Doctrine ORM'); } sfLucene::initIndex($index, $culture); $instance = sfLucene::getInstance($index, $culture, false); $this->setupEventDispatcher($instance); $this->rebuild($instance, $model, $offset, $limit); }
protected function getSearchInstances($node) { static $instances; $class = get_class($node); if (!isset($instances)) { $instances = array(); } if (!isset($instances[$class])) { $config = sfLucene::getConfig(); foreach ($config as $name => $item) { if (isset($item['models'][$class])) { foreach ($item['index']['cultures'] as $culture) { $instances[$class][] = sfLucene::getInstance($name, $culture); } } } } return $instances[$class]; }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $index = $arguments['index']; $culture = $arguments['culture']; $limit = $options['limit']; $model = $options['model']; $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); if (sfConfig::get('sf_orm') != 'doctrine') { throw new LogicException('This feature is only implemented for Doctrine ORM'); } $start = microtime(true); $search = sfLucene::getInstance($index, $culture, is_null($model)); $search->optimize(); $this->setupEventDispatcher($search); $models = $search->getParameter('models')->getAll(); $factory = new sfLuceneIndexerFactory($search); $handler = null; foreach ($factory->getHandlers() as $handler) { if ($handler instanceof sfLuceneModelIndexerHandler) { break; } } if (!$handler instanceof sfLuceneModelIndexerHandler) { throw new LogicException('No sfLuceneModelIndexerHandler defined !'); } if ($model) { $this->update($handler, $app, $index, $culture, $model, $limit); } else { foreach ($models as $model => $params) { $this->update($handler, $app, $index, $culture, $model, $limit); } } $time = microtime(true) - $start; $final = $this->formatter->format('Update index done !!', array('fg' => 'green', 'bold' => true)); $final .= $this->formatter->format(number_format($time, 5), array('fg' => 'cyan')) . ' seconds.'; $this->dispatcher->notify(new sfEvent($this, 'command.log', array('', $final))); }
/** * Returns an array of the index paths to be removed by the garbage cleanup routine. */ public static function getDirtyIndexRemains() { $location = sfConfig::get('sf_data_dir') . DIRECTORY_SEPARATOR . 'index'; $length = strlen($location) + 1; $config = sfLucene::getConfig(); $remove = array(); $namesRemoved = array(); foreach (sfFinder::type('dir')->mindepth(0)->maxdepth(0)->in($location) as $directory) { $name = substr($directory, $length); if (!isset($config[$name])) { $namesRemoved[] = $name; $remove[] = $directory; } } foreach (sfFinder::type('dir')->mindepth(1)->maxdepth(1)->in($location) as $directory) { $interested = substr($directory, $length); list($name, $culture) = explode('/', $interested); if (!in_array($name, $namesRemoved) && !in_array($culture, $config[$name]['index']['cultures'])) { $remove[] = $directory; } } return $remove; }
protected function execute($arguments = array(), $options = array()) { $app = $arguments['application']; $index = $arguments['index']; $culture = $arguments['culture']; $model = $arguments['model']; $state = $options['state']; $limit = $options['limit']; $page = $options['page']; $delete = $options['delete']; $this->checkAppExists($app); $this->standardBootstrap($app, $options['env']); if (sfConfig::get('sf_orm') != 'doctrine') { throw new LogicException('This feature is only implemented for Doctrine ORM'); } if ($state) { // use state file // the state file only contains the last page used and the limit $state = $this->getState($model); $page = $state['page']; $limit = $state['limit']; $this->logSection('lucene', sprintf('Loading state page:%s, limit:%s', $page, $limit)); } $this->dispatcher->connect('lucene.indexing_loop', array($this, 'handleMemoryLimitEvent')); $instance = sfLucene::getInstance($index, $culture, $this->configuration); $this->setupEventDispatcher($instance); if ($delete) { $query = 'sfl_model:' . $model; $instance->getSearchService()->deleteByQuery($query); $instance->getSearchService()->commit(); } $this->rebuild($instance, $model, $page, $limit); if ($state) { $file = $this->getFilestatePath($model); $this->getFilesystem()->remove($file); } }
/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $config = sfLucene::getConfig(); $query = $arguments['query']; // check and reduce indexes if ($options['index'] !== null) { if (!in_array($options['index'], array_keys($config))) { throw new Exception('Index %s not exist', $options['index']); } $config = array($config[$options['index']]); } // build the lucene criteria $criteria = sfLuceneCriteria::newInstance()->addPhrase($query)->setOffset($options['start'])->setLimit($options['limit']); // walk over the indexes foreach ($config as $index => $indexConfig) { // check culture $cultures = $indexConfig['index']['cultures']; if ($options['culture'] !== null) { if (!in_array($options['culture'], $cultures)) { //TODO: change to log error throw new Exception(sprintf('Culture %s is not configurate for index %s', $options['culture'], $index)); } $cultures = array($options['culture']); } $this->log(sprintf('search for `%s` from %u to %u', $query, $options['start'], $options['limit'])); // walk over the cultures foreach ($cultures as $culture) { // get lucene instance and retrieve the results $results = sfLucene::getInstance($index, $culture)->friendlyFind($criteria); $this->log(sprintf('found %u results in index `%s` with culture `%s`', count($results), $index, $culture)); foreach ($results as $result) { $this->logSection('result ', sprintf('%s %s (%u%%)', $result->getInternalModel(), $result->getInternalTitle(), $result->getScore())); } } } }
$behavior->preDelete($m1); $t->is(count($behavior->_getDeleteQueue()), 0, '::setLock() disables the delete queue'); $behavior->clear(); sfLucenePropelBehavior::setLock(false); $behavior->preSave($m1); $t->is(count($behavior->_getSaveQueue()), 1, '::setLock() enables the save queue'); $behavior->preDelete($m1); $t->is(count($behavior->_getDeleteQueue()), 1, '::setLock() enables the delete queue'); $behavior->clear(); foreach (array($m1, $m2, $m3, $m3) as $m) { $indexer = new sfLucenePropelIndexer(sfLucene::getInstance('testLucene', 'en'), $m); $indexer->delete(); } $t->diag('testing ->insertIndex()'); $en = sfLucene::getInstance('testLucene', 'en'); $fr = sfLucene::getInstance('testLucene', 'fr'); foreach (array('en', 'fr') as $cult) { $indexer = new sfLucenePropelIndexer(${$cult}, $m1); $indexer->delete(); } $behavior->insertIndex($m1); $en->commit(); $fr->commit(); $t->is($en->numDocs(), 1, '->insertIndex() added model to first index it appears in'); $t->is($fr->numDocs(), 1, '->insertIndex() added model to second index it appears in'); $t->diag('testing ->deleteIndex();'); $behavior->deleteIndex($m1); $en->commit(); $fr->commit(); $t->is($en->numDocs(), 0, '->deleteIndex() deleted model from first index it appears in'); $t->is($fr->numDocs(), 0, '->deleteIndex() deleted model from second index it appears in');
$t->isa_ok($f, 'sfParameterHolder', 'model property "fields" is a sfParameterHolder'); $t->is($f->getNames(), array('id', 'title', 'description'), 'model property "fields" contains all the fields'); $t->is($f->get('id')->get('type'), 'unindexed', 'field property "type" is the type'); $t->is($f->get('id')->get('boost'), 1, 'field property "boost" is the boost'); $t->diag('testing ::getConfig()'); $cswap = $app->cswap($luceneade->config_dir . '/search.yml')->write('<?php $foo = 42;'); try { $e = $t->exception('::getConfig() fails if search.yml is corrupt'); sfLucene::getConfig(); $e->no(); } catch (Exception $ex) { $e->caught($ex); } $cswap->write('<?php $config = array(1, 2, 3);'); try { $t->is(sfLucene::getConfig(), array(1, 2, 3), '::getConfig() returns the $config variable in the search.yml file'); } catch (Exception $e) { $t->fail('::getConfig() returns the $config variable in the search.yml file'); } $cswap->restore(); $t->diag('testing ->getCategoriesHarness()'); $cats = $lucene->getCategoriesHarness(); $t->isa_ok($cats, 'sfLuceneCategories', '->getCategories() returns an instance of sfLuceneCategories'); $t->ok($lucene->getCategoriesHarness() === $cats, '->getCategories() is a singleton'); $t->diag('testing ->getIndexerFactory()'); $indexer = $lucene->getIndexerFactory(); $t->isa_ok($indexer, 'sfLuceneIndexerFactory', '->getIndexer() returns an instance of sfLuceneIndexerFactory'); $t->diag('testing ->getContext()'); $t->isa_ok($lucene->getContext(), 'sfContext', '->getContext() returns an instance of sfContext'); $t->is($lucene->getContext(), sfContext::getInstance(), '->getContext() returns the same context'); $t->diag('testing ->configure()');
function inst() { return sfLuceneCriteria::newInstance(sfLucene::getInstance('testLucene')); }
<?php /* * This file is part of the sfLucenePlugin package * (c) 2007 - 2008 Carl Vondrick <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * @package sfLucenePlugin * @subpackage Test * @author Carl Vondrick * @version SVN: $Id$ */ require dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new limeade_test(1, limeade_output::get()); $lucene = sfLucene::getInstance('index', 'en', $app_configuration); class MockResult extends sfLuceneDocument { public function __construct($a) { } } $doc = new MockResult('a'); try { new sfLuceneActionResult($doc, $lucene); $t->pass('__construct() can be called'); } catch (Exception $e) { $t->fail('__construct() can be called'); }
$root = sfConfig::get('sf_data_dir') . '/index/'; // build valid indexes structure sfLucene::getInstance('testLucene', 'en')->getLucene(); sfLucene::getInstance('testLucene', 'fr')->getLucene(); // build invalid indexes structures file_put_contents($root . 'testLucene/en/random_file', 'r@nd()'); mkdir($root . 'testLucene/foo', 0777, true); file_put_contents($root . 'testLucene/foo/bar', 'foo'); mkdir($root . 'badIndex/en', 0777, true); file_put_contents($root . 'badIndex/bar', 'foo'); $dirty = sfLuceneToolkit::getDirtyIndexRemains(); $t->ok(in_array($root . 'testLucene/foo', $dirty), '::getDirtyIndexRemains() schedules valid indexes but invalid cultures for deletion'); $t->ok(in_array($root . 'badIndex', $dirty), '::getDirtyIndexRemains() schedules the entire of a bad index for deletion'); $t->ok(!in_array($root . 'testLucene', $dirty), '::getDirtyIndexRemains() did not schedule an entire valid index for deletion'); $t->ok(!in_array($root . 'testLucene/en', $dirty), '::getDirtyIndexRemains() did not schedule a valid index and valid culture for deletion'); $t->ok(!in_array($root . 'testLucene/fr', $dirty), '::getDirtyIndexRemains() did not schedule another valid index and valid culture for deletion'); $t->ok(!in_array($root . 'testLucene/en/random_file', $dirty), '::getDirtyIndexRemains() did not schedule an alien file in a valid index and valid culture for deletion'); $t->diag('testing ::getApplicationInstance'); $t->ok(sfLuceneToolkit::getApplicationInstance('en') === sfLucene::getInstance('testLucene', 'en'), '::getApplicationInstance() guesses the first index with no configuration parameter set'); sfConfig::set('app_lucene_index', 'fooLucene'); $t->ok(sfLuceneToolkit::getApplicationInstance('en') === sfLucene::getInstance('fooLucene', 'en'), '::getApplicationInstance() acknowledges manual override from app.yml'); $limeade->config()->remove('app_lucene_index'); $cswap = $app->cswap($luceneade->config_dir . '/search.yml')->write('<?php $config = array();'); try { $e = $t->exception('::getApplicationInstance() fails if search.yml is empty'); sfLuceneToolkit::getApplicationInstance(); $e->no(); } catch (Exception $ex) { $e->caught($ex); } $cswap->restore();
public function start($app, $env, $options = array()) { if ($this->isRunning($app, $env)) { throw new sfException('Server is running, cannot start (pid file : ' . $this->getPidFile($app, $env) . ')'); } $instances = sfLucene::getAllInstances($this->configuration); if (count($instances) == 0) { throw new sfException('There is no Solr instance for the current application'); } $host = $instances[0]->getParameter('host'); $port = $instances[0]->getParameter('port'); $base_url = $instances[0]->getParameter('base_url'); // start the jetty built in server $command = sprintf('cd %s/plugins/sfSolrPlugin/lib/vendor/Solr/example; %s %s -Xmx%s -Xms%s -Dsolr.solr.home=%s/config/solr/ -Dsolr.data.dir=%s/data/solr_index -Dsolr.lib.dir=%s/plugins/sfSolrPlugin/lib/vendor/Solr/example/solr/lib -Djetty.port=%s -Djetty.logs=%s -jar start.jar > %s/solr_server_%s_%s.log 2>&1 & echo $!', sfConfig::get('sf_root_dir'), $this->nohup, $this->java, $options['Xmx'], $options['Xms'], sfConfig::get('sf_root_dir'), sfConfig::get('sf_root_dir'), sfConfig::get('sf_root_dir'), $port, sfConfig::get('sf_root_dir') . '/log', sfConfig::get('sf_root_dir') . '/log', $app, $env); $this->logSection('exec ', $command); exec($command, $op); if (method_exists($this->getFilesystem(), 'execute')) { $this->getFilesystem()->execute(sprintf('cd %s', sfConfig::get('sf_root_dir'))); } else { $this->getFilesystem()->sh(sprintf('cd %s', sfConfig::get('sf_root_dir'))); } $pid = (int) $op[0]; file_put_contents($this->getPidFile($app, $env), $pid); $this->logSection("solr", "Server started with pid : " . $pid); $this->logSection("solr", "server started : http://" . $host . ":" . $port . $base_url); }
* For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * @package sfLucenePlugin * @subpackage Test * @author Carl Vondrick * @version SVN: $Id: sfLucenePagerTest.php 7108 2008-01-20 07:44:42Z Carl.Vondrick $ */ require dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new limeade_test(30, limeade_output::get()); $limeade = new limeade_sf($t); $app = $limeade->bootstrap(); $luceneade = new limeade_lucene($limeade); $luceneade->configure()->clear_sandbox(); $lucene = sfLucene::getInstance('testLucene', 'en'); $t->diag('testing constructor'); try { new sfLucenePager('a', $lucene); $t->fail('__construct() rejects a non-array'); } catch (Exception $e) { $t->pass('__construct() rejects a non-array'); } try { new sfLucenePager(new sfLuceneResults(array(), $lucene)); $t->pass('__construct() accepts sfLuceneResults'); } catch (Exception $e) { $t->fail('__construct() accepts sfLuceneResults'); } try { new sfLucenePager(array(), null);
/** * Finds all instances of sfLucene that this model appears in. This does * not return the instance if the model does not exist in it. */ protected function getSearchInstances($node) { static $instances; $class = get_class($node); if (!isset($instances)) { $instances = array(); } // continue only if we have not already cached the instances for this class if (!isset($instances[$class])) { $instances[$class] = array(); $config = sfLucene::getConfig(); // go through each instance foreach ($config as $name => $item) { if (isset($item['models'][$class])) { foreach ($item['index']['cultures'] as $culture) { // store instance $instances[$class][] = sfLucene::getInstance($name, $culture, sfProjectConfiguration::getActive()); } } } } if (count($instances[$class]) == 0) { throw new sfLuceneException('No sfLucene instances could be found for "' . $class . '"'); } return $instances[$class]; }
protected function getSearchInstances($node) { $class = get_class($node); if (!isset(self::$instances[$class])) { $config = sfLucene::getConfig(); $configuration = sfProjectConfiguration::getActive(); foreach ($config as $name => $item) { $inheritance_class = $this->getInheritanceClass($node, $item); if (!$inheritance_class) { throw new sfException('Cannot find the correct inheritance class for the object type : ' . get_class($node)); } if (isset($item['models'][$inheritance_class])) { foreach ($item['index']['cultures'] as $culture) { self::$instances[$class][] = sfLucene::getInstance($name, $culture, $configuration); } } } } return self::$instances[$class]; }
{ return new MockDocument(); } } class MockDocument { public function getFieldValue($field) { if ($field == 'sfl_type') { return 'regular'; } throw new Exception('d'); } } $data = array(new MockResult('foo'), new MockResult('bar'), new MockResult('baz')); $search = sfLucene::getInstance('testLucene'); $results = new sfLuceneResults($data, $search); $t->diag('testing ->getSearch(), ->toArray()'); $t->is($results->getSearch(), $search, '->getSearch() returns the same search instance'); $t->is($results->toArray(), $data, '->toArray() returns the same search data'); $t->diag('testing Iterator interface'); $got = array(); $once = false; foreach ($results as $key => $value) { if (!$once) { $t->ok($value instanceof sfLuceneResult, 'iterator interface returns instances of sfLuceneResult'); $once = true; } $got[$key] = $value->getResult(); } $t->is($got, $data, 'sfLuceneResults implements the Iterator interface');