Voici un script d'indexation en php qui fait appel à du java

 
<?php 
date_default_timezone_set('Europe/Brussels');
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
$config = new Zend_Config(
    array(
        'database' => array(
            'adapter' => 'PDO_Mysql',
            'params' => array(
                'host'   => '127.0.0.1',
                'dbname' => 'madb',
                'username' => 'huser',
                'password' => 'hpd',
            )
        )
    )
);
 
require_once ("/usr/include/php5/java/Java.inc");
java_require("lucene-core.jar");
 
try {
    $db = Zend_Db::factory($config->database);    
    $db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
    // probablement mauvais identifiants,    
    echo $e->getMessage();
    // ou alors le SGBD n'est pas joignable
} catch (Zend_Exception $e) {
    // probablement que factory() n'a pas réussi à charger
    // la classe de l'adaptateur demandé
    echo $e->getMessage();
}
 
$sql = 'SELECT * FROM courrier_documents ORDER BY addate ';
$results = $db->fetchAll($sql);
 
try {
	echo "indexing ... ";
 
	$tmp = "/var/lucene/courrier-index";
	$analyzer = new java("org.apache.lucene.analysis.standard.StandardAnalyzer");
	$writer = new java("org.apache.lucene.index.IndexWriter", $tmp, $analyzer, true);
 
	$i =0;
	foreach($results as $result)
	{
		$nomFichier  = $result["nom"];
		$description = $result["desc"];
		$service1    = $result["service"];
		$service2    = $result["service2"];
		$expediteur  = $result["exp"];
		$recom 	     = $result["recom"];
		$num         = $result["num"];
		$Taddate     = $result["addate"];
		$idDoc       = $result["id"];
 
		list($addateTemp, $heure) = explode(" ",$Taddate);
 
		list($year, $month, $day) = explode("-",$addateTemp);
 
		$addate = $year.$month.$day;
 
		$docContent = $description." ".$expediteur." ".$num;
		$i++;
 
		$doc = new java("org.apache.lucene.document.Document");
    	$doc->add(new java("org.apache.lucene.document.Field",
      		"idDoc", 
       		$idDoc, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->NO));
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"nomFichier", 
       		$nomFichier, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->NO));
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"recom", 
       		$recom, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->NO));
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"dateaffichage", 
       		$addateTemp, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->UN_TOKENIZED));
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"service1", 
       		$service1, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));    	
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"service2", 
       		$service2, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));       	
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"expediteur", 
       		$expediteur, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));    	
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"num", 
       		$num, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->UN_TOKENIZED));     
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"addate", 
       		$addate, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->UN_TOKENIZED));    	
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"description", 
       		$description, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"contents", 
       		$docContent, 
       		java('org.apache.lucene.document.Field$Store')->NO, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));
 
		/*
	 	 * information sur les destinataires du document
		 */
 
       	$suite = true;	
 
       	if($suite) :
		$sqlDesti = "SELECT * FROM `courrier_recipients` WHERE `iddoc` = '$idDoc'";
 
		$destinataires = $db->fetchAll($sqlDesti);
 
		$listDestiPrincipale = "";
		$listDestiSecondaire = "";
 
		foreach($destinataires as $destinataire)
		{
			if($destinataire["principale"] == 1)
			{
				$listDestiPrincipale .= $destinataire["uid"]." ";
			}
			else
			{
				$listDestiSecondaire .= $destinataire["uid"]." ";
			}				
		}		
 
		$doc->add(new java("org.apache.lucene.document.Field",
      		"destiPrinc", 
       		$listDestiPrincipale, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));   
 
       	$doc->add(new java("org.apache.lucene.document.Field",
      		"destiSecond", 
       		$listDestiSecondaire, 
       		java('org.apache.lucene.document.Field$Store')->YES, 
       		java('org.apache.lucene.document.Field$Index')->TOKENIZED));	       		
 
       	endif;
       	$writer->addDocument($doc);	
 
    	echo "$i doc ajoute \n";	
	}
 
  $writer->optimize();
  $writer->close();
  echo "done\n"; 
 
} catch (JavaException $e) {
  echo "Exception occured: "; echo $e; echo "<br>\n";
}

Discussion

Entrer votre commentaire
Si vous ne pouvez déchiffrer le code, téléchargez ce fichier .wav pour l'entendre.
 
 
java/lucene_et_php.txt · Dernière modification: 2009/05/29 16:13 par admin
 
Sauf mention contraire, le contenu de ce wiki est placé sous la licence suivante:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki