Skip to content

Instantly share code, notes, and snippets.

@basdenooijer
Created February 22, 2011 12:28
Show Gist options
  • Save basdenooijer/838598 to your computer and use it in GitHub Desktop.
Save basdenooijer/838598 to your computer and use it in GitHub Desktop.
Solarium update examples
<?php
// this is the easiest way to add a delete command, by using one of the helper methods:
$query = new Solarium_Query_Update;
$query->addDeleteQuery('*:*');
$query->addCommit();
$client->update($query);
// alternatively you can construct the command yourself:
$query = new Solarium_Query_Update;
$delete = new Solarium_Query_Update_Command_Delete;
$delete->addQuery('*:*');
$query->add('cmd1', $delete);
$client->update($query);
<?php
$query = new Solarium_Query_Update;
// delete a single document by id
$query->addDeleteById(10);
// delete multiple documents by id
$query->addDeleteByIds(array(3,6,9));
$query->addCommit();
// the result will be the deletion of id 3,6,9 and 10
$client->update($query);
<?php
// this is the easiest way to add a rollback command, by using the helper method:
$query = new Solarium_Query_Update;
$query->addRollback();
$client->update($query);
// alternatively you can construct the command yourself:
$query = new Solarium_Query_Update;
$rollback = new Solarium_Query_Update_Command_Rollback;
$query->add('cmd1', $rollback);
$client->update($query);
<?php
// create a new document
$document = new Solarium_Document_ReadWrite;
$document->id = 15;
$document->name = 'example doc';
// this is the easiest way to add a document, by using the helper method:
// the second and third argument are optional, see API docs
$query = new Solarium_Query_Update;
$query->addDocument($document, true, 2000);
$client->update($query);
// alternatively you can construct the command yourself:
$query = new Solarium_Query_Update;
$add = new Solarium_Query_Update_Command_Add;
$add->addDocument($document);
$add->setOverwrite(true);
$query->add('cmd1', $add);
$client->update($query);
// you can also add multiple documents at once:
$query = new Solarium_Query_Update;
$query->addDocuments(array($doc1, $doc2, $doc3));
$client->update($query);
<?php
// this is the easiest way to add an optimize command, by using the helper method:
// optimize index with (optional) settings for waitFlush, waitSearcher and maxSegments
$query = new Solarium_Query_Update;
$query->addOptimize(true, true, 5);
$client->update($query);
// alternatively you can construct the command yourself:
$query = new Solarium_Query_Update;
$optimize = new Solarium_Query_Update_Command_Optimize;
$optimize->setWaitSearcher(true);
$optimize->setWaitFlush(true);
$optimize->setMaxSegments(5);
$query->add('cmd1', $optimize);
$client->update($query);
<?php
// create a new document
$document = new Solarium_Document_ReadWrite;
$document->id = 16;
$document->name = 'example doc';
$query = new Solarium_Query_Update;
// rollback any uncommited updates
$query->addRollback();
// delete a single document by id
$query->addDeleteById(15);
// delete a set of documents by query
$query->addDeleteQuery('id:[1 TO 10]');
// add the new document
$query->addDocument($document);
// commit the changes
$query->addCommit();
// and optimize
$query->addOptimize();
// now execute the update
$client->update($query);
<?php
// this is the easiest way to add a commit command, by using the helper method:
// commit changes to index with (optional) settings for waitFlush, waitSearcher and expungeDeletes
$query = new Solarium_Query_Update;
$query->addCommit(true, true, false);
$client->update($query);
// alternatively you can construct the command yourself:
$query = new Solarium_Query_Update;
$commit = new Solarium_Query_Update_Command_Commit;
$commit->setWaitSearcher(true);
$commit->setWaitFlush(true);
$commit->setExpungeDeletes(false);
$query->add('cmd1', $commit);
$client->update($query);
<?php
// this example combines delete by id and by query into a single delete command
$query = new Solarium_Query_Update;
$delete = new Solarium_Query_Update_Command_Delete;
$delete->addQuery('*:*');
$delete->addIds(array(1,2,3,4,5));
$query->add('cmd1', $delete);
$client->update($query);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment