user_query = $user_query; $this->sphinx_query = analyse_query::get_sphinx_query($this->user_query); $this->sc = new SphinxClient(); $connect_params = explode(',', $sphinx_mysql_connect); if ($connect_params[0]) { $tmp = explode(":",$connect_params[0]); $this->sc->_host = $tmp[0]; } $this->sc->Open(); $this->sc->SetLimits(0, $this->bypass,$this->maxmatches); $this->sc->SetArrayResult(true); $this->sc->SetMatchMode(SPH_MATCH_EXTENDED2); $this->sc->SetSortMode(SPH_SORT_EXTENDED, '@weight DESC'); $this->sc->SetRankingMode(SPH_RANK_PROXIMITY_BM25); $this->sc->SetSelect("id"); $this->sphinx_base = new sphinx_base(); $this->sphinx_base->setDefaultIndex($this->index_name); $this->sphinx_base->setChampBaseFilepath($this->champ_base_path); $this->sc->SetFieldWeights($this->sphinx_base->get_fields_pond()); } protected function get_search_indexes(){ global $lang; return $this->index_name.'_'.$lang.','.$this->index_name; } protected function get_full_raw_query(){ //A REDEFINIR return 'select notice_id as id, 100 as weight from notices'; } protected function get_tempo_tablename(){ return 'sphinx_'.md5(get_class($this).'_'.md5($this->sphinx_query)); } protected function _get_objects_ids(){ if(isset($this->objects_ids)){ return $this->objects_ids; } $this->objects_ids = ''; $this->_build_tmp_table(); $query = ''; if ($this->sphinx_query != '*') { $query = $this->get_fields_restrict().' ('.$this->sphinx_query.') '; } if ($this->sphinx_query == '*' && !$this->get_filter_query()){ $query = $this->get_full_raw_query(); $result = pmb_mysql_query($query); $response = array(); while($row = pmb_mysql_fetch_assoc($result)){ $response[] = $row; if($this->objects_ids){ $this->objects_ids.=','; } $this->objects_ids.= $row['id']; } $this->insert_in_tmp_table($response); return $this->objects_ids; } $nb = 0; do { $this->sc->SetLimits($nb, $this->bypass); $result = $this->sc->Query($query.$this->get_filter_query(),$this->get_search_indexes()); for($i = 0 ; $iobjects_ids){ $this->objects_ids.=','; } $this->objects_ids.=$result['matches'][$i]['id']; } $nb+=count($result['matches']); $this->insert_in_tmp_table($result['matches']); if(!$this->nb_result){ $this->nb_result = $result['total_found']; } } while ($nb < $result['total_found']); return $this->objects_ids; } protected function _build_tmp_table(){ global $memo_tempo_table_to_rebuild; $memo_tempo_table_to_rebuild = array(); $query = 'create temporary table '.$this->get_tempo_tablename().'('.$this->id_key.' int,pert int,index using btree('.$this->id_key.')) engine=memory' ; $memo_tempo_table_to_rebuild[] = $query; pmb_mysql_query($query); } protected function insert_in_tmp_table($objects){ if(count($objects)){ global $memo_tempo_table_to_rebuild; $query = 'insert into '.$this->get_tempo_tablename().'('.$this->id_key.', pert) values '; for($i = 0 ; $i0){ $query.=', '; } $query.= '('.$objects[$i]['id'].','.$objects[$i]['weight'].')'; } $memo_tempo_table_to_rebuild[] = $query; pmb_mysql_query($query); } } public function get_result(){ //$start = microtime(true); //print '
lancement de get_result
'; $this->_get_objects_ids(); //printtime('searcher_sphinx::_get_objects_ids'); $this->_filter_results(); //printtime('searcher_sphinx::_filter_results'); //print '

FILTER RESULT : '.count(explode(',',$this->objects_ids)).'

'; return $this->objects_ids; } protected function _filter_results(){ //A REDEFINIR } public function get_raw_query(){ $this->_get_objects_ids(); $query = 'select '.$this->id_key.', pert from '.$this->get_tempo_tablename(); return $query; } public function get_full_query(){ $this->get_result(); $query = 'select '.$this->id_key.', pert from '.$this->get_tempo_tablename(); return $query; } public function get_nb_results(){ if($this->nb_result){ return $this->nb_result; } $this->get_result(); if (!$this->nb_result && ($this->objects_ids != '')) { $this->nb_result = count(explode(',', $this->objects_ids)); } return $this->nb_result; } public function get_sorted_result($tri = "default",$start=0,$number=20){ //A REDEFINIR $this->tri = $tri; $this->get_result(); $sort = new sort("notices","session"); $query = $sort->appliquer_tri_from_tmp_table($this->tri,$this->get_tempo_tablename(),$this->id_key,$start,$number); $res = pmb_mysql_query($query); if(pmb_mysql_num_rows($res)){ $this->result=array(); while($row = pmb_mysql_fetch_object($res)){ $this->result[] = $row->notice_id; } } return $this->result; } public function explain($display,$mode,$mini=false){ //A DERIVER si on veut } public function get_fields_restrict(){ if(!count($this->fields_restrict)){ return ''; } $this->fields_restrict = array_unique($this->fields_restrict); return '@('.implode(',',$this->fields_restrict).')'; } public function init_fields_restrict($mode){ $this->fields_restrict = array(); } public function add_fields_restrict($restrict){ $indexes = $this->sphinx_base->getIndexes(); $indexes = $indexes[$this->index_name]['fields']; for($i=0 ; $i0){ for($j=0 ; $jfields_restrict[]='f_'.str_pad($value, 3,"0",STR_PAD_LEFT).'_'.str_pad($sub_value, 2,"0",STR_PAD_LEFT); } } } }else{ foreach($restrict[$i]['values'] as $value){ for($j=0 ; $jfields_restrict[]=$indexes[$j]; } } } } } } } public function get_results_list_from_search($label, $user_input, $list, $navbar) { $template = "

".$this->get_nb_results()." ".$label." ".$user_input."

".$list."
".$navbar."
"; return $template; } public function get_pert_result($query = false){ if ($query) { return 'select '.$this->id_key.', pert from '.$this->get_tempo_tablename(); } return $this->get_tempo_tablename(); } protected function get_filters(){ return array(); } protected function get_filter_query() { if(!isset($this->filter_query)){ $filters = $this->get_filters(); $filter_query = ''; for($i=0 ; $ifilter_query = ' @(specifics_filters) ('.$filter_query.')'; }else{ $this->filter_query = ''; } } return $this->filter_query; } /** * Retourne la liste des langues pour l'indexation * TODO Aller lire un paramètre proprement * @return array() */ public function get_available_languages() { //TODO A FAIRE PROPREMENT return array('','fr_FR','en_UK'); } }