function apachesolr_autocomplete_suggest_word_completion in Apache Solr Autocomplete 7.2
Same name and namespace in other branches
- 6 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest_word_completion()
- 7 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest_word_completion()
Helper function that suggests ways to complete partial words.
For example, if $keys = "lea", this might return suggestions like: lean, learn, lease. The suggested terms are returned in order of frequency (most frequent first).
File
- ./
apachesolr_autocomplete.module, line 294 - Alters search forms to suggest terms using Apache Solr using AJAX.
Code
function apachesolr_autocomplete_suggest_word_completion($keys, $context) {
/**
* Split $keys into two:
* $first_part will contain all complete words (delimited by spaces). Can be empty.
* $last_part is the (assumed incomplete) last word. If this is empty, don't suggest.
* Example:
* $keys = "learning dis" : $first_part = "learning", $last_part = "dis"
*/
preg_match('/^(:?(.* |))([^ ]+)$/', $keys, $matches);
$first_part = @$matches[2];
// Make sure $last_part contains meaningful characters
$last_part = preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', '', @$matches[3]);
if ($last_part == '') {
return array();
}
// Ask Solr to return facets that begin with $last_part; these will be the suggestions.
$params = $context['apachesolr_params'];
$params['facet.prefix'] = $last_part;
// Get array of themed suggestions.
$result = apachesolr_autocomplete_suggest($context['solr'], $first_part, $params, $context['suggestions_to_return']);
if (!$result) {
return array();
}
// Concatenate the original terms with the returned suggestion.
$tmp = array();
foreach ($result['suggestions'] as $key => $value) {
$key = substr($key, 1);
$value['value'] = $first_part . $value['value'];
$tmp["*" . $value['value']] = $value;
}
return $tmp;
}