Commit 134ee620 authored by Bas de Nooijer's avatar Bas de Nooijer

- added examples

parent 32911c88
config.php
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// check solarium version available
echo 'Solarium library version: ' . Solarium_Version::VERSION . ' - ';
// create a client instance
$client = new Solarium_Client($config);
// 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';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// this executes the query 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>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
if ($_POST) {
// if data is posted add it to solr
// create a new document for the data
// please note that any type of validation is missing in this example to keep it simple!
$doc = new Solarium_Document_ReadWrite();
$doc->id = $_POST['id'];
$doc->name = $_POST['name'];
$doc->price = $_POST['price'];
// create a client instance
$client = new Solarium_Client($config);
// get an update query instance
$update = $client->createUpdate();
// add the document and a commit command to the update query
$update->addDocument($doc);
$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();
} else {
// if no data is posted show a form
?>
<form method="POST">
Id: <input type="text" name="id"/> <br/>
Name: <input type="text" name="name"/> <br/>
Price: <input type="text" name="price"/> <br/>
<input type="submit" value="Add"/>
</form>
<?php
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// set a query (all prices starting from 12)
$query->setQuery('price:[12 TO *]');
// set start and rows param (comparable to SQL limit) using fluent interface
$query->setStart(2)->setRows(20);
// set fields to fetch (this overrides the default setting 'all fields')
$query->setFields(array('id','name','price'));
// sort the results by price ascending
$query->addSort('price', Solarium_Query_Select::SORT_ASC);
// this executes the query 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>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// this is the custom result document class
class myDoc extends Solarium_Document_ReadOnly{
public function getSpecialPrice()
{
return round(($this->price * .95), 2);
}
}
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// set the custom resultclass
$query->setDocumentClass('myDoc');
// this executes the query 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>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
// this method is added by the custom class
echo '<tr><th>offer price</th><td>' . $document->getSpecialPrice() . '</td></tr>';
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// create a filterquery
$fq = $query->createFilterQuery();
$fq->setKey('maxprice');
$fq->setQuery('price:[1 TO 300]');
// add it to the query
$query->addFilterQuery($fq);
// this executes the query 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>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// get the facetset component
$facetSet = $query->getFacetSet();
// create a facet field instance and set options
$facet = $facetSet->createFacetField();
$facet->setKey('stock');
$facet->setField('inStock');
// add the facet instance to the facetset
$facetSet->addFacet($facet);
// this executes the query and returns the result
$resultset = $client->select($query);
// 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>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// get the facetset component
$facetSet = $query->getFacetSet();
// create a facet query instance and set options
$facet = $facetSet->createFacetQuery();
$facet->setKey('stock');
$facet->setQuery('inStock: true');
// add the facet instance to the facetset
$facetSet->addFacet($facet);
// this executes the query and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// display facet query count
$count = $resultset->getFacetSet()->getFacet('stock')->getValue();
echo '<hr/>Facet query count : ' . $count;
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// get the facetset component
$facetSet = $query->getFacetSet();
// create a facet query instance and set options
$facet = $facetSet->createFacetMultiQuery();
$facet->setKey('stock');
$facet->createQuery('stock_pricecat1', 'inStock:true AND price:[1 TO 300]');
$facet->createQuery('nostock_pricecat1', 'inStock:false AND price:[1 TO 300]');
$facet->createQuery('stock_pricecat2', 'inStock:true AND price:[300 TO *]');
$facet->createQuery('nostock_pricecat2', 'inStock:false AND price:[300 TO *]');
// add the facet instance to the facetset
$facetSet->addFacet($facet);
// this executes the query and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// display facet counts
echo '<hr/>Multiquery facet counts:<br/>';
$facet = $resultset->getFacetSet()->getFacet('stock');
foreach($facet as $key => $count) {
echo $key . ' [' . $count . ']<br/>';
}
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// get the facetset component
$facetSet = $query->getFacetSet();
// create a facet field instance and set options
$facet = $facetSet->createFacetRange();
$facet->setKey('priceranges');
$facet->setField('price');
$facet->setStart(1);
$facet->setGap(100);
$facet->setEnd(1000);
// add the facet instance to the facetset
$facetSet->addFacet($facet);
// this executes the query and returns the result
$resultset = $client->select($query);
// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();
// display facet counts
echo '<hr/>Facet ranges:<br/>';
$facet = $resultset->getFacetSet()->getFacet('priceranges');
foreach($facet as $range => $count) {
echo $range . ' to ' . ($range + 100) . ' [' . $count . ']<br/>';
}
// show documents using the resultset iterator
foreach ($resultset as $document) {
echo '<hr/><table>';
echo '<tr><th>id</th><td>' . $document->id . '</td></tr>';
echo '<tr><th>name</th><td>' . $document->name . '</td></tr>';
echo '<tr><th>price</th><td>' . $document->price . '</td></tr>';
echo '</table>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// add a query and morelikethis settings (using fluent interface)
$query->setQuery('apache')
->getMoreLikeThis()
->setFields('manu,cat')
->setMinimumDocumentFrequency(1)
->setMinimumTermFrequency(1);
// this executes the query and returns the result
$resultset = $client->select($query);
$mlt = $resultset->getMoreLikeThis();
// 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><br/><b>MLT results:</b><br/>';
// mlt results can be fetched by document id (the field defined as uniquekey in this schema)
$mltResult = $mlt->getResult($document->id);
if($mltResult){
echo 'Max score: '.$mltResult->getMaximumScore().'<br/>';
echo 'NumFound: '.$mltResult->getNumFound().'<br/>';
echo 'Num. fetched: '.count($mltResult).'<br/>';
foreach($mltResult AS $mltDoc) {
echo 'MLT result doc: '. $mltDoc->name . ' (id='. $mltDoc->id . ')<br/>';
}
}else{
echo 'No MLT results';
}
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
$query->setQuery('memory');
// get highlighting component and apply settings
$hl = $query->getHighlighting();
$hl->setFields('name, features');
$hl->setSimplePrefix('<b>');
$hl->setSimplePostfix('</b>');
// this executes the query and returns the result
$resultset = $client->select($query);
$highlighting = $resultset->getHighlighting();
// 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><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/>';
}
}
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
// get the dismax component and set a boost query
$dismax = $query->getDisMax();
$dismax->setBoostQuery('cat:"graphics card"^2');
// this query is now a dismax query
$query->setQuery('memory -printer');
// this executes the query 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>';
}
htmlFooter();
\ No newline at end of file
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance and a query helper instance
$query = $client->createSelect();
$helper = $query->getHelper();
// add a filterquery on a price range, using the helper to generate the range
$fqPrice = $query->createFilterQuery();
$fqPrice->setKey('price');
$fqPrice->setQuery($helper->rangeQuery('price', 10, 300));
$query->addFilterQuery($fqPrice);
// add a filterquery to find products in a range of 5km, using the helper to generate the 'geofilt' filter
$fqRegion = $query->createFilterQuery();
$fqRegion->setKey('region');
$fqRegion->setQuery($helper->geofilt(45.15, -93.85, 'store', 5));
$query->addFilterQuery($fqRegion);
// this executes the query 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>';
}
htmlFooter();
\ No newline at end of file
<?php
$config = array(
'adapteroptions' => array(
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/solr/',
)
);
<html>
<head>
<title>Solarium examples</title>
</head>
<body>
<h1>Solarium examples</h1>
<p>
In this folder you can find various examples for the Solarium PHP Solr client library, to be used on a Solr 'example' index as distributed with Solr.<br/>
Only the code is included, for more details about the examples and how to get them working please see the manual on the project website: <a href="http://www.solarium-project.org" target="_blank">http://www.solarium-project.org</a>
</p>
<p>
If examples for some Solarium functionality are missing please request them by opening an issue in the issue tracker: <a href="http://github.com/basdenooijer/solarium/issues" target="_blank">http://github.com/basdenooijer/solarium/issues</a>
</p>
<p>
<b>Important:</b> This code is intended to demonstrate the usage of Solarium. It is not intended for real-world use. For instance user input validation is missing or very limited, as the best way to do this will depend on your application and is not Solarium functionality.<br>
It's advised not to deploy the examples to a public environment, or at least make sure the example directory is not available via your webserver.
</p>
<ul style="list-style:none;">
<li>1. Basic usage</li>
<ul style="list-style:none;">
<li><a href="1.1-check-solarium-and-ping.php">1.1 Check solarium availability and ping Solr</a></li>
<li><a href="1.2-basic-select.php">1.2 Basic select</a></li>
<li><a href="1.3-basic-update.php">1.3 Basic update - add document</a></li>
</ul>
<li>2. Select query</li>
<ul style="list-style:none;">
<li><a href="2.1-query-params.php">2.1 Select query params</a></li>
<li><a href="2.2-custom-result-document.php">2.2 Custom result document</a></li>
<li><a href="2.3-filterquery.php">2.3 Filterquery</a></li>
<li>2.4 Components</li>
<ul style="list-style:none;">
<li>2.5.1 FacetSet</li>
<ul style="list-style:none;">
<li><a href="2.5.1.1-facet-field.php">2.5.1.1 Facet field</a></li>
<li><a href="2.5.1.2-facet-query.php">2.5.1.2 Facet query</a></li>
<li><a href="2.5.1.3-facet-multiquery.php">2.5.1.3 Facet multiquery</a></li>
<li><a href="2.5.1.4-facet-range.php">2.5.1.4 Facet range</a></li>
</ul>
<li><a href="2.5.2-morelikethis.php">2.5.2 MoreLikeThis</a></li>
<li><a href="2.5.3-highlighting.php">2.5.3 Highlighting</a></li>
<li><a href="2.5.4-dismax.php">2.5.4 Dismax</a></li>
</ul>
<li><a href="2.6-helper-functions.php">2.6 Helper functions</a></li>
</ul>
<li>3. Update query</li>
<ul style="list-style:none;">
<li><a href="3.1-add-docs.php">3.1 Add docs</a></li>
<li><a href="3.2-delete-by-query.php">3.2 Delete by query</a></li>
<li><a href="3.3-delete-by-id.php">3.3 Delete by ID</a></li>
</ul>
<li>4. Usage modes</li>
<ul style="list-style:none;">
<li><a href="4.1-api-usage.php">4.1 API</a></li>
<li><a href="4.2-configuration-usage.php">4.2 Configuration</a></li>
<li><a href="4.3-extending-usage.php">4.3 Extending</a></li>
</ul>
<li>5. Customization</li>
<ul style="list-style:none;">
<li><a href="5.1-partial-usage.php">5.1 Partial usage</a></li>
<li><a href="5.2-extending.php">5.2 Extending</a></li>
<li>5.3 Plugin system</li>
<ul style="list-style:none;">
<li><a href="5.3.1-plugin-event-hooks.php">5.3.1 Event hooks</a></li>
<li><a href="5.3.2-plugin-solarium-presets.php">5.3.2 Modifying Solarium presets</a></li>
</ul>
</ul>
<li>6. Miscellaneous</li>
<ul style="list-style:none;">
<li><a href="6.1-zend-http-adapter.php">6.1 Zend_Http adapter</a></li>
<li><a href="6.2-escaping.php">6.2 Escaping</a></li>
<li><a href="6.3-demo-app.php">2.7 Demo search (mini) application</a></li>
</ul>
</ul>
</body>
</html>
\ No newline at end of file
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
require('../library/Solarium/Autoloader.php');
Solarium_Autoloader::register();
if (file_exists('config.php')) {
require('config.php');
} else {
require('config.dist.php');
}
function htmlHeader(){
echo '<html><head><title>Solarium examples</title></head><body>';
}
function htmlFooter(){
echo '</body></html>';
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment