2011-09-13 12:53:19 +00:00
< ? php
require_once 'framework/common/strings.php' ;
2011-11-22 14:28:50 +00:00
require_once realpath ( dirname ( __FILE__ )) . '/sphinxapi2.php' ;
2010-10-14 14:06:05 +00:00
function search2 ( $index , $findMe , $tabFiltres = array (), $deb = 0 , $nbRep = 20 , $max = 200 , $any = false ) {
$deb = $deb * 1 ;
$nbRep = $nbRep * 1 ;
$max = $max * 1 ;
debugLog ( 'I' , " Search Sphinx dans $index de $findMe ( $deb , $nbRep , $max ) avec " . implode ( ',' , $tabFiltres ), __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
$iDb = new WDB ();
//$any = false; // si false $index='xxx'
2011-11-22 14:31:04 +00:00
$cl = new SphinxClient2 ();
2011-09-13 12:53:19 +00:00
if ( $index == 'histo' ){
2011-08-29 14:32:39 +00:00
$cl -> SetServer ( SPHINX_HISTO_HOST , SPHINX_HISTO_PORT );
} else {
$cl -> SetServer ( SPHINX_HOST , SPHINX_PORT );
}
2010-10-14 14:06:05 +00:00
$cl -> SetConnectTimeout ( 1 );
$cl -> SetLimits ( $deb , $nbRep , $max ); //0, $nbRep*10);
//$cl->SetSortMode(SPH_SORT_ATTR_DESC, 'actif');//siege');
if ( substr ( $index , 0 , 4 ) == 'etab' )
$cl -> SetSortMode ( SPH_SORT_EXTENDED , 'rang DESC, actif DESC, siege DESC' ); //, nic DESC
//$cl->SetWeights ( array ( 100, 1 ) );
//$cl->SetMatchMode ( $any ? SPH_MATCH_ANY : SPH_MATCH_ALL );
$cl -> SetMatchMode ( SPH_MATCH_EXTENDED );
foreach ( $tabFiltres as $nomFiltre => $valFiltre )
$cl -> SetFilter ( $nomFiltre , array ( 0 => $valFiltre ));
$cl -> SetRankingMode ( SPH_RANK_PROXIMITY_BM25 );
//$cl->SetArrayResult ( true );
$res = $cl -> Query ( $findMe , $index );
//@sendMail('production@scores-decisions.com', 'ylenaour@scores-decisions.com', "Debug", print_r($res,true)) ;
if ( $res === false ) {
2011-12-14 11:09:40 +00:00
debugLog ( 'I' , " Search Sphinx : Pas de réponse pour $findMe avec " . implode ( ',' , $tabFiltres ) . ' (' . $cl -> GetLastError () . ')' , __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
2010-10-14 14:06:05 +00:00
return array ( 'results' => false ,
'nbRet' => 0 ,
'nbTot' => 0 ,
2011-06-10 10:08:21 +00:00
'error' => $cl -> GetLastError (),
'warning' => $cl -> GetLastWarning (),
2010-10-14 14:06:05 +00:00
'duration' => $res [ time ]); //return false;
2011-09-13 12:53:19 +00:00
} else { // Le moteur est op<6F> rationel
2010-10-14 14:06:05 +00:00
if ( $cl -> GetLastWarning () ) {
debugLog ( 'I' , " Search Sphinx : Warning pour $findMe - " . $cl -> GetLastWarning (), __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
print " WARNING: " . $cl -> GetLastWarning () . " \n \n " ;
}
/* print " Query ' $findMe ' retrieved $res[total] of $res[total_found] matches in $res[time] sec. \n " ;
print " Query stats : \n " ;
if ( is_array ( $res [ " words " ]) )
foreach ( $res [ " words " ] as $word => $info )
print " - ' $word ' found $info[hits] times in $info[docs] documents \n " ;
print " \n " ; */
debugLog ( 'I' , " 'Search Sphinx dans $index de $findMe (Filtre= " . implode ( ',' , $tabFiltres ) . " ), Deb= $deb , nbRep= $nbRep , max= $max , any= $any " , __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
// debugLog('I',"Search Sphinx dans $index ". print_r($tabFiltres, true));
$tabRet = array ();
2011-09-13 12:53:19 +00:00
if ( @ is_array ( $res [ 'matches' ])) { // il y a des donn<6E> es <20> renvoyer
if ( substr ( $index , 0 , 4 ) == 'etab' ) { // Recherche par <20> tab
2010-10-14 14:06:05 +00:00
foreach ( $res [ 'matches' ] as $doc => $docinfo ) { // On balaye chaque ligne
$listeEtab = $iDb -> select ( 'etablissements e' ,
" 'Etab' as Loc, e.id, e.source, e.source_id, e.triCode, e.autre_id, e.siren, e.nic, e.siege, " .
" e.raisonSociale, e.enseigne, e.sigle, e.adr_num, e.adr_btq, e.adr_typeVoie, e.adr_libVoie, " .
" e.adr_comp, e.adr_cp, e.adr_ville, e.tel, e.fax, e.cj, e.ape_etab, e.ape_entrep, " .
" CONCAT(e.siren, e.nic) as siret, e.actif " ,
" id= $doc LIMIT 0,1 " , true );
$etab = $listeEtab [ 0 ];
$tabRet [] = array ( 'Localisation' => $etab [ 'Loc' ],
'id' => $doc ,
'Pertinence' => $docinfo [ 'weight' ],
'Siret' => $etab [ 'siret' ],
'Siege' => $etab [ 'siege' ],
'Nom' => $etab [ 'raisonSociale' ],
'Sigle' => $etab [ 'sigle' ],
'Enseigne' => $etab [ 'enseigne' ],
'Adresse' => trim ( $etab [ 'adr_num' ] . ' ' .
$etab [ 'adr_btq' ] . ' ' .
$etab [ 'adr_typeVoie' ] . ' ' .
$etab [ 'adr_libVoie' ] . ' ' .
$etab [ 'adr_comp' ]),
'CP' => $etab [ 'adr_cp' ],
'Ville' => $etab [ 'adr_ville' ],
'Tel' => $etab [ 'tel' ],
'Fax' => $etab [ 'fax' ],
'FJ' => $etab [ 'cj' ],
'Siren' => $etab [ 'siren' ],
'Nic' => $etab [ 'nic' ],
'Actif' => $etab [ 'actif' ],
'NafEtab' => $etab [ 'ape_etab' ], // Etablissement
//'NafEtabLib'=>$etab['LibNafEt'],
'NafEnt' => $etab [ 'ape_entrep' ], // Entreprise
//'NafEntLib'=>$etab['LibNafEt'],
);
}
2011-09-13 12:53:19 +00:00
} elseif ( $index == 'histo' ) {
2010-10-14 14:06:05 +00:00
foreach ( $res [ 'matches' ] as $doc => $docinfo ) {
$listeEtab = $iDb -> select ( 'histobodacc.bodacc_ocr' ,
" 'Histo' as Loc, id, nomFichier, annee1, bod, texte " ,
" id= $doc " );
$etab = $listeEtab [ 0 ];
$tabRet [] = array ( 'Localisation' => $etab [ 'Loc' ],
'id' => $doc ,
'Pertinence' => $docinfo [ 'weight' ],
'Fichier' => $etab [ 'nomFichier' ],
'Annee' => $etab [ 'annee1' ],
'Code' => $etab [ 'bod' ],
'Texte' => $etab [ 'texte' ],
);
}
} else { // Recherche par dirigeant
$findMe4 = trim ( substr ( $findMe , 0 , 4 ));
foreach ( $res [ 'matches' ] as $doc => $docinfo ) { // On balaye chaque ligne dirigeant
$listeEtab = $iDb -> select ( 'rncs_dirigeants d, etablissements e' ,
" 'Etab' as Loc, e.id, e.source, e.source_id, e.triCode, e.autre_id, e.siren, e.nic, e.siege, " .
" e.raisonSociale, e.enseigne, e.sigle, e.adr_num, e.adr_btq, e.adr_typeVoie, e.adr_libVoie, " .
" e.adr_comp, e.adr_cp, e.adr_ville, e.tel, e.fax, e.cj, e.ape_etab, e.ape_entrep, " .
" CONCAT(e.siren, e.nic) as siret, /*d.rs,*/ d.nom, d.prenom, /*d.nomUsage,*/ d.naissance_date, d.naissance_lieu, " .
" d.fonction_lib, /*d.depart,*/ e.actif " ,
" d.id= $doc AND d.siren=e.siren AND e.siege=1 ORDER BY e.actif DESC LIMIT 0,1 " , true );
//die(print_r($listeEtab));
$etab = $listeEtab [ 0 ];
$tabRet [] = array ( 'Localisation' => $etab [ 'Loc' ],
'id' => $doc ,
'Pertinence' => $docinfo [ 'weight' ],
'Siret' => $etab [ 'siret' ],
'Siege' => $etab [ 'siege' ],
'Nom' => $etab [ 'raisonSociale' ],
'Sigle' => $etab [ 'sigle' ],
'Enseigne' => $etab [ 'enseigne' ],
'Adresse' => trim ( $etab [ 'adr_num' ] . ' ' .
$etab [ 'adr_btq' ] . ' ' .
$etab [ 'adr_typeVoie' ] . ' ' .
$etab [ 'adr_libVoie' ] . ' ' .
$etab [ 'adr_comp' ]),
'CP' => $etab [ 'adr_cp' ],
'Ville' => $etab [ 'adr_ville' ],
'Tel' => $etab [ 'tel' ],
'Fax' => $etab [ 'fax' ],
'FJ' => $etab [ 'cj' ],
'Siren' => $etab [ 'siren' ],
'Nic' => $etab [ 'nic' ],
'Actif' => $etab [ 'actif' ],
'NafEtab' => $etab [ 'ape_etab' ], // Etablissement
'NafEnt' => $etab [ 'ape_entrep' ], // Entreprise
'rs' => '' , //$etab['rs'],
'nomD' => $etab [ 'nom' ],
'prenom' => $etab [ 'prenom' ],
'nomUsage' => '' , //$etab['nomUsage'],
'dateEffet' => '' , //$etab['dateEffet'],
'fonction' => $etab [ 'fonction_lib' ],
'depart' => 0 , //$etab['depart'],
);
}
}
}
}
debugLog ( 'I' , 'Search Sphinx : Retourne ' . $res [ total ] . '/' . $res [ total_found ] . ' en ' . $res [ time ] . 'secondes' , __LINE__ , __FILE__ , __FUNCTION__ , __CLASS__ );
return array ( 'results' => $tabRet ,
'nbRet' => $res [ total ],
'nbTot' => $res [ total_found ],
'duration' => $res [ time ],
'words' => $res [ 'words' ],
);
}
2011-06-10 10:08:21 +00:00
/*
< nafen > 7020 z <
*/
function comptage ( $findMe , $tabFiltres = array (), $deb = 0 , $nbRep = 20 , $max = 1000 , $any = false , $fichierCsv = false ) {
$deb = $deb * 1 ;
$nbRep = $nbRep * 1 ;
$max = $max * 1 ;
$index = 'comptage' ;
$iDb = new WDB ();
/*
$cl = new SphinxClient ();
$cl -> SetServer ( $host , $port );
$cl -> SetWeights ( array ( 100 , 1 ) );
$cl -> SetMatchMode ( $mode );
if ( count ( $filtervals ) ) $cl -> SetFilter ( $filter , $filtervals );
if ( $groupby ) $cl -> SetGroupBy ( $groupby , SPH_GROUPBY_ATTR , $groupsort );
if ( $sortby ) $cl -> SetSortMode ( SPH_SORT_EXTENDED , $sortby );
if ( $sortexpr ) $cl -> SetSortMode ( SPH_SORT_EXPR , $sortexpr );
if ( $distinct ) $cl -> SetGroupDistinct ( $distinct );
if ( $limit ) $cl -> SetLimits ( 0 , $limit , ( $limit > 1000 ) ? $limit : 1000 );
$res = $cl -> Query ( $q , $index );
*/
$cl = new SphinxClient ();
$cl -> SetServer ( '192.168.3.24' , 3312 );
$cl -> SetConnectTimeout ( 1 );
$cl -> SetLimits ( $deb , $nbRep , $max );
// if (substr($index,0,4)=='etab')
// $cl->SetSortMode(SPH_SORT_EXTENDED, 'rang DESC, actif DESC, siege DESC');//, nic DESC
//$cl->SetWeights ( array ( 100, 1 ) );
//$cl->SetMatchMode ( $any ? SPH_MATCH_ANY : SPH_MATCH_ALL );
$cl -> SetMatchMode ( SPH_MATCH_EXTENDED );
foreach ( $tabFiltres as $nomFiltre => $valFiltre )
$cl -> SetFilter ( $nomFiltre , $valFiltre );
/* 8.4 . 3. SetFilterRange
Prototype : function SetFilterRange ( $attribute , $min , $max , $exclude = false )
Adds new integer range filter . */
$cl -> SetRankingMode ( SPH_RANK_PROXIMITY_BM25 );
//$cl->SetArrayResult ( true );
$res = $cl -> Query ( $findMe , $index );
if ( $res === false ) {
2011-08-29 14:32:39 +00:00
// debugLog('I',"Search Sphinx : Pas de r<> ponse pour $findMe avec ".implode(',',$tabFiltres).' ('.$cl->GetLastError() .')',__LINE__,__FILE__, __FUNCTION__, __CLASS__);
2011-06-10 10:08:21 +00:00
return array ( 'results' => false ,
'nbRet' => 0 ,
'nbTot' => 0 ,
'error' => $cl -> GetLastError (),
'warning' => $cl -> GetLastWarning (),
'durationCpt' => $res [ time ],
'durationExp' => 0 ); //return false;);//return false;
2011-08-29 14:32:39 +00:00
} else { // Le moteur est op<6F> rationel
2011-06-10 10:08:21 +00:00
$tdeb = microtime ( 1 );
if ( $cl -> GetLastWarning () ) {
//debugLog('I',"Search Sphinx : Warning pour $findMe - ".$cl->GetLastWarning(),__LINE__,__FILE__, __FUNCTION__, __CLASS__);
print " WARNING: " . $cl -> GetLastWarning () . " \n \n " ;
}
$tabRet = array ();
2011-08-29 14:32:39 +00:00
if ( @ is_array ( $res [ 'matches' ])) { // il y a des donn<6E> es <20> renvoyer
2011-06-10 10:08:21 +00:00
$tabIdDoc = array ();
foreach ( $res [ 'matches' ] as $doc => $docinfo ) //{ // On balaye chaque ligne
$tabIdDoc [] = $doc ;
$strDoc = '(' . implode ( ',' , $tabIdDoc ) . ')' ;
$tabTmp = $iDb -> select ( 'etablissements_act' ,
'id,ape_etab,ape_entrep,source,siren,nic,actif,siege,adr_cp,adr_dep,adr_com,tel,fax,cj,capital,age_entrep,age_etab,tca,tcaexp,teff_entrep,teff_etab,rang,dateCrea_etab,eff_entrep,eff_etab,nbEtab,bilType,bilAnnee,bilTca,bilCA,bilCAexp,bilRN' ,
" id IN $strDoc " , false , MYSQL_ASSOC );
if ( $fichierCsv ) {
$fp = fopen ( $fichierCsv , 'w' );
foreach ( $tabTmp as $iTmp => $tmp ) {
$tabRet [] = $tmp ;
fwrite ( $fp , implode ( " \t " , $tmp ) . EOL );
}
fclose ( $fp );
}
}
}
return array ( 'results' => $tabRet ,
'nbRet' => $res [ total ],
'nbTot' => $res [ total_found ],
'durationCpt' => $res [ time ],
'durationExp' => round ( microtime ( 1 ) - $tdeb , 3 ),
'error' => $cl -> GetLastError (),
'warning' => $cl -> GetLastWarning (),
'words' => $res [ 'words' ],
);
}
2010-10-14 14:06:05 +00:00
?>