You are here

public function Apache_Solr_Service::search in Apache Solr Search 5

Simple Search interface

Parameters

string $query The raw query string:

int $offset The starting offset for result documents:

int $limit The maximum number of result documents to return:

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 value

Apache_Solr_Response

Throws

Exception If an error occurs during the service call

File

SolrPhpClient/Apache/Solr/Service.php, line 854

Class

Apache_Solr_Service
Starting point for the Solr API. Represents a Solr server resource and has methods for pinging, adding, deleting, committing, optimizing and searching.

Code

public function search($query, $offset = 0, $limit = 10, $params = array()) {
  if (!is_array($params)) {
    $params = array();
  }

  // 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['json.nl'] = $this->_namedListTreatment;
  $params['q'] = $query;
  $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
  $queryString = http_build_query($params, null, $this->_queryStringDelimiter);

  // 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);
  return $this
    ->_sendRawGet($this->_searchUrl . $this->_queryDelimiter . $queryString);
}