You are here

function apachesolr_search_get_search_suggestions in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 apachesolr_search.module \apachesolr_search_get_search_suggestions()
  2. 7 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 seperate so we are not dependent from core search when someone tries to disable the core search

File

./apachesolr_search.module, line 1509
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 = $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);
      if ($suggestions) {
        $replacements = array();

        // Get the original query and retrieve all words with suggestions.
        foreach ($suggestions as $word => $value) {
          $replacements[$word] = $value->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;
}