function apachesolr_autocomplete_suggest in Apache Solr Autocomplete 7.2
Same name and namespace in other branches
- 6 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest()
- 7 apachesolr_autocomplete.module \apachesolr_autocomplete_suggest()
2 calls to apachesolr_autocomplete_suggest()
- apachesolr_autocomplete_suggest_additional_term in ./apachesolr_autocomplete.module
- Helper function that suggests additional terms to search for.
- apachesolr_autocomplete_suggest_word_completion in ./apachesolr_autocomplete.module
- Helper function that suggests ways to complete partial words.
File
- ./apachesolr_autocomplete.module, line 387
- Alters search forms to suggest terms using Apache Solr using AJAX.
Code
function apachesolr_autocomplete_suggest(DrupalApacheSolrServiceInterface $solr, $term, $params, $suggestions_to_return = 5) {
$matches = array();
$suggestions = array();
$term = trim($term);
$keys_array = explode(' ', $term);
$keys_array = array_filter($keys_array);
$query = apachesolr_drupal_query("apachesolr", $params, '', '', $solr);
apachesolr_search_add_spellcheck_params($query);
apachesolr_search_add_boost_params($query);
drupal_alter('apachesolr_autocomplete_query', $query);
if (!$query) {
return array();
}
$response = $query
->search($term);
foreach ($params['facet.field'] as $field) {
foreach ($response->facet_counts->facet_fields->{$field} as $terms => $count) {
$terms = preg_replace('/[_-]+/', ' ', $terms);
foreach (explode(' ', $terms) as $term) {
$term = trim(preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', '', $term));
if ($term) {
if (isset($matches[$term])) {
$matches[$term] += $count;
}
else {
$matches[$term] = $count;
}
}
}
}
}
if (sizeof($matches) > 0) {
$matches_clone = $matches;
$stopwords = apachesolr_autocomplete_get_stopwords($solr);
foreach ($matches_clone as $term => $count) {
if (strlen($term) > 3 && !in_array($term, $stopwords) && !array_search($term, $keys_array)) {
}
else {
unset($matches_clone[$term]);
unset($matches[$term]);
}
}
$max_occurrence = $response->response->numFound * 0.9;
foreach ($matches_clone as $match => $count) {
if ($count > $max_occurrence) {
unset($matches_clone[$match]);
}
}
arsort($matches_clone);
$matches_clone = array_slice($matches_clone, 0, $suggestions_to_return, TRUE);
if ($response->response->numFound > 0 && $term != '') {
$suggestions['*' . $term] = array(
'value' => $term,
'count' => $response->response->numFound,
);
}
foreach ($matches_clone as $match => $count) {
if ($term != $match) {
$suggestion = trim($match);
if (substr_count($suggestion, ' ') >= 2) {
$count = FALSE;
}
if ($suggestion != '') {
$suggestions['*' . $suggestion] = array(
'value' => $suggestion,
'count' => $count,
);
}
}
}
}
return array(
'suggestions' => $suggestions,
'response' => &$response,
);
}