Changeset 1311
- Timestamp:
- Sep 1, 2010, 1:39:59 AM (10 years ago)
- Location:
- cpc/trunk/project
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpc/trunk/project/lib/model/solr/SolrListener.php
r1305 r1311 40 40 return ; 41 41 foreach(file(self::getFileCommands().'.lock') as $line) { 42 if (preg_match('/ (UPDATE|REMOVE): ([^\/]+)\/(\d+)/', $line, $matches)) {42 if (preg_match('/\] (UPDATE|REMOVE): (.+)/', $line, $matches)) { 43 43 if ($matches[1] == 'UPDATE') { 44 44 $obj = Doctrine::getTable($matches[2])->find($matches[3]); … … 55 55 } 56 56 57 private function get_and_strip($obj, $field) { 58 $f = $obj->get($field); 59 if ($f) { 60 if (get_class($f) && ! $f->id) 61 return ; 62 return strip_tags($f); 63 } 64 return ; 65 } 66 67 private function getObjFieldsValue($obj, $fields) 68 { 69 if (!is_array($fields)) { 70 return $this->get_and_strip($obj, $fields); 71 } 72 $s = ''; 73 foreach($fields as $f) { 74 $s .= $this->get_and_strip($obj, $f).' '; 75 } 76 return $s; 77 } 78 79 private function getLuceneObjId($obj) 80 { 81 return get_class($obj).'/'.$obj->getId(); 82 } 83 57 84 58 public function deleteLuceneRecord($obj) 85 59 { … … 92 66 { 93 67 $t = NULL; 94 if ($t = $this->_options['index_if'] && $t && $obj->get($t)) 95 return ; 96 $document = new Apache_Solr_Document(); 97 $document->addField('id', $this->getLuceneObjId($obj)); 98 $document->addField('object_id', $obj->getId()); 99 $document->addField('object_name', get_class($obj)); 100 101 102 if ($t = $this->_options['description']) { 103 $content = $this->getObjFieldsValue($obj, $t); 104 $wordcount = str_word_count($content); 105 } 106 107 if (isset($this->_options['extra_weight'])) 108 $extra_weight = $this->_options['extra_weight']; 109 else 110 $extra_weight = 1; 111 112 if (isset($this->_options['devaluate_if_wordcount_under']) && ($wclimit = $this->_options['devaluate_if_wordcount_under'])) { 113 if ($wclimit > $wordcount) 114 $extra_weight *= ($wordcount*0.5) / $wclimit + 0.5 ; 115 } 116 117 // On donne un poids plus important au titre 118 if (isset($this->_options['title']) && $t = $this->_options['title']) { 119 $document->addField('title', $this->getObjFieldsValue($obj, $t), 1.2 * $extra_weight); 120 } 121 122 // La description 123 if (isset($content)) { 124 $document->addField('description', $content, $extra_weight); 125 $document->addField('wordcount', $wordcount); 126 } 127 128 // par default la date est la created_at 129 if ( !($t = $this->_options['date'])) { 130 $t = 'created_at'; 131 } 132 $d = preg_replace('/\+.*/', 'Z', date('c', strtotime($this->getObjFieldsValue($obj, $t)))); 133 $document->addField('date', $d, $extra_weight); 134 135 try { 136 foreach($obj->getTags() as $tag) if ($tag) { 137 $document->setMultiValue('tag', preg_replace('/:/', '=', $tag), $extra_weight); 138 } 139 }catch (Exception $e) {} 140 141 if ($t = $this->_options['moretags']) { 142 if (!is_array($t)) { 143 $s = $this->get_and_strip($obj, $t); 144 if ($s) 145 $document->setMultiValue('tag', $t.'='.$s, $extra_weight); 146 }else{ 147 foreach ($t as $i) { 148 $s = $this->get_and_strip($obj, $i); 149 if (strlen($s)) { 150 $s = strip_tags($s); 151 $document->setMultiValue('tag', $i.'='.$s, $extra_weight); 152 } 153 } 154 } 155 } 68 $obj_options = $obj->getListener()->getOptions(); 69 70 print_r($obj->getOptions()); 71 exit; 156 72 157 73 $this->solr->addDocument($document); … … 215 131 } 216 132 217 private function sendCommand($status, $obj) { 218 self::getFileCommand()->log($status.': '.get_class($obj).'/'.$obj->id); 219 } 220 133 private function sendCommand($status, $json) { 134 self::getFileCommand()->log($status.': '.json_encode($json)); 135 } 136 137 private function get_and_strip($obj, $field) { 138 $f = $obj->get($field); 139 if ($f) { 140 if (get_class($f) && ! $f->id) 141 return ; 142 return strip_tags($f); 143 } 144 return ; 145 } 146 147 private function getObjFieldsValue($obj, $fields) 148 { 149 if (!is_array($fields)) { 150 return $this->get_and_strip($obj, $fields); 151 } 152 $s = ''; 153 foreach($fields as $f) { 154 $s .= $this->get_and_strip($obj, $f).' '; 155 } 156 return $s; 157 } 158 159 private function getLuceneObjId($obj) 160 { 161 return get_class($obj).'/'.$obj->getId(); 162 } 163 221 164 // Réindexation après une création / modification 222 165 public function postSave(Doctrine_Event $event) … … 224 167 $obj = $event->getInvoker(); 225 168 // $this->getSolrConnector()->updateLuceneRecord($obj); 226 $this->sendCommand('UPDATE', $obj); 169 170 if ($t = $this->_options['index_if'] && $t && $obj->get($t)) 171 return ; 172 173 $json = array(); 174 $json['id'] = $this->getLuceneObjId($obj); 175 $json['object_id'] = $obj->getId(); 176 $json['object_name'] = get_class($obj); 177 178 179 if ($t = $this->_options['description']) { 180 $content = $this->getObjFieldsValue($obj, $t); 181 $wordcount = str_word_count($content); 182 } 183 184 if (isset($this->_options['extra_weight'])) 185 $extra_weight = $this->_options['extra_weight']; 186 else 187 $extra_weight = 1; 188 189 if (isset($this->_options['devaluate_if_wordcount_under']) && ($wclimit = $this->_options['devaluate_if_wordcount_under'])) { 190 if ($wclimit > $wordcount) 191 $extra_weight *= ($wordcount*0.5) / $wclimit + 0.5 ; 192 } 193 194 // On donne un poids plus important au titre 195 if (isset($this->_options['title']) && $t = $this->_options['title']) { 196 $json['title']['content'] = $this->getObjFieldsValue($obj, $t); 197 $json['title']['weight'] = 1.2 * $extra_weight; 198 } 199 200 // La description 201 if (isset($content)) { 202 $json['description']['content'] = $content; 203 $json['description']['weight'] = $extra_weight; 204 $json['wordcount'] = $wordcount; 205 } 206 207 // par default la date est la created_at 208 if ( !($t = $this->_options['date'])) { 209 $t = 'created_at'; 210 } 211 $d = preg_replace('/\+.*/', 'Z', date('c', strtotime($this->getObjFieldsValue($obj, $t)))); 212 $json['date']['content'] = $d; 213 $json['date']['weight'] = $extra_weight; 214 215 216 $json['tags']['content'] = array(); 217 try { 218 foreach($obj->getTags() as $tag) if ($tag) { 219 $json['tags']['content'][] = preg_replace('/:/', '=', $tag); 220 } 221 }catch (Exception $e) {} 222 223 if ($t = $this->_options['moretags']) { 224 if (!is_array($t)) { 225 $s = $this->get_and_strip($obj, $t); 226 if ($s) 227 $json['tags']['content'][] = $t.'='.$s; 228 }else{ 229 foreach ($t as $i) { 230 $s = $this->get_and_strip($obj, $i); 231 if (strlen($s)) { 232 $s = strip_tags($s); 233 $json['tags']['content'][] = $i.'='.$s; 234 } 235 } 236 } 237 } 238 239 $json['tags']['weight'] = $extra_weight; 240 241 242 $this->sendCommand('UPDATE', $json); 227 243 } 228 244 … … 232 248 $obj = $event->getInvoker(); 233 249 // $this->getSolrConnector()->deleteLuceneRecord($obj); 234 $this->sendCommand('DELETE', $obj); 250 $json = new stdClass(); 251 $json->id = $this->getLuceneObjId($obj); 252 $this->sendCommand('DELETE', $json); 235 253 } 236 254 -
cpc/trunk/project/test/unit/solrableTest.php
r1304 r1311 26 26 $id = "Intervention/".$i->id; 27 27 $a = $s->search("id:$id"); 28 print_r($a); 28 29 29 $t->is(count($a['response']['docs']), 1, "L'intervention a été ajoutée"); 30 30 $a = $s->search("bonjour id:$id"); 31 print_r($a);32 31 $t->is($a['response']['docs'][0]['id'], $id, "L'intervention est trouvable"); 32 33 33 $a = $s->search("salut id:$id"); 34 34 $t->is(count($a['response']['docs']), 0, "L'intervention n'est pas retournée sur des mots non indexé"); 35 35 36 $i->intervention = $inter." salut"; 36 37 $i->save();
Note: See TracChangeset
for help on using the changeset viewer.