323 lines
14 KiB
PHP
323 lines
14 KiB
PHP
<?
|
|
|
|
include_once(FWK_PATH.'common/strings.php');
|
|
include_once(FWK_PATH.'sphinx/sphinxapi2.php');
|
|
|
|
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();
|
|
/*
|
|
$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 );
|
|
*/
|
|
|
|
//$any = false; // si false $index='xxx'
|
|
$cl = new SphinxClient ();
|
|
$cl->SetServer (SPHINX_HOST, SPHINX_PORT);
|
|
$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) {
|
|
debugLog('I',"Search Sphinx : Pas de réponse pour $findMe avec ".implode(',',$tabFiltres).' ('.$cl->GetLastError() .')',__LINE__,__FILE__, __FUNCTION__, __CLASS__);
|
|
return array( 'results'=>false,
|
|
'nbRet'=>0,
|
|
'nbTot'=>0,
|
|
'error'=>$cl->GetLastError(),
|
|
'warning'=>$cl->GetLastWarning(),
|
|
'duration'=>$res[time]);//return false;
|
|
|
|
} else { // Le moteur est opérationel
|
|
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();
|
|
if ( @is_array($res['matches'])) { // il y a des données à renvoyer
|
|
if (substr($index,0,4)=='etab') { // Recherche par étab
|
|
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'],
|
|
);
|
|
}
|
|
} elseif ($index=='idx_histo') {
|
|
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('bodacc_dirigeants d, bodacc_detail b, 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.dateEffet, d.fonction, d.depart, e.actif",
|
|
"d.id=$doc AND d.id=b.id AND b.siren=e.siren AND (d.nom LIKE '%$findMe4%' OR d.prenom LIKE '%$findMe4%' OR d.nomUsage LIKE '%$findMe4%' OR d.rs LIKE '%$findMe4%') LIMIT 0,1", true);
|
|
if (count($listeEtab)==0)
|
|
$listeEtab=$iDb->select('bodacc_dirigeants d, bodacc_detail b, 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.dateEffet, d.fonction, d.depart, e.actif",
|
|
"d.id=$doc AND d.id=b.id AND b.siren=e.siren 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
|
|
'NafEnt'=>$etab['ape_entrep'], // Entreprise
|
|
'rs'=>$etab['rs'],
|
|
'nomD'=>$etab['nom'],
|
|
'prenom'=>$etab['prenom'],
|
|
'nomUsage'=>$etab['nomUsage'],
|
|
'dateEffet'=>$etab['dateEffet'],
|
|
'fonction'=>$etab['fonction'],
|
|
'depart'=>$etab['depart'],
|
|
);
|
|
}
|
|
*/
|
|
//print_r($tabRet);
|
|
//die();
|
|
$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'],
|
|
);
|
|
}
|
|
|
|
/*
|
|
<nafen>7020z<
|
|
|
|
*/
|
|
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) {
|
|
// debugLog('I',"Search Sphinx : Pas de réponse pour $findMe avec ".implode(',',$tabFiltres).' ('.$cl->GetLastError() .')',__LINE__,__FILE__, __FUNCTION__, __CLASS__);
|
|
return array( 'results'=>false,
|
|
'nbRet'=>0,
|
|
'nbTot'=>0,
|
|
'error'=>$cl->GetLastError(),
|
|
'warning'=>$cl->GetLastWarning(),
|
|
'durationCpt'=>$res[time],
|
|
'durationExp'=>0);//return false;);//return false;
|
|
|
|
} else { // Le moteur est opérationel
|
|
$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();
|
|
if ( @is_array($res['matches'])) { // il y a des données à renvoyer
|
|
$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'],
|
|
);
|
|
}
|
|
|
|
?>
|