Changeset 1316 for cpc/trunk/project/lib/model/solr/SolrListener.php
- Timestamp:
- Sep 4, 2010, 8:54:34 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpc/trunk/project/lib/model/solr/SolrListener.php
r1312 r1316 1 1 <?php 2 3 4 class SolrConnector extends sfLogger5 {6 private $solr = NULL;7 private $_options = NULL;8 9 public static function getFileCommands() {10 return sfConfig::get('sf_log_dir').'/solr/commands.log';11 }12 13 protected function doLog($message, $priority)14 {15 error_log(sprintf('%s (%s)', $message, sfLogger::getPriorityName($priority)));16 }17 18 public function __construct( $listener_options = NULL)19 {20 $host = sfConfig::get('app_solr_host', 'localhost');21 $port = sfConfig::get('app_solr_port', '8983');22 $url = sfConfig::get('app_solr_url', '/solr');23 $this->solr = new Apache_Solr_Service($host, $port, $url);24 25 if(!$this->solr->ping()) {26 throw new Exception('Search is not available right now.');27 // $this->doLog('SolrConnector: Arg, Search is not available right now', sfLogger::ERR);28 }29 30 $this->_options = $listener_options;31 32 return $this->solr;33 }34 35 36 public function updateFromCommands() {37 echo "UpdateFromCommands\n";38 echo file_exists(self::getFileCommands())."\n";39 if (!file_exists(self::getFileCommands().'.lock') && file_exists(self::getFileCommands())) {40 echo "rename \n";41 rename(self::getFileCommands(), self::getFileCommands().'.lock');42 }43 if (!file_exists(self::getFileCommands().'.lock')) {44 echo "No lock\n";45 return ;46 }47 foreach(file(self::getFileCommands().'.lock') as $line) {48 if (preg_match('/\] (UPDATE|REMOVE): (.+)/', $line, $matches)) {49 echo $matches[1]." ".$matches[2]."\n";50 $obj = json_decode($matches[2]);51 if ($matches[1] == 'UPDATE') {52 $this->updateLuceneRecord($obj);53 }else{54 $this->solr->deleteById($obj->id);55 }56 }57 }58 unlink(self::getFileCommands().'.lock');59 }60 61 62 public function deleteLuceneRecord($id)63 {64 if($this->solr->deleteById($id) )65 return $this->solr->commit();66 return false;67 }68 69 public function updateLuceneRecord($obj)70 {71 $document = new Apache_Solr_Document();72 $document->addField('id', $obj->id);73 $document->addField('object_id', $obj->object_id);74 $document->addField('object_name', $obj->object_name);75 if (isset($obj->wordcount))76 $document->addField('wordcount', $obj->wordcount);77 if (isset($obj->title))78 $document->addField('title', $obj->title->content, $obj->title->weight);79 if (isset($obj->description))80 $document->addField('description', $obj->description->content, $obj->description->weight);81 if (isset($obj->date))82 $document->addField('date', $obj->date->content, $obj->date->weight);83 $this->solr->addDocument($document);84 $this->solr->commit();85 }86 87 public function deleteAll() {88 $this->solr->deleteByQuery('*:*');89 $this->solr->commit();90 }91 92 public function search($queryString, $params = array(), $offset = 0, $maxHits = 0) {93 if($maxHits == 0)94 $maxHits = sfConfig::get('app_solr_max_hits', 256);95 $response = $this->solr->search($queryString, $offset, $maxHits, $params);96 return unserialize($response->getRawResponse());97 }98 99 }100 2 101 3 class SolrListener extends Doctrine_Record_Listener … … 141 43 } 142 44 143 private function sendCommand($status, $json) { 144 self::getFileCommand()->log($status.': '.json_encode($json)); 145 } 146 147 private function get_and_strip($obj, $field) { 148 $f = $obj->get($field); 149 if ($f) { 45 private function get_and_strip($obj, $field) { 46 $f = $obj->get($field); 47 if ($f) { 150 48 if (get_class($f) && ! $f->id) 151 49 return ; … … 172 70 } 173 71 174 // Réindexation après une création / modification 175 public function postSave(Doctrine_Event $event) 176 { 177 $obj = $event->getInvoker(); 178 // $this->getSolrConnector()->updateLuceneRecord($obj); 179 180 if ($t = $this->_options['index_if'] && $t && $obj->get($t)) 181 return ; 182 183 $json = array(); 184 $json['id'] = $this->getLuceneObjId($obj); 185 $json['object_id'] = $obj->getId(); 186 $json['object_name'] = get_class($obj); 187 188 72 // Réindexation après une création / modification 73 public function postSave(Doctrine_Event $event) 74 { 75 $obj = $event->getInvoker(); 76 77 if ($t = $this->_options['index_if'] && $t && $obj->get($t)) 78 return ; 79 80 $json = array(); 81 $json['id'] = $this->getLuceneObjId($obj); 82 $json['object_id'] = $obj->getId(); 83 $json['object_name'] = get_class($obj); 84 189 85 if ($t = $this->_options['description']) { 190 86 $content = $this->getObjFieldsValue($obj, $t); 191 87 $wordcount = str_word_count($content); 192 88 } 193 89 194 90 if (isset($this->_options['extra_weight'])) 195 91 $extra_weight = $this->_options['extra_weight']; … … 248 144 249 145 $json['tags']['weight'] = $extra_weight; 250 251 146 252 $this->sendCommand('UPDATE', $json); 253 } 254 255 // Désindexation après une suppression 256 public function postDelete(Doctrine_Event $event) 257 { 258 $obj = $event->getInvoker(); 259 // $this->getSolrConnector()->deleteLuceneRecord($obj); 260 $json = new stdClass(); 261 $json->id = $this->getLuceneObjId($obj); 262 $this->sendCommand('DELETE', $json); 263 } 264 147 SolrCommands::addCommand('UPDATE', $json); 148 } 149 150 // Désindexation après une suppression 151 public function postDelete(Doctrine_Event $event) 152 { 153 $obj = $event->getInvoker(); 154 $json = new stdClass(); 155 $json->id = $this->getLuceneObjId($obj); 156 SolrCommands::addCommand('DELETE', $json); 157 } 158 265 159 }
Note: See TracChangeset
for help on using the changeset viewer.