Skip to content

Instantly share code, notes, and snippets.

@basdenooijer
Created March 30, 2011 12:04
Show Gist options
  • Save basdenooijer/894286 to your computer and use it in GitHub Desktop.
Save basdenooijer/894286 to your computer and use it in GitHub Desktop.
Solarium document examples
<?php
// make Solarium available
require('Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client;
$client->setHost('192.168.1.2');
$client->setPort(8983);
$client->setPath('/solr/');
$client->setCore('geonames');
// a simple query
$query = new Solarium_Query_Select;
$result = $client->select($query);
foreach ($result AS $document) {
echo "--- DOCUMENT ---\n";
// count the fields in this document
echo 'Number of fields: ' . count($document) . "\n";
// access field by name as object property
echo 'Document ID: ' . $document->id . "\n";
// access field by name as array entry
echo 'Country: ' . $document['countrycode'] . "\n";
// iterate all fields
foreach($document AS $field => $value)
{
echo $field . ' = ' . $value . "\n";
}
}
<?php
// make Solarium available
require('Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client;
$client->setHost('192.168.1.2');
$client->setPort(8983);
$client->setPath('/solr/');
$client->setCore('geonames');
// create a new document
$document = new Solarium_Document_ReadWrite;
// set a field value as property
$document->id = 15;
// set a field value as array entry
$document['population'] = 120000;
// set a field value with the setField method, including a boost
$document->setField('name', 'example doc', 3);
// add two values to a multivalue field
$document->addField('countries', 'NL');
$document->addField('countries', 'UK');
// set a field to multiple values, this overwrites any existing (multi)value for this field!
$document->countries = array('DE','FR','IT');
// example: add / remove field with methods
$document->setField('dummy', 10);
$document->removeField('dummy');
// example: add / remove field with methods by setting NULL value
$document->setField('dummy', 10);
$document->setField('dummy', NULL); //this removes the field
// example: add / remove field as object property
$document->dummy = 10;
unset($document->dummy);
// example: add / remove field as array entry
$document['dummy'] = 10;
unset($document['dummy']);
// set a document boost value
$document->setBoost(2.5);
// set a field boost
$document->setFieldBoost('population', 4.5);
// add it to the update query and also add a commit
$query = new Solarium_Query_Update;
$query->addDocument($document);
$query->addCommit();
// run it, the result should be a new document in the Solr index
$client->update($query);
<?php
// make Solarium available
require('Solarium/Autoloader.php');
Solarium_Autoloader::register();
// create a client instance
$client = new Solarium_Client;
$client->setHost('192.168.1.2');
$client->setPort(8983);
$client->setPath('/solr/');
$client->setCore('geonames');
// a simple query
$query = new Solarium_Query_Select;
$query->setDocumentClass('myEntity');
$result = $client->select($query);
// the custom class (this would normally be a separate file)
class myEntity
{
protected $_fields;
public function __construct($fields)
{
$this->_fields = $fields;
}
// custom entity logic here
// (.....)
}
@ambaldinesh44
Copy link

Am getting This error

Severity: Warning

Message: require(/solarium/library/Solarium_Document_ReadWrite .php): failed to open stream: No such file or directory

Filename: Solarium/Autoloader.php

Line Number: 93

How to fix this error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment