Skip to content

Commit

Permalink
Added new examples and improved some existing examples for 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
basdenooijer committed Jul 22, 2011
1 parent daec42c commit ce1d99f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
14 changes: 7 additions & 7 deletions examples/1.3-basic-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
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();

// create a new document for the data
// please note that any type of validation is missing in this example to keep it simple!
$doc = $update->createDocument();
$doc->id = $_POST['id'];
$doc->name = $_POST['name'];
$doc->price = $_POST['price'];

// add the document and a commit command to the update query
$update->addDocument($doc);
$update->addCommit();
Expand Down
15 changes: 7 additions & 8 deletions examples/3.1-add-docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
require('init.php');
htmlHeader();

// create a client instance
$client = new Solarium_Client($config);

// get an update query instance
$update = $client->createUpdate();

// create a new document for the data
$doc1 = new Solarium_Document_ReadWrite();
$doc1 = $update->createDocument();
$doc1->id = 123;
$doc1->name = 'testdoc-1';
$doc1->price = 364;

// and a second one
$doc2 = new Solarium_Document_ReadWrite();
$doc2 = $update->createDocument();
$doc2->id = 124;
$doc2->name = 'testdoc-2';
$doc2->price = 340;

// create a client instance
$client = new Solarium_Client($config);

// 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();
Expand Down
1 change: 1 addition & 0 deletions examples/6.2-escaping.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
$input = 'ATA "133';

// in this case phrase escaping is used (most common) but you can also do term escaping, see the manual
// also note that the same can be done using the placeholder syntax, see example 6.3
$helper = $query->getHelper();
$query->setQuery('features:' . $helper->escapePhrase($input));

Expand Down
8 changes: 0 additions & 8 deletions examples/6.3-demo-app.php

This file was deleted.

45 changes: 45 additions & 0 deletions examples/6.3-placeholder-syntax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

require('init.php');
htmlHeader();

// create a client instance
$client = new Solarium_Client($config);

// get a select query instance
$query = $client->createSelect();

// search input string, this value fails without escaping because of the double-quote
$input = 'ATA "133';

// the placeholder syntax applies phrase escaping to the first term
// see the manual for all supported formats
$query->setQuery('features: %p1% AND inStock:%2%',array($input,1));

// show the result after replacing the placeholders with values
echo $query->getQuery() . '<br/>';

// 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();
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h1>Solarium examples</h1>
<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>
<li><a href="6.3-placeholder-syntax.php">6.3 Placeholder syntax</a></li>
</ul>

</ul>
Expand Down

0 comments on commit ce1d99f

Please sign in to comment.