Source for file Service.php
Documentation is available at Service.php
* Copyright (c) 2007-2009, Conduit Internet Technologies, Inc.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of Conduit Internet Technologies, Inc. nor the names of
* its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* @copyright Copyright 2007-2009 Conduit Internet Technologies, Inc. (http://conduit-it.com)
* @license New BSD (http://solr-php-client.googlecode.com/svn/trunk/COPYING)
* @version $Id: Service.php 22 2009-11-09 22:46:54Z donovan.jimenez $
* @author Donovan Jimenez <djimenez@conduit-it.com>
// See Issue #1 (http://code.google.com/p/solr-php-client/issues/detail?id=1)
// Doesn't follow typical include path conventions, but is more convenient for users
require_once(dirname(__FILE__
) .
'/Document.php');
require_once(dirname(__FILE__
) .
'/Response.php');
* Starting point for the Solr API. Represents a Solr server resource and has
* methods for pinging, adding, deleting, committing, optimizing and searching.
* $solr = new Apache_Solr_Service(); //or explicitly new Apache_Solr_Service('localhost', 8180, '/solr')
* $solr->deleteByQuery('*:*'); //deletes ALL documents - be careful :)
* $document = new Apache_Solr_Document();
* $document->id = uniqid(); //or something else suitably unique
* $document->title = 'Some Title';
* $document->content = 'Some content for this wonderful document. Blah blah blah.';
* $solr->addDocument($document); //if you're going to be adding documents in bulk using addDocuments
* //with an array of documents is faster
* $solr->commit(); //commit to see the deletes and the document
* $solr->optimize(); //merges multiple segments into one
* //and the one we all care about, search!
* //any other common or custom parameters to the request handler can go in the
* //optional 4th array argument.
* $solr->search('content:blah', 0, 10, array('sort' => 'timestamp desc'));
* @todo Investigate using other HTTP clients other than file_get_contents built-in handler. Could provide performance
* improvements when dealing with multiple requests by using HTTP's keep alive functionality
* SVN Revision meta data for this class
const SVN_REVISION =
'$Revision: 22 $';
* SVN ID meta data for this class
const SVN_ID =
'$Id: Service.php 22 2009-11-09 22:46:54Z donovan.jimenez $';
* Response version we support
const SOLR_VERSION =
'1.2';
* Response writer we'll request - JSON. See http://code.google.com/p/solr-php-client/issues/detail?id=6#c1 for reasoning
const SOLR_WRITER =
'json';
* NamedList Treatment constants
const NAMED_LIST_FLAT =
'flat';
const NAMED_LIST_MAP =
'map';
const METHOD_GET =
'GET';
const METHOD_POST =
'POST';
const PING_SERVLET =
'admin/ping';
const UPDATE_SERVLET =
'update';
const SEARCH_SERVLET =
'select';
const THREADS_SERVLET =
'admin/threads';
* Server identification strings
* Whether {@link Apache_Solr_Response} objects should create {@link Apache_Solr_Document}s in
* the returned parsed data
* Whether {@link Apache_Solr_Response} objects should have multivalue fields with only a single value
* collapsed to appear as a single value would.
* How NamedLists should be formatted in the output. This specifically effects facet counts. Valid values
* are {@link Apache_Solr_Service::NAMED_LIST_MAP} (default) or {@link Apache_Solr_Service::NAMED_LIST_FLAT}.
* Query delimiters. Someone might want to be able to change
* these (to use & instead of & for example), so I've provided them.
* Constructed servlet full path URLs
* Keep track of whether our URLs have been constructed
* Reusable stream context resources for GET and POST operations
* Default HTTP timeout when one is not specified (initialized to default_socket_timeout ini setting)
* Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
* NOTE: inside a phrase fewer characters need escaped, use {@link Apache_Solr_Service::escapePhrase()} instead
static public function escape($value)
//list taken from http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping%20Special%20Characters
$pattern =
'/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
* Escape a value meant to be contained in a phrase for special query characters
* Convenience function for creating phrase syntax from a value
static public function phrase($value)
return '"' .
self::escapePhrase($value) .
'"';
* Constructor. All parameters are optional and will take on default values
public function __construct($host =
'localhost', $port =
8180, $path =
'/solr/')
// create our shared get and post stream contexts
// determine our default http timeout from ini settings
// double check we didn't get 0 for a timeout
* Return a valid http URL given this server's host, port and path and a provided servlet name
//escape all parameters appropriately for inclusion in the query string
$escapedParams =
array();
foreach ($params as $key =>
$value)
return 'http://' .
$this->_host .
':' .
$this->_port .
$this->_path .
$servlet .
$queryString;
* Construct the Full URLs for the three servlets we reference
//Initialize our full servlet URLs now that we have server information
* Central method for making a get operation against this Solr Server
* @param float $timeout Read timeout in seconds
* @return Apache_Solr_Response
* @throws Exception If a non 200 response status is returned
// set the timeout if specified
if ($timeout !==
FALSE &&
$timeout >
0.0)
// timeouts with file_get_contents seem to need
// to be halved to work as expected
$timeout = (float)
$timeout /
2;
// use the default timeout pulled from default_socket_timeout otherwise
//$http_response_header is set by file_get_contents
if ($response->getHttpStatus() !=
200)
throw
new Exception('"' .
$response->getHttpStatus() .
'" Status: ' .
$response->getHttpStatusMessage(), $response->getHttpStatus());
* Central method for making a post operation against this Solr Server
* @param float $timeout Read timeout in seconds
* @param string $contentType
* @return Apache_Solr_Response
* @throws Exception If a non 200 response status is returned
protected function _sendRawPost($url, $rawPost, $timeout =
FALSE, $contentType =
'text/xml; charset=UTF-8')
// Add our posted content type
'header' =>
"Content-Type: $contentType",
// set the timeout if specified
if ($timeout !==
FALSE &&
$timeout >
0.0)
// timeouts with file_get_contents seem to need
// to be halved to work as expected
$timeout = (float)
$timeout /
2;
//$http_response_header is set by file_get_contents
if ($response->getHttpStatus() !=
200)
throw
new Exception('"' .
$response->getHttpStatus() .
'" Status: ' .
$response->getHttpStatusMessage(), $response->getHttpStatus());
* Set the host used. If empty will fallback to constants
//Use the provided host or use the default
throw
new Exception('Host parameter is empty');
* Set the port used. If empty will fallback to constants
//Use the provided port or use the default
throw
new Exception('Port is not a valid port number');
* Set the path used. If empty will fallback to constants
$path =
trim($path, '/');
$this->_path =
'/' .
$path .
'/';
* Set the create documents flag. This determines whether {@link Apache_Solr_Response} objects will
* parse the response and create {@link Apache_Solr_Document} instances in place.
* @param unknown_type $createDocuments
* Get the current state of teh create documents flag.
* Set the collapse single value arrays flag.
* @param boolean $collapseSingleValueArrays
* Get the current state of the collapse single value arrays flag.
* Set how NamedLists should be formatted in the response data. This mainly effects
* the facet counts format.
* @param string $namedListTreatment
* @throws Exception If invalid option is set
switch ((string)
$namedListTreatment)
throw
new Exception('Not a valid named list treatement option');
* Get the current setting for named list treatment.
* Set the string used to separate the path form the query string.
* @param string $queryDelimiter
* Set the string used to separate the parameters in thequery string
* @param string $queryStringDelimiter
* Call the /admin/ping servlet, can be used to quickly tell if a connection to the
* server is able to be made.
* @param float $timeout maximum time to wait for ping in seconds, -1 for unlimited (default is 2)
* @return float Actual time taken to ping the server, FALSE if timeout or HTTP error status occurs
public function ping($timeout =
2)
// when using timeout in context and file_get_contents
// it seems to take twice the timout value
$timeout = (float)
$timeout /
2;
// attempt a HEAD request to the solr ping page
// result is false if there was a timeout
// or if the HTTP status was not 200
* Call the /admin/threads servlet and retrieve information about all threads in the
* Solr servlet's thread group. Useful for diagnostics.
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
* Raw Add Method. Takes a raw post body and sends it to the update service. Post body
* should be a complete and well formed "add" xml document.
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function add($rawPost)
* Add a Solr Document to the index
* @param Apache_Solr_Document $document
* @param boolean $allowDups
* @param boolean $overwritePending
* @param boolean $overwriteCommitted
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function addDocument(Apache_Solr_Document $document, $allowDups =
false, $overwritePending =
true, $overwriteCommitted =
true)
$dupValue =
$allowDups ?
'true' :
'false';
$pendingValue =
$overwritePending ?
'true' :
'false';
$committedValue =
$overwriteCommitted ?
'true' :
'false';
$rawPost =
'<add allowDups="' .
$dupValue .
'" overwritePending="' .
$pendingValue .
'" overwriteCommitted="' .
$committedValue .
'">';
return $this->add($rawPost);
* Add an array of Solr Documents to the index all at once
* @param array $documents Should be an array of Apache_Solr_Document instances
* @param boolean $allowDups
* @param boolean $overwritePending
* @param boolean $overwriteCommitted
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function addDocuments($documents, $allowDups =
false, $overwritePending =
true, $overwriteCommitted =
true)
$dupValue =
$allowDups ?
'true' :
'false';
$pendingValue =
$overwritePending ?
'true' :
'false';
$committedValue =
$overwriteCommitted ?
'true' :
'false';
$rawPost =
'<add allowDups="' .
$dupValue .
'" overwritePending="' .
$pendingValue .
'" overwriteCommitted="' .
$committedValue .
'">';
foreach ($documents as $document)
return $this->add($rawPost);
* Create an XML fragment from a {@link Apache_Solr_Document} instance appropriate for use inside a Solr add call
if ($document->getBoost() !==
false)
$xml .=
' boost="' .
$document->getBoost() .
'"';
foreach ($document as $key =>
$value)
$fieldBoost =
$document->getFieldBoost($key);
foreach ($value as $multivalue)
$xml .=
'<field name="' .
$key .
'"';
if ($fieldBoost !==
false)
$xml .=
' boost="' .
$fieldBoost .
'"';
// only set the boost for the first field in the set
$xml .=
'>' .
$multivalue .
'</field>';
$xml .=
'<field name="' .
$key .
'"';
if ($fieldBoost !==
false)
$xml .=
' boost="' .
$fieldBoost .
'"';
$xml .=
'>' .
$value .
'</field>';
// replace any control characters to avoid Solr XML parser exception
* Replace control (non-printable) characters from string that are invalid to Solr's XML parser with a space.
// See: http://w3.org/International/questions/qa-forms-utf-8.html
// Printable utf-8 does not include any of these chars below x7F
return preg_replace('@[\x00-\x08\x0B\x0C\x0E-\x1F]@', ' ', $string);
* Send a commit command. Will be synchronous unless both wait parameters are set to false.
* @param boolean $optimize Defaults to true
* @param boolean $waitFlush Defaults to true
* @param boolean $waitSearcher Defaults to true
* @param float $timeout Maximum expected duration (in seconds) of the commit operation on the server (otherwise, will throw a communication exception). Defaults to 1 hour
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function commit($optimize =
true, $waitFlush =
true, $waitSearcher =
true, $timeout =
3600)
$optimizeValue =
$optimize ?
'true' :
'false';
$flushValue =
$waitFlush ?
'true' :
'false';
$searcherValue =
$waitSearcher ?
'true' :
'false';
$rawPost =
'<commit optimize="' .
$optimizeValue .
'" waitFlush="' .
$flushValue .
'" waitSearcher="' .
$searcherValue .
'" />';
* Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be
* a complete and well formed "delete" xml document
* @param string $rawPost Expected to be utf-8 encoded xml document
* @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function delete($rawPost, $timeout =
3600)
* Create a delete document based on document ID
* @param string $id Expected to be utf-8 encoded
* @param boolean $fromPending
* @param boolean $fromCommitted
* @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function deleteById($id, $fromPending =
true, $fromCommitted =
true, $timeout =
3600)
$pendingValue =
$fromPending ?
'true' :
'false';
$committedValue =
$fromCommitted ?
'true' :
'false';
//escape special xml characters
$rawPost =
'<delete fromPending="' .
$pendingValue .
'" fromCommitted="' .
$committedValue .
'"><id>' .
$id .
'</id></delete>';
return $this->delete($rawPost, $timeout);
* Create and post a delete document based on multiple document IDs.
* @param array $ids Expected to be utf-8 encoded strings
* @param boolean $fromPending
* @param boolean $fromCommitted
* @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function deleteByMultipleIds($ids, $fromPending =
true, $fromCommitted =
true, $timeout =
3600)
$pendingValue =
$fromPending ?
'true' :
'false';
$committedValue =
$fromCommitted ?
'true' :
'false';
$rawPost =
'<delete fromPending="' .
$pendingValue .
'" fromCommitted="' .
$committedValue .
'">';
//escape special xml characters
$rawPost .=
'<id>' .
$id .
'</id>';
return $this->delete($rawPost, $timeout);
* Create a delete document based on a query and submit it
* @param string $rawQuery Expected to be utf-8 encoded
* @param boolean $fromPending
* @param boolean $fromCommitted
* @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function deleteByQuery($rawQuery, $fromPending =
true, $fromCommitted =
true, $timeout =
3600)
$pendingValue =
$fromPending ?
'true' :
'false';
$committedValue =
$fromCommitted ?
'true' :
'false';
// escape special xml characters
$rawPost =
'<delete fromPending="' .
$pendingValue .
'" fromCommitted="' .
$committedValue .
'"><query>' .
$rawQuery .
'</query></delete>';
return $this->delete($rawPost, $timeout);
* Send an optimize command. Will be synchronous unless both wait parameters are set
* @param boolean $waitFlush
* @param boolean $waitSearcher
* @param float $timeout Maximum expected duration of the commit operation on the server (otherwise, will throw a communication exception)
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function optimize($waitFlush =
true, $waitSearcher =
true, $timeout =
3600)
$flushValue =
$waitFlush ?
'true' :
'false';
$searcherValue =
$waitSearcher ?
'true' :
'false';
$rawPost =
'<optimize waitFlush="' .
$flushValue .
'" waitSearcher="' .
$searcherValue .
'" />';
* Simple Search interface
* @param string $query The raw query string
* @param int $offset The starting offset for result documents
* @param int $limit The maximum number of result documents to return
* @param array $params key / value pairs for other query parameters (see Solr documentation), use arrays for parameter keys used more than once (e.g. facet.field)
* @return Apache_Solr_Response
* @throws Exception If an error occurs during the service call
public function search($query, $offset =
0, $limit =
10, $params =
array(), $method =
self::METHOD_GET)
// construct our full parameters
// sending the version is important in case the format changes
$params['version'] =
self::SOLR_VERSION;
// common parameters in this interface
$params['wt'] =
self::SOLR_WRITER;
$params['start'] =
$offset;
$params['rows'] =
$limit;
// use http_build_query to encode our arguments because its faster
// than urlencoding all the parts ourselves in a loop
// because http_build_query treats arrays differently than we want to, correct the query
// string by changing foo[#]=bar (# being an actual number) parameter strings to just
// multiple foo=bar strings. This regex should always work since '=' will be urlencoded
// anywhere else the regex isn't expecting it
$queryString =
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryString);
if ($method ==
self::METHOD_GET)
else if ($method ==
self::METHOD_POST)
throw
new Exception("Unsupported method '$method', please use the Apache_Solr_Service::METHOD_* constants");
Documentation generated on Mon, 09 Nov 2009 18:15:45 -0500 by phpDocumentor 1.4.2