You are here

public function SearchApiSolrConnection::search in Search API Solr 7

Executes a search on the Solr server.

Parameters

string|null $query: (optional) The raw query string. Defaults to an empty query.

array $params: (optional) Key / value pairs for other query parameters (see Solr documentation). Use arrays for parameter keys used more than once (e.g., facet.field).

string $method: The HTTP method to use. Must be either "GET", "POST" or "AUTO". Defaults to "GET".

Return value

object A response object.

Throws

SearchApiException If an error occurs during the service call.

Overrides SearchApiSolrConnectionInterface::search

File

includes/solr_connection.inc, line 924

Class

SearchApiSolrConnection
Represents a Solr server resource.

Code

public function search($query = NULL, 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 (isset($query)) {
    $params['q'] = $query;
  }

  // Carry out some performance improvements when no search keys are given.
  if (!isset($params['q']) || !strlen($params['q'])) {

    // Without search keys, the qf parameter is useless. We also remove empty
    // search keys here. (With our normal service class, empty keys won't be
    // set, but another module using this connection class might do that.)
    unset($params['q'], $params['qf']);
  }

  // Build the HTTP query string. We have our own method for that since PHP's
  // built-in http_build_query() doesn't give us the format Solr wants.
  $queryString = $this
    ->httpBuildQuery($params);
  if (!empty($this->options['log_query'])) {
    watchdog('search_api_solr', 'Query: @query', array(
      '@query' => $queryString,
    ), WATCHDOG_DEBUG);
  }
  if ($method == 'GET' || $method == 'AUTO') {
    $searchUrl = $this
      ->constructUrl(self::SEARCH_SERVLET, array(), $queryString);
    if ($method == 'GET' || strlen($searchUrl) <= variable_get('search_api_solr_http_get_max_length', 4000)) {
      $response = $this
        ->sendRawGet($searchUrl);
      if (!empty($this->options['log_response'])) {
        $this
          ->logResponse($response);
      }
      return $response;
    }
  }

  // Method is POST, or AUTO with a long query
  $searchUrl = $this
    ->constructUrl(self::SEARCH_SERVLET);
  $options['data'] = $queryString;
  $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
  $response = $this
    ->sendRawPost($searchUrl, $options);
  if (!empty($this->options['log_response'])) {
    $this
      ->logResponse($response);
  }
  return $response;
}