You are here

function apachesolr_search_get_search_suggestions in Apache Solr Search 7

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

Retrieve all of the suggestions that were given after a certain search

Return value

array()

1 call to apachesolr_search_get_search_suggestions()
apachesolr_search_search_page_custom in ./apachesolr_search.module
Mimics apachesolr_search_search_page() but is used for custom search pages We prefer to keep them separate so we are not dependent from core search when someone tries to disable the core search

File

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

Code

function apachesolr_search_get_search_suggestions($env_id) {
  $suggestions_output = array();
  if (apachesolr_has_searched($env_id)) {
    $query = apachesolr_current_query($env_id);
    $keyword = strtolower($query
      ->getParam('q'));
    $searcher = $query
      ->getSearcher();
    $response = apachesolr_static_response_cache($searcher);

    // Get spellchecker suggestions into an array.
    if (!empty($response->spellcheck->suggestions)) {
      $suggestions = get_object_vars($response->spellcheck->suggestions);

      // allow the suggestions to be altered before processing
      drupal_alter('apachesolr_suggestions', $suggestions, $env_id);
      if ($suggestions) {
        $replacements = array();

        // Get the original query and retrieve all words with suggestions.
        foreach ($suggestions as $word => $value) {
          $suggestion = $value->suggestion;

          // We need to check if it's an object as setting the spellcheck.extendedResults query parameter to true makes words
          // objects instead of strings.
          $replacements[$word] = is_object($suggestion[0]) ? $suggestion[0]->word : $suggestion[0];
        }

        // Replace the keyword with the suggested keyword.
        $suggested_keyword = strtr($keyword, $replacements);

        // Show only if suggestion is different than current query.
        if ($keyword != $suggested_keyword) {
          $suggestions_output[] = $suggested_keyword;
        }
      }
    }
  }
  return $suggestions_output;
}