Skip to content

Instantly share code, notes, and snippets.

@basdenooijer
Created June 21, 2011 13:49
Show Gist options
  • Save basdenooijer/1037886 to your computer and use it in GitHub Desktop.
Save basdenooijer/1037886 to your computer and use it in GitHub Desktop.
Get started
<html><head><title>Add docs</title></head><body>
<?php
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client();
// create a new document for the data
$doc1 = new Solarium_Document_ReadWrite();
$doc1->id = 123;
$doc1->name = 'testdoc-1';
$doc1->price = 364;
// and a second one
$doc2 = new Solarium_Document_ReadWrite();
$doc2->id = 124;
$doc2->name = 'testdoc-2';
$doc2->price = 340;
// get an update query instance
$update = $client->createUpdate();
// add the documents and a commit command to the update query
$update->addDocuments(array($doc1, $doc2));
$update->addCommit();
// this executes the query and returns the result
$result = $client->update($update);
echo '<b>Update query executed<b><br/>';
echo 'Query status: ' . $result->getStatus(). '<br/>';
echo 'Query time: ' . $result->getQueryTime();
?>
</body></html>
<html><head><title>Basic select</title></head><body>
<?php
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client();
// get a select query instance
$query = $client->createSelect();
// override the default row limit of 10 by setting rows to 30
$query->setRows(30);
// this executes the query with default settings and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table>';
}
?>
</body></html>
<html><head><title>Delete docs</title></head><body>
<?php
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client();
// create a new document for the data
$doc1 = new Solarium_Document_ReadWrite();
$doc1->id = 123;
$doc1->name = 'testdoc-1';
$doc1->price = 364;
// and a second one
$doc2 = new Solarium_Document_ReadWrite();
$doc2->id = 124;
$doc2->name = 'testdoc-2';
$doc2->price = 340;
// get an update query instance
$update = $client->createUpdate();
// get an update query instance
$update = $client->createUpdate();
// add the delete query and a commit command to the update query
$update->addDeleteQuery('name:testdoc*');
$update->addCommit();
// this executes the query and returns the result
$result = $client->update($update);
echo '<b>Update query executed<b><br/>';
echo 'Query status: ' . $result->getStatus(). '<br/>';
echo 'Query time: ' . $result->getQueryTime();
?>
</body></html>
<?php
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
// check solarium version available
echo 'Solarium library version: ' . Solarium_Version::VERSION . ' - ';
// create a client instance
$client = new Solarium_Client();
// create a ping query
$ping = $client->createPing();
// execute the ping query
try{
$client->ping($ping);
echo 'Ping query succesful';
}catch(Solarium_Exception $e){
echo 'Ping query failed';
}
<html><head><title>Select query with options</title></head><body>
<?php
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client();
// get a select query instance
$query = $client->createSelect();
// set start and rows param (comparable to SQL limit) using fluent interface
$query->setStart(1)->setRows(5);
// set fields to fetch (this overrides the default setting 'all fields')
$query->setFields(array('id','name','features','price'));
// set a query string (without a field, so the default search field will be used)
$query->setQuery('memory');
// create a filterquery and set options using the fluent interface
$fq = $query->createFilterQuery('maxprice')->addTag('maxprice')->setQuery('price:[1 TO 400]');
// add a facet on a field
$facetSet = $query->getFacetSet();
$facet = $facetSet->createFacetField('stock')->setField('inStock')->addExclude('maxprice');
// enable the highlighting component
$hl = $query->getHighlighting();
$hl->setFields('features');
$hl->setSimplePrefix('<b>');
$hl->setSimplePostfix('</b>');
// this executes the query with default settings and returns the result
$resultset = $client->select($query);
$highlighting = $resultset->getHighlighting();
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// display facet counts
echo '<hr/>Facet counts for field "inStock":<br/>';
$facet = $resultset->getFacetSet()->getFacet('stock');
foreach($facet as $value => $count) {
echo $value . ' [' . $count . ']<br/>';
}
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);
echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}
echo '</table><br/><b>Highlighting results:</b><br/>';
// highlighting results can be fetched by document id (the field defined as uniquekey in this schema)
$highlightedDoc = $highlighting->getResult($document->id);
if($highlightedDoc){
foreach($highlightedDoc as $field => $highlight) {
echo implode(' (...) ', $highlight) . '<br/>';
}
}
}
?>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment