1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| //GeekGeekerie.class.php
public function save(Doctrine_Connection $conn = null)
{
// ...
$conn = $conn ? $conn : $this->getTable()->getConnection();
$conn->beginTransaction();
try
{
$ret = parent::save($conn);
$this->updateLuceneIndex();
$conn->commit();
return $ret;
}
catch (Exception $e)
{
$conn->rollBack();
throw $e;
}
}
public function delete(Doctrine_Connection $conn = null)
{
$index = $this->getTable()->getLuceneIndex();
if ($hit = $index->find('pk:'.$this->getId()))
{
$index->delete($hit->id);
}
return parent::delete($conn);
}
public function updateLuceneIndex()
{
$index = $this->getTable()->getLuceneIndex();
// remove an existing entry
if ($hit = $index->find('pk:'.$this->getId()))
{
$index->delete($hit->id);
}
// don't index non active geekerie
if (!$this->isActive($this))
{
return;
}
$doc = new Zend_Search_Lucene_Document();
// store job primary key URL to identify it in the search results
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('pk', $this->getId()));
// index job fields
$doc->addField(Zend_Search_Lucene_Field::UnStored('content', $this->getContent(), 'utf-8'));
$doc->addField(Zend_Search_Lucene_Field::UnStored('author', $this->getAuthor(), 'utf-8'));
// add job to the index
$index->addDocument($doc);
$index->commit();
}
//GeekGeekerieTable.class.php
static public function getLuceneIndex()
{
ProjectConfiguration::registerZend();
if (file_exists($index = self::getLuceneIndexFile()))
{
return Zend_Search_Lucene::open($index);
}
else
{
return Zend_Search_Lucene::create($index);
}
}
static public function getLuceneIndexFile()
{
return sfConfig::get('sf_data_dir').'/geekerie.'.sfConfig::get('sf_environment').'.index';
}
public function getForLuceneQuery($query)
{
$hits = $this->getLuceneIndex()->find($query);
$pks = array();
foreach ($hits as $hit)
{
$pks[] = $hit->pk;
}
if (empty($pks))
{
return 'no results';
}
$q = $this->createQuery('g')
->whereIn('g.id', $pks)
->limit(20);
return $q->execute();
}
//L'action
public function executeSearch(sfWebRequest $request)
{
if (!$query = $request->getParameter('query'))
{
return $this->forward('geekerie', 'index');
}
$this->geek_geekerie_list = Doctrine::getTable('GeekGeekerie')->getForLuceneQuery($query);
echo $this->geek_geekerie_list;
}
//Le tpl
<?php foreach ($geek_geekerie_list as $geek_geekerie): ?>
<?php include_partial('geekerie', array('geek_geekerie' => $geek_geekerie,'aVote' => $aVote)); ?>
<?php endforeach; ?> |
Partager