You are here

public function Drupal_Apache_Solr_Service::search in Apache Solr Search 6.2

Same name and namespace in other branches
  1. 6 Drupal_Apache_Solr_Service.php \Drupal_Apache_Solr_Service::search()

Switch to POST for search if resultant query string is too long.

Default threshold is 4000 characters. Note that this is almost an exact copy of Apache_Solr_Service::search().

See also

Apache_Solr_Service::search

File

./Drupal_Apache_Solr_Service.php, line 271

Class

Drupal_Apache_Solr_Service

Code

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

  // 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);

  // Check string length of the query string, change method to POST
  // if longer than 4000 characters.
  if (strlen($queryString) > variable_get('apachesolr_search_post_threshold', 4000)) {
    $method = self::METHOD_POST;
  }
  if ($method == self::METHOD_GET) {
    return $this
      ->_sendRawGet($this->_searchUrl . $this->_queryDelimiter . $queryString);
  }
  else {
    if ($method == self::METHOD_POST) {
      return $this
        ->_sendRawPost($this->_searchUrl, $queryString, FALSE, 'application/x-www-form-urlencoded; charset=UTF-8');
    }
    else {
      throw new Exception("Unsupported method '{$method}', please use the Apache_Solr_Service::METHOD_* constants");
    }
  }
}