You are here

function _apachesolr_reference_solr_query in Apachesolr Reference 7

Queries a SOLR environment for results.

Parameters

string $solr_env: Solr environment id.

array $field_query: solr query arguments

array $field_list: list of solr fields to return

array|bool $sort: sort method

int $rows: number of rows to return

int $start: number to start at

Return value

array Array of solr results

2 calls to _apachesolr_reference_solr_query()
apachesolr_reference_autocomplete_callback_get_matches in ./apachesolr_reference.module
Return JSON based on given field, instance and string.
_apachesolr_reference_fetch_field_items in ./apachesolr_reference.module
Fetch SOLR objects for a field from a list of SOLR ids.

File

./apachesolr_reference.module, line 736
functionality for creating reference fields to apache solr objects.

Code

function _apachesolr_reference_solr_query($solr_env, $field_query, $field_list, $sort = FALSE, $rows = 10000, $start = 0) {
  $results = FALSE;
  module_load_include('module', 'apachesolr');
  $query_cache =& drupal_static(__FUNCTION__, array());

  // Try to load the solr environment.
  try {
    $solr = apachesolr_get_solr($solr_env);
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
  }

  // Build the SOLR query.
  $params = array();
  $params['fq'] = $field_query;
  $params['fl'] = is_array($field_list) ? implode(',', $field_list) : $field_list;
  $params['rows'] = $rows;
  $params['start'] = $start;

  // Use params as key value to check static cache.
  // To prevent running the same search on a single page load.
  $cache_key = serialize($params);
  if (isset($query_cache[$cache_key])) {
    return $query_cache[$cache_key];
  }

  // Load the solr environment.
  $query = new SolrBaseQuery('apachesolr', $solr, $params, '', current_path());

  // Add sorting.
  if (isset($params['sort'])) {
    $query
      ->setAvailableSort($this->params['sort'][0], $this->params['sort'][1]);
    $query
      ->setSolrsort($this->params['sort'][0], $this->params['sort'][1]);
  }
  $query->page = 0;
  try {

    // Boost parameters if apachesolr_search module is available.
    apachesolr_search_add_boost_params($query);

    // The way that the apachesolr module works, it static caches queries per
    // environment on all page loads. This is preventing multiple queries from
    // being run, since the static cache key is the searcher value.
    // The searcher value is only unique per solr env, not per query.
    // Because of this, need to clear apachesolrs static cache for this env
    // before running query, not ideal but cannot figure out any other work arounds.
    // This may have unknown effects but shouldnt be that big of a deal.
    $searcher = $query
      ->getSearcher();
    $solr_cache =& drupal_static('apachesolr_static_response_cache', array());
    $solr_cache[$searcher] = NULL;

    // Execute search.
    list($final_query, $response) = apachesolr_do_query($query);
    apachesolr_has_searched($solr
      ->getId(), TRUE);
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
  }

  // Results found?
  if (isset($response->response->numFound) && $response->response->numFound > 0) {
    $results = $response->response->docs;
    $query_cache[$cache_key] = $results;
  }
  return $results;
}