issue #0000533 : Méthode rechercheHisto fonctionnelle

This commit is contained in:
Michael RICOIS 2011-03-07 17:16:53 +00:00
parent 8ef44ca34d
commit eae07a7de8
2 changed files with 168 additions and 34 deletions

View File

@ -0,0 +1,131 @@
<?php
require_once 'framework/common/strings.php';
require_once realpath(dirname(__FILE__)).'/sphinxapi.php';
class SphinxCriteres
{
var $findMe = '';
var $tabFiltres = array();
var $position = 0;
var $nbRep = 20;
var $max = 200;
var $any = false;
}
class SphinxSearch
{
/**
* Effectue une recherche en interrogeant le moteur Sphinx
*
* @param string $index
* @param SphinxCriteres $criteres Critères de recherche
* @return array $return Tableau des résultats
*/
public function search($name, $criteres)
{
debugLog('I',"Search Sphinx dans $name de ".$criteres->findMe." (".$criteres->deb.", ".$criteres->nbRep.", ".$criteres->max.") avec ".implode(',',$criteres->tabFiltres),__LINE__,__FILE__, __FUNCTION__, __CLASS__);
$cl = new SphinxClient();
switch (strtolower($name)) {
case 'entrep':
break;
case 'dir':
break;
case 'histo':
$cl->SetServer (SPHINX_HISTO_HOST, SPHINX_HISTO_PORT);
$cl->SetConnectTimeout ( 1 );
$cl->SetLimits ($criteres->position, $criteres->nbRep, $criteres->max);
$cl->SetMatchMode (SPH_MATCH_EXTENDED);
foreach ($criteres->tabFiltres as $nomFiltre => $valFiltre) {
$cl->SetFilter($nomFiltre, array(0=>$valFiltre));
}
$cl->SetRankingMode ( SPH_RANK_PROXIMITY_BM25 );
$res = $cl->Query ( $criteres->findMe, 'histo' );
if ($res===false) {
//Erreur
debugLog('I',"Search Sphinx : Pas de réponse pour ".$criteres->findMe." avec ".implode(',',$criteres->tabFiltres).' ('.$cl->GetLastError() .')',__LINE__,__FILE__, __FUNCTION__, __CLASS__);
return array(
'results' => false,
'nbRet' => 0,
'nbTot' => 0,
'duration' => 0);
} else {
if ( $cl->GetLastWarning() ) {
debugLog('I',"Search Sphinx : Warning pour $criteres->findMe - ".$cl->GetLastWarning(),__LINE__,__FILE__, __FUNCTION__, __CLASS__);
print "WARNING: " . $cl->GetLastWarning() . "\n\n";
}
debugLog('I',"'Search Sphinx dans $name de $criteres->findMe (Filtre=".implode(',',$criteres->tabFiltres)."), Deb=".$criteres->position.", nbRep=".$criteres->nbRep.", max=".$criteres->max.", any=".$criteres->any,__LINE__,__FILE__, __FUNCTION__, __CLASS__);
$tabRet = array();
if ( is_array($res['matches'])) {
$iDb = new WDB('histobodacc');
$iDb->query("SET NAMES 'latin1';");
foreach ( $res['matches'] as $doc => $docinfo ) {
$listeEtab = $iDb->select(
'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'],
);
}
}
}
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'],
);
break;
}
}
/**
* Méthode magique __call() permettant d'appeller une méthode virtuelle
* du type searchByEnt(), searchByDir() ou searchByHisto()...
*
* @param string $method Nom de la méthode virtuelle appelée
* @param array $args Tableau des critères de recherche
* @return array|null $return Tableau des résultats ou NULL
*/
public function __call($method, $args)
{
debugLog('I',"Appel de la méthode '$method' ". implode(', ', $args),__LINE__,__FILE__, __FUNCTION__, __CLASS__);
if(preg_match('#^searchBy#i',$method))
{
$name = str_replace('searchBy','',$method);
$criteres = array(
0 => 'findMe',
1 => 'tabFiltres',
2 => 'position',
3 => 'nbRep',
4 => 'max',
5 => 'any',
);
$searchArgs = new stdClass();
$i = 0;
foreach($args as $argument) {
$searchArgs->{$criteres[$i]} = $argument;
$i++;
}
return $this->search($name, $searchArgs);
}
return null;
}
}

View File

@ -2822,22 +2822,25 @@ class Interne extends WsScore
//Initialisation
$error = new ErrorType();
if (empty($annee)) $annee = '';
if (empty($typeBod)) $typeBod = '';
if (empty($deb)) $deb = 0;
if (empty($nbRep)) $nbRep = 20;
if (empty($maxRep)) $maxRep = 200;
if (empty($annee)) $pertinence = false;
if (empty($annee)) $annee = '';
if (empty($typeBod)) $typeBod = '';
if (empty($deb)) $deb = 0;
if (empty($nbRep)) $nbRep = 20;
if (empty($maxRep)) $maxRep = 200;
if (empty($annee)) $pertinence = false;
require_once 'framework/sphinx/recherche2.php';
require_once 'Metier/sphinx/sphinxsearch.php';
debugLog('I',"rechercheHisto de $recherche ($annee) (Max Rep=$nbRep)",__LINE__,__FILE__, __FUNCTION__, __CLASS__);
$tabFiltresAnnee = array();
if ($annee<>'' && $annee*1>=1953 && $annee*1<=date('Y')*1)
$tabFiltresAnnee = array('annee1'=>$annee);
$ret = search2('idx_histo', "$recherche", $tabFiltresAnnee, $deb, $nbRep, $maxRep, $pertinence);
$sphinxsearch = new SphinxSearch();
$ret = $sphinxsearch->searchByHisto($recherche, $tabFiltresAnnee, $deb, $nbRep, $maxRep, $pertinence);
$liste = $ret['results'];
$nbTot = $ret['nbTot'];
$duree = $ret['duration'];
@ -2845,33 +2848,33 @@ class Interne extends WsScore
$tabRet = array();
$k=0;
foreach ($liste as $n=>$etab)
{
$texte=/*preg_replace('/ +/', ' ', */$etab['Texte'];//);
$posMin=100000;
$hitMin=1000000;
foreach ($tabMots as $mot=>$tabMot){
if ($tabMot['hits']<$hitMin) {
$hitMin=$tabMot['hits'];
$motSignificatif=$mot;
if(count($liste)>0) {
foreach ($liste as $n=>$etab)
{
$texte = $etab['Texte'];
$posMin=100000;
$hitMin=1000000;
foreach ($tabMots as $mot=>$tabMot){
if ($tabMot['hits']<$hitMin) {
$hitMin=$tabMot['hits'];
$motSignificatif=$mot;
}
}
$posMin = stripos($texte, ''.$motSignificatif);
if ($posMin<150) $posMin=150;
$texte2 = substr($texte, $posMin-150, 250);
$reponse = new RechercheHistoReponses();
$reponse->id = $etab['id'];
$reponse->Pertinence = $etab['Pertinence'];
$reponse->Fichier = strtr($etab['Fichier'],
array( '.txt'=>'.pdf', '/mnt/bodacc/' => 'http://tville.scores-decisions.com/bodacc/')
);
$reponse->Annee = $etab['Annee'];
$reponse->Code = $etab['Code'];
$reponse->Texte = utf8_encode($texte2);
$tabRet[] = $reponse;
$k++;
}
$posMin = stripos($texte, ''.$motSignificatif);
//@sendMail('production@scores-decisions.com', 'ylenaour@scores-decisions.com', "Debug", print_r($tabMots,true).EOL.$motSignificatif.' trouvé en position '.$posMin) ;
if ($posMin<150) $posMin=150;
$texte2=substr($texte, $posMin-150, 250);
$reponse = new RechercheHistoReponses();
$reponse->id = $etab['id'];
$reponse->Pertinence = $etab['Pertinence'];
$reponse->Fichier = strtr($etab['Fichier'], array('.txt'=>'.pdf', '/mnt/bodacc/'=>'http://tville.scores-decisions.com/bodacc/'));
$reponse->Annee = $etab['Annee'];
$reponse->Code = $etab['Code'];
$reponse->Texte = preg_replace('/^[A-Za-z0-9\sàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝŸ]/','',$texte2);
$tabRet[] = $reponse;
$k++;
}
$criteres = new RechercheHistoCriteres();