You are here

function apachesolr_autocomplete_suggest_additional_term in Apache Solr Autocomplete 7

Same name and namespace in other branches
  1. 6 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest_additional_term()
  2. 7.2 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest_additional_term()

Helper function that suggests additional terms to search for.

For example, if $keys = "learn", this might return suggestions like: learn student, learn school, learn mathematics. The suggested terms are returned in order of frequency (most frequent first).

Parameters

string $keys: Keys to search for.

int $suggestions_to_return: Number of suggestions to return.

array $search_page: Search page.

Return value

array Suggestions array.

1 call to apachesolr_autocomplete_suggest_additional_term()
apachesolr_autocomplete_callback in ./apachesolr_autocomplete.module
Callback for url apachesolr_autocomplete/autocomplete.

File

./apachesolr_autocomplete.module, line 377
Alters search forms to suggest terms using Apache Solr using AJAX. Thanks to: robertDouglass who contributed some of the code. sch4lly for contributing to D7 version

Code

function apachesolr_autocomplete_suggest_additional_term($keys, $suggestions_to_return = 5, $search_page = NULL) {
  $keys = trim($keys);
  $keys = check_plain($keys);
  if ($keys == '') {
    return array();
  }

  // Return no suggestions when $keys consists of only word delimiters
  if (drupal_strlen(preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', '', $keys)) < 1) {
    return array();
  }

  // Ask Solr to return facets from the 'spell' field to use as suggestions.
  $params = apachesolr_autocomplete_basic_params($suggestions_to_return, $search_page);

  // Initialize arrays
  $suggestions = array();
  $replacements = array();

  // Get array of themed suggestions.
  $result = apachesolr_autocomplete_suggest($keys, $params, $keys, $suggestions_to_return, $search_page);
  if ($result && apachesolr_autocomplete_variable_get_suggest_keywords()) {
    if (isset($result['suggestions']) && sizeof($result['suggestions'])) {
      $suggestions = array_merge($suggestions, $result['suggestions']);
    }
  }

  // Suggest using the spellchecker
  if (apachesolr_autocomplete_variable_get_suggest_spellcheck()) {
    if (isset($result['response']->spellcheck) && isset($result['response']->spellcheck->suggestions)) {
      $spellcheck_suggestions = get_object_vars($result['response']->spellcheck->suggestions);
      foreach ($spellcheck_suggestions as $word => $value) {
        $replacements[$word] = $value->suggestion[0];
      }
      if (count($replacements)) {
        $new_keywords = strtr($keys, $replacements);
        if ($new_keywords != $keys) {

          // Place spellchecker suggestion before others
          $suggestion = array(
            'theme' => 'apachesolr_autocomplete_spellcheck',
            'suggestion' => $new_keywords,
          );
          $suggestions = array_merge(array(
            '*' . $new_keywords => $suggestion,
          ), $suggestions);
        }
      }
    }
  }
  return $suggestions;
}