You are here

public function DrupalApacheSolrService::search in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 Drupal_Apache_Solr_Service.php \DrupalApacheSolrService::search()
  2. 6.3 Drupal_Apache_Solr_Service.php \DrupalApacheSolrService::search()

Simple Search interface

Parameters

string $query The raw query string:

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

response object

Throws

Exception If an error occurs during the service call

Overrides DrupalApacheSolrServiceInterface::search

File

./Drupal_Apache_Solr_Service.php, line 915

Class

DrupalApacheSolrService
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 = '', array $params = array(), $method = 'GET') {

  // Always use JSON. See http://code.google.com/p/solr-php-client/issues/detail?id=6#c1 for reasoning
  $params['wt'] = 'json';

  // Additional default params.
  $params += array(
    'json.nl' => self::NAMED_LIST_FORMAT,
  );
  if ($query) {
    $params['q'] = $query;
  }

  // PHP's built in http_build_query() doesn't give us the format Solr wants.
  $queryString = $this
    ->httpBuildQuery($params);

  // Check string length of the query string, change method to POST
  $len = strlen($queryString);

  // Fetch our threshold to find out when to flip to POST
  $max_len = apachesolr_environment_variable_get($this->env_id, 'apachesolr_search_post_threshold', 3600);

  // if longer than $max_len (default 3600) characters
  // we should switch to POST (a typical server handles 4096 max).
  // If this class is used independently (without environments), we switch automatically to POST at an
  // limit of 1800 chars.
  if ($len > 1800 && (empty($this->env_id) || $len > $max_len)) {
    $method = 'POST';
  }
  if ($method == 'GET') {
    $searchUrl = $this
      ->_constructUrl(self::SEARCH_SERVLET, array(), $queryString);
    return $this
      ->_sendRawGet($searchUrl);
  }
  else {
    if ($method == 'POST') {
      $searchUrl = $this
        ->_constructUrl(self::SEARCH_SERVLET);
      $options['data'] = $queryString;
      $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
      return $this
        ->_sendRawPost($searchUrl, $options);
    }
    else {
      throw new Exception("Unsupported method '{$method}' for search(), use GET or POST");
    }
  }
}