Exemples
Exemples d'utilisation de l'extension Apache Solr en PHP.
Exemple #1 Contenu du fichier BootStrap
<?php /* Nom de domaine du serveur Solr */ define('SOLR_SERVER_HOSTNAME', 'solr.example.com'); /* Si l'on doit exécuter en mode sécurisé ou non */ define('SOLR_SECURE', true); /* Port HTTP de connexion */ define('SOLR_SERVER_PORT', ((SOLR_SECURE) ? 8443 : 8983)); /* Nom d'utilisateur pour l'authentification HTTP Basic */ define('SOLR_SERVER_USERNAME', 'admin'); /* Mot de passe pour l'authentification HTTP Basic */ define('SOLR_SERVER_PASSWORD', 'changeit'); /* Délai maximal de connexion HTTP */ /* C'estla durée maximale en secondes autorisée pour l'opération de transfert de données http. La valeur par défaut est 30 secondes */ define('SOLR_SERVER_TIMEOUT', 10); /* Nom du fichier de la clé privée formattée PEM + du certificat privé (concaténés dans cet ordre) */ define('SOLR_SSL_CERT', 'certs/combo.pem'); /* Nom du fichier du certificat privé formatté PEM seulement */ define('SOLR_SSL_CERT_ONLY', 'certs/solr.crt'); /* Nom du fichier de la clé privée formattée PEM */ define('SOLR_SSL_KEY', 'certs/solr.key'); /* Mot de passe pour le fichier de la clé privée formattée PEM */ define('SOLR_SSL_KEYPASSWORD', 'StrongAndSecurePassword'); /* Nom du fichier contenant un ou plusieurs certificats CA pour l'authentification */ define('SOLR_SSL_CAINFO', 'certs/cacert.crt'); /* Nom du dossier contenant les certificats CA pour l'authentification */ define('SOLR_SSL_CAPATH', 'certs/'); ?>
Exemple #2 Ajout d'un document à l'index
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$updateResponse = $client->addDocument($doc);
print_r($updateResponse->getResponse());
?>
<?php
include "bootstrap.php";
$doc = new SolrDocument();
$second_doc = new SolrDocument();
$doc->addField('id', 1123);
$doc->features = "PHP Client Side";
$doc->features = "Fast development cycles";
$doc['cat'] = 'Software';
$doc['cat'] = 'Custom Search';
$doc->cat = 'Information Technology';
$second_doc->addField('cat', 'Lucene Search');
$second_doc->merge($doc, true);
print_r($second_doc->toArray());
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('lucene');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$response = $query_response->getResponse();
print_r($response);
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('lucene');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);
$response = $query_response->getResponse();
print_r($response);
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setTerms(true);
$query->setTermsField('cat');
$updateResponse = $client->query($query);
print_r($updateResponse->getResponse());
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setTerms(true);
/* Return only terms starting with $prefix */
$prefix = 'c';
$query->setTermsField('cat')->setTermsPrefix($prefix);
$updateResponse = $client->query($query);
print_r($updateResponse->getResponse());
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setTerms(true);
/* Return only terms starting with $prefix */
$prefix = 'c';
/* Return only terms with a frequency of 2 or greater */
$min_frequency = 2;
$query->setTermsField('cat')->setTermsPrefix($prefix)->setTermsMinCount($min_frequency);
$updateResponse = $client->query($query);
print_r($updateResponse->getResponse());
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery('*:*');
$query->setFacet(true);
$query->addFacetField('cat')->addFacetField('name')->setFacetMinCount(2);
$updateResponse = $client->query($query);
$response_array = $updateResponse->getResponse();
$facet_data = $response_array->facet_counts->facet_fields;
print_r($facet_data);
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery('*:*');
$query->setFacet(true);
$query->addFacetField('cat')->addFacetField('name')->setFacetMinCount(2)->setFacetMinCount(4, 'name');
$updateResponse = $client->query($query);
$response_array = $updateResponse->getResponse();
$facet_data = $response_array->facet_counts->facet_fields;
print_r($facet_data);
?>
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
'timeout' => SOLR_SERVER_TIMEOUT,
'secure' => SOLR_SECURE,
'ssl_cert' => SOLR_SSL_CERT_ONLY,
'ssl_key' => SOLR_SSL_KEY,
'ssl_keypassword' => SOLR_SSL_KEYPASSWORD,
'ssl_cainfo' => SOLR_SSL_CAINFO,
);
$client = new SolrClient($options);
$query = new SolrQuery('*:*');
$query->setFacet(true);
$query->addFacetField('cat')->addFacetField('name')->setFacetMinCount(2)->setFacetMinCount(4, 'name');
$updateResponse = $client->query($query);
$response_array = $updateResponse->getResponse();
$facet_data = $response_array->facet_counts->facet_fields;
print_r($facet_data);
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
SolrObject Object ( [cat] => SolrObject Object ( [electronics] => 14 [memory] => 3 [Lucene] => 2 [Software] => 2 [card] => 2 [connector] => 2 [drive] => 2 [graphics] => 2 [hard] => 2 [monitor] => 2 [search] => 2 [software] => 2 ) [name] => SolrObject Object ( [gb] => 6 ) )