function apachesolr_autocomplete_suggest_additional_term in Apache Solr Autocomplete 6
Same name and namespace in other branches
- 7.2 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest_additional_term()
- 7 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).
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 250 - Alters search forms to suggest terms using Apache Solr using AJAX. Thanks to robertDouglass who contributed some of the code.
Code
function apachesolr_autocomplete_suggest_additional_term($keys, $suggestions_to_return = 5) {
$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_SEARCH_EXCLUDE . ']+/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);
// Initialize arrays
$suggestions = array();
$replacements = array();
// Get array of themed suggestions.
$result = apachesolr_autocomplete_suggest($keys, $params, $keys, $suggestions_to_return);
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 (is_object($result['response']->spellcheck) && is_object($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
$suggestions = array_merge(array(
'*' . $new_keywords => array(
'theme' => 'apachesolr_autocomplete_spellcheck',
'suggestion' => $new_keywords,
),
), $suggestions);
}
}
}
}
return $suggestions;
}