You are here

function apachesolr_search_search_results in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr_search.module \apachesolr_search_search_results()
  2. 7 apachesolr_search.module \apachesolr_search_search_results()

Executes search depending on the conditions given. See apachesolr_search.pages.inc for another use of this function

2 calls to apachesolr_search_search_results()
apachesolr_search_custom_page in ./apachesolr_search.pages.inc
Returns search results on user defined search pages.
apachesolr_search_search_execute in ./apachesolr_search.module
Implements hook_search_execute().

File

./apachesolr_search.module, line 833
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_search_results($keys = NULL, $conditions = NULL, $search_page = NULL) {
  $params = array();
  $results = array();

  // Process the search form. Note that if there is $_POST data,
  // search_form_submit() will cause a redirect to search/[module path]/[keys],
  // which will get us back to this page callback. In other words, the search
  // form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle.
  if (empty($_POST['form_id']) || $_POST['form_id'] != 'apachesolr_search_custom_page_search_form' && $_POST['form_id'] != 'search_form' && $_POST['form_id'] != 'search_block_form') {

    // Check input variables
    if (empty($search_page)) {
      $search_page = apachesolr_search_page_load('core_search');

      // Verify if it actually loaded
      if (empty($search_page)) {

        // Something must have been really messed up.
        apachesolr_failure(t('Solr search'), $keys);
        return array();
      }
    }
    if (empty($conditions)) {
      $conditions = apachesolr_search_conditions_default($search_page);
    }

    // Sort options from the conditions array.
    // @see apachesolr_search_conditions_default()
    //   See This condition callback to find out how.
    $solrsort = isset($conditions['apachesolr_search_sort']) ? $conditions['apachesolr_search_sort'] : '';

    // What to do when we have an initial empty search
    $empty_search_behavior = isset($search_page['settings']['apachesolr_search_browse']) ? $search_page['settings']['apachesolr_search_browse'] : '';
    try {
      $solr = apachesolr_get_solr($search_page['env_id']);

      // Default parameters
      $params['fq'] = isset($conditions['fq']) ? $conditions['fq'] : array();
      $params['rows'] = $search_page['settings']['apachesolr_search_per_page'];
      if (empty($search_page['settings']['apachesolr_search_spellcheck'])) {

        // Spellcheck needs to have a string as false/true
        $params['spellcheck'] = 'false';
      }
      else {
        $params['spellcheck'] = 'true';
      }

      // Empty text Behavior
      if (!$keys && !isset($conditions['f']) && ($empty_search_behavior == 'browse' || $empty_search_behavior == 'blocks')) {

        // Pass empty search behavior as string on to apachesolr_search_search_page()
        // Hardcoded apachesolr name since we rely on this for the facets
        $context['page_id'] = $search_page['page_id'];
        $context['search_type'] = 'apachesolr_search_browse';
        apachesolr_search_run_empty('apachesolr', $params, $search_page['search_path'], $solr, $context);
        $results['apachesolr_search_browse'] = $empty_search_behavior;
        if ($empty_search_behavior == 'browse') {

          // Hide sidebar blocks for content-area browsing instead.
          apachesolr_suppress_blocks($search_page['env_id'], TRUE);
        }
      }
      elseif ($keys || isset($conditions['f']) || $empty_search_behavior == 'results') {

        // Don't allow local params to pass through to EDismax from the url.
        // We also remove any remaining leading {! since that causes a parse
        // error in Solr.
        $keys = preg_replace('/^(?:{![^}]*}\\s*)*(?:{!\\s*)*/', ' ', $keys);
        $params['q'] = $keys;

        // Hardcoded apachesolr name since we rely on this for the facets
        $context['page_id'] = $search_page['page_id'];
        $context['search_type'] = 'apachesolr_search_results';
        $results = apachesolr_search_run('apachesolr', $params, $solrsort, $search_page['search_path'], pager_find_page(), $solr, $context);
      }
    } catch (Exception $e) {
      watchdog('Apache Solr', nl2br(check_plain($e
        ->getMessage())), NULL, WATCHDOG_ERROR);
      apachesolr_failure(t('Solr search'), $keys);
    }
  }
  return $results;
}